簡體   English   中英

如何在使用PHP / MYSQL循環時檢查Next Row的值?

[英]How can I check the value of the Next Row while looping using PHP/MYSQL?

我使用此代碼得到了previousRow的記錄

<?php
  $previousRow = array();
  while ($temp = mysql_fetch_row($res2)) 
 {

     echo "<br>currentRow:".$temp[1];
     echo "previousRow:".$previousRow[1];
     $previousRow = $temp; 

  } 
 ?>

oupout

currentRow:1previousRow:

currentRow:5previousRow:1

currentRow:6previousRow:5

currentRow:7previousRow:6

currentRow:8previousRow:7

如何檢查上一行替換的下一行的值?

任何幫助將不勝感激。

如果我找到你,那么這樣的事情會有所幫助嗎?

$previousRow = array();
$currentRow = mysql_fetch_row($res2);

while ($currentRow) {
    $nextRow = mysql_fetch_row($res2);

    echo "<br>currentRow:".$currentRow[1];
    echo "previousRow:".$previousRow[1];
    echo "nextRow:".$nextRow[1];

    $previousRow = $currentRow;
    $currentRow = $nextRow;
}

請嘗試下面給出的代碼。

$res = array();
while ($result = mysql_fetch_row($r)) {
    $res[] = $result;
 }
 echo "<pre>";
 foreach($res AS $index=>$res1){
     echo "Current".$res1[1]; 
     echo "  Next" . $res[$index+1][1];
     echo "  Prev" . $res[$index-1][1]; echo "<br>";
 }

謝謝

我會首先收集所有行,然后用以下內容遍歷它們:

<?php
$rows = array();
while ($temp = mysql_fetch_row($res2)) 
{
    $rows[] = $temp;
}
$rowCount = count($rows);
for ($i = 0; $i < $rowCount; $i++) {
     echo "<br>currentRow:".$rows[$i][1];
     if ($i > 0) {
         echo "previousRow:".$rows[$i - 1][1];
     }
         if ($i + 1 < $rowCount - 1) {
             echo "nextRow:".$rows[$i + 1][1];
         }
} 
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM