第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

在 php 中創(chuàng)建動(dòng)態(tài) JSON 數(shù)組

在 php 中創(chuàng)建動(dòng)態(tài) JSON 數(shù)組

PHP
汪汪一只貓 2023-10-15 15:06:44
我想像這樣動(dòng)態(tài)創(chuàng)建一個(gè) JSON:{"storie":[{"story_id":"111","username":"Username1","profile_photo":"boh.png","copertina":"ok.png","num_elements":"1","rating":"4.5"},{"story_id":"222","username":"Username2","profile_photo":"hello.png","copertina":"hi.png","num_elements":"2","rating":"3.5"}]}我正在嘗試從 MySQL 數(shù)據(jù)庫(kù)獲取值,并且我能夠做到這一點(diǎn):$response = array();$sql = mysqli_query($conn, "SELECT * FROM storie WHERE userid IN (SELECT following FROM follow WHERE follower='$userid')");while($row = mysqli_fetch_assoc($sql)){  $usern = getuserinfo($row['userid'], "username", $conn);  $prof_photo = getuserinfo($row['userid'], "profile_photo", $conn);  $idsto=$row['storia_id'];  $elem = mysqli_query($conn, "SELECT COUNT(*) AS da_vedere FROM `storie_images` WHERE storia_id='$idsto' AND imm_id NOT IN (SELECT imm_id FROM image_views WHERE viewer='$userid')");  while($ok = mysqli_fetch_assoc($elem)){    $num_elem = $ok['da_vedere'];  }  //here I put the line that add the array to the json array:}但問(wèn)題是這一行,它應(yīng)該創(chuàng)建一個(gè)新數(shù)組并將其放入 json 中:$response['storie'] = [array("story_id" => $idsto, "username" => $usern, "profile_photo" => $prof_photo, "copertina" => $row['copertina'], "num_elements" => $num_elem, "rating" => "4.5")];當(dāng)只有一條記錄時(shí)它有效,但如果有更多記錄它就不起作用。有人能幫我嗎?
查看完整描述

2 回答

?
拉風(fēng)的咖菲貓

TA貢獻(xiàn)1995條經(jīng)驗(yàn) 獲得超2個(gè)贊

只需替換:

$response['storie'] = [array("story_id" => $idsto, "username" => $usern, "profile_photo" => $prof_photo, "copertina" => $row['copertina'], "num_elements" => $num_elem, "rating" => "4.5")];

$response['storie'][] = array("story_id" => $idsto, "username" => $usern, "profile_photo" => $prof_photo, "copertina" => $row['copertina'], "num_elements" => $num_elem, "rating" => "4.5");


查看完整回答
反對(duì) 回復(fù) 2023-10-15
?
呼啦一陣風(fēng)

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超6個(gè)贊

那是因?yàn)槟?while 內(nèi)使用 $response['story'] 來(lái)解決此問(wèn)題:


1-創(chuàng)建另一個(gè)名為 result 的數(shù)組,然后在 while 中使用它


$response = array();

$result = array ()

while (....)

{

//here I put the line that add the array to the json array:

$result[] = array("story_id" => $idsto, "username" => $usern, "profile_photo" => $prof_photo, "copertina" => $row['copertina'], "num_elements" => $num_elem, "rating" => "4.5");

2-然后在循環(huán)外使用響應(yīng)數(shù)組:


$response['storie'] = $result;

你的代碼將是這樣的:


   $response = array();

   $result = array ()


    $sql = mysqli_query($conn, "SELECT * FROM storie WHERE userid IN (SELECT following FROM follow WHERE follower='$userid')");

    while($row = mysqli_fetch_assoc($sql)){

    $usern = getuserinfo($row['userid'], "username", $conn);

    $prof_photo = getuserinfo($row['userid'], "profile_photo", $conn);

    $idsto=$row['storia_id'];

    $elem = mysqli_query($conn, "SELECT COUNT(*) AS da_vedere FROM `storie_images` WHERE storia_id='$idsto' AND imm_id NOT IN (SELECT imm_id FROM image_views WHERE viewer='$userid')");

    while($ok = mysqli_fetch_assoc($elem)){

    $num_elem = $ok['da_vedere'];

    }

    //here I put the line that add the array to the json array:

    $result[] = array("story_id" => $idsto, "username" => $usern, "profile_photo" => $prof_photo, "copertina" => $row['copertina'], "num_elements" => $num_elem, "rating" => "4.5");

    }


   $response['storie'] = $result;


查看完整回答
反對(duì) 回復(fù) 2023-10-15
  • 2 回答
  • 0 關(guān)注
  • 163 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)