Domanda

Di seguito sto avendo dei problemi con lo script PHP su cui sto attualmente lavorando. Quello che sto cercando di fare è fare un elenco con 5 eventi che vengono mostrati per data. Nel mio database ho una tabella con eventi. Ogni evento ha una data (datetime), un ID e un nome. Ciò che il PHP deve fare è controllare la tabella con eventi e filtrare quelli che sono già passati. Se un evento è già passato, non viene mostrato. Se deve ancora succedere, viene mostrato.

Ora il problema è che nel fatto di While Loop, la sceneggiatura non sembra andare in una riga successiva quando ha avuto una corsa. Ad esempio: se la tabella del database ha 10 eventi, mostrerà 10 volte l'evento che si trova nella prima riga della tabella durante il test.

Ho bisogno di sapere cosa sto facendo di sbagliato o se c'è un modo per aumentare la riga dopo ogni corsa del ciclo.

<?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

?>
È stato utile?

Soluzione

Il codice carica la data una volta e poi lo confronta ogni volta ad oggi. Spostare

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

Nel loop, prima del controllo della data.

Altri suggerimenti

Prova questo:

<?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

?>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top