你好,我剛剛8天前開始學習PHP。我正在嘗試從SQL表加載註釋,並將它們按ID排序。要做到這一點,我想使用一個數組,並添加到索引,評論ID是那麼它將按順序。PHP如何添加到任何索引的數組?
EX:(IK這不是正確的代碼)
while loop through SQL table{
array[3] = This is comment 3.
array[1] = This is comment 1.
array[0] = This is comment 0.
array[2] = This is comment 2.
}
正如你可以看到它只是通過它們的索引放入正確的順序意見。我將如何在PHP中執行此操作?
這裏是我迄今爲止,它不工作:
$return = "";
$array = array();
$lowers = 0;
$res2 = mysql_query("SELECT * from `".Mod::$id."_comments`");
if($res2){
while($row = mysql_fetch_assoc($res2)){
if($row['id'] < $lowers){
$lowers = $row['id'];
}
$name = $row['id'] . "_delete";
$array[$row['id']] = "<div class=\"caption\"><hr><h5>"
. getUserByUUID($row['uuid']) . " (" . date('m/d/Y', $row['timestamp']) . ") <input type=\"button\" name=\"" . $name .
"\" id=\"" . $name . "\" value=\"Delete\" onClick=\"CALLJAVASCRIPTFUNCTIONHERE()\" ></h5>
<figure class=\"img-polaroid\">" .
$row['comment'] . "</figure>
</div>";//TODO CHANGE THE CALL JAVA SCRIPT FUNCTION TO THE PROPER FUNCTION!!!!!
alert("ARRAY: " . $array[$row['id']]);//This is getting called but it does nothing. Also I made a php function called alert that DOES work.
}
}
for($loop = $lowers; $loop < (count($array) + $lowers); $loop++){
alert("LOOP: " . $loop);
$return = $return + $array[$loop];
}
return $return;
for($loop = $lowers; $loop < (count($array) + $lowers); $loop++){
alert("LOOP: " . $loop);
$return = $return + $array[$loop];
}
return $return;
感謝您的任何幫助。
爲什麼要用這個數組呢這可以通過簡單的查詢來完成 – 2014-10-04 03:22:37