do-while循环中的行不会增加

时间:2011-05-04 18:00:43

标签: php row increment do-while

我正在解决下面我正在处理的php脚本的一些问题。我要做的是制作一个列表,其中包含按日期排序的5个事件。 在我的数据库中,我有一个包含事件的表。每个事件都有一个日期(DATETIME),一个id和一个名字。 php需要做的是检查表中的事件并过滤已经通过的事件。如果事件已经过去,则不会显示。如果它仍然必须发生,它会显示出来。

现在问题是在do while循环中,脚本在运行时似乎没有进入下一行。 例如:如果数据库表中有10个事件,它将显示测试时表的第一行上事件的10倍。

我需要知道我做错了什么,或者是否有办法在每次循环运行后使行增加。

<?php 

    $test_query_kalender = "SELECT * FROM kalender ORDER BY datum ASC";  
    $test_result_kalender = mysql_query($test_query_kalender);
    $rij_kalender = mysql_fetch_assoc($test_result_kalender);

$vandaag_unix = time();
$datum_unix = strtotime($rij_kalender['datum']);
$i = 0; //this variable is used to insure that only 5 items are being shown on the page

do{     
if($datum_unix >= $vandaag_unix) //checks if the date of the event has already passed 
{  
    //if the date has not passed, the event will be shown
    echo "<p>" . date("d-m-Y", $datum_unix) . "&nbsp;&nbsp;" . $rij_kalender['naam'] . "</p>";
    $i++;
} 

else
{ //if it has already passed then it should put nothing, but for testing I put a line in it
    echo "<p>" . $rij_kalender['naam'] . "</p>";
}

} while(($i <= 4) && ($rij_kalender = mysql_fetch_assoc($test_result_kalender)));

echo "<p>While loop finished</p>"; //just some checking

?>

2 个答案:

答案 0 :(得分:1)

您的代码加载日期一次,然后每次将其与今天进行比较。移动

$datum_unix = strtotime($rij_kalender['datum']);

进入循环,在日期检查之前。

答案 1 :(得分:0)

试试这个:

<?php 

    $test_query_kalender = "SELECT * FROM kalender ORDER BY datum ASC";  
    $test_result_kalender = mysql_query($test_query_kalender);
    $rij_kalender = mysql_fetch_assoc($test_result_kalender);

$vandaag_unix = time();
$datum_unix = strtotime($rij_kalender['datum']);
$i = 0; //this variable is used to insure that only 5 items are being shown on the page

while($rij_kalender = mysql_fetch_assoc($test_result_kalender))
{
  $datum_unix = strtotime($rij_kalender['datum']);
  if($datum_unix >= $vandaag_unix) //checks if the date of the event has already passed 
  {  
    //if the date has not passed, the event will be shown
    echo "<p>" . date("d-m-Y", $datum_unix) . "&nbsp;&nbsp;" . $rij_kalender['naam'] . "</p>";
    $i++;
  } 
  else
  { 
    //if it has already passed then it should put nothing, but for testing I put a line in it
    echo "<p>" . $rij_kalender['naam'] . "</p>";
  }

  if ($i == 5) break;
}

echo "<p>While loop finished</p>"; //just some checking

?>