2 回答

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");

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;
- 2 回答
- 0 關(guān)注
- 163 瀏覽
添加回答
舉報(bào)