Php Mysql - visualizzare record per anno con menu a tendina

freddie24

Utente Attivo
21 Ott 2011
56
0
0
Ciao a tutti,

ho un database con una tabella "eventi" i cui record sono : ID , data (YYYY-MM-DD) , evento , luogo.

visto che dovrò inserire degli eventi che vanno dal 2000 ad oggi, sarebbe più comodo visualizzarli secondo l'anno tramite un menu a tendina.

ecco il mio codice :

PHP:
<?php
include ("config.php"); 
include ("connect.php"); 

// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM eventi ORDER BY CAST(data AS DATE)") 
or die(mysql_error());  


//echo "<tr> <th>Data</th> <th>   Evento   </th> <th>Luogo</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
	// Print out the contents of each row into a table
		
	echo "<table  width='100%' border='1'>";
  echo"<tr>";
    echo "<th width='10%'>Data</th>";
    echo"<td>";
	echo $row['data'];
	echo"</td>";
  echo"</tr>";
  echo"<tr>";
    echo"<th width='10%'>Luogo</th>";
    echo"<td>";
	echo $row['luogo'];
	echo"</td>";
  echo"</tr>";
  echo"<tr>";
    echo"<th width='10%'>Evento</th>";
    echo"<td>";
	echo $row['evento'];
	echo"</td>";
  echo"</tr>";
echo"</table>";

echo"<br/><br/>";
	
	
} 


?>

Mi aiutate?

Ringrazio in Anticipo
 
Una soluzione potrebbe essere questa

PHP:
<?php
include ("config.php"); 
include ("connect.php"); 

// Se è stato scelto l'anno
if (isset($_POST['anno']) && $_POST['anno'] != "") {
    $anno = $_POST['anno'];
} else {
    // altrimenti impostiamo quello corrente
    $anno = date('Y');
}

// Costruiamo la query
$query = "SELECT * FROM eventi WHERE YEAR(data) = '$anno' ORDER BY CAST(data AS DATE)";

$result = mysql_query($query) or die(mysql_error());

echo "<form method='post' action='" . $_SERVER['PHP_SELF'] . "'>\n";

// al cambio verrà inviato il post
echo "<select name='anno' onchange='this.form.submit();'>\n";

for ($i = date('Y'); $i >= 2000; $i--) {
    echo "<option value='$i'";
    if ($anno == $i) {
        echo " selected='selected'";
    }
    echo ">$i</option>\n";
}

echo "</select>\n";

echo "</form>\n";
//echo "<tr> <th>Data</th> <th>   Evento   </th> <th>Luogo</th> </tr>";
// keeps getting the next row until there are no more to get

while ($row = mysql_fetch_array($result)) {
    // Print out the contents of each row into a table
    echo "<table  width='100%' border='1'>";
    echo"<tr>";
    echo "<th width='10%'>Data</th>";
    echo"<td>";
    echo $row['data'];
    echo"</td>";
    echo"</tr>";
    echo"<tr>";
    echo"<th width='10%'>Luogo</th>";
    echo"<td>";
    echo $row['luogo'];
    echo"</td>";
    echo"</tr>";
    echo"<tr>";
    echo"<th width='10%'>Evento</th>";
    echo"<td>";
    echo $row['evento'];
    echo"</td>";
    echo"</tr>";
    echo"</table>";

    echo"<br/><br/>";
}
?>
 
Ciao Grazie 1000 per l'aiuto!

ho modificato il tuo codice aggiungendo +1 al "ciclo for" perchè in programma potrebbero esserci degli eventi anche nell'anno successivo a quello attuale:

PHP:
//ho aggiunto +1 perchè in programma potrebbero esserci degli eventi anche nell'anno successivo.
for ($i = date('Y')+1; $i >= 2000; $i--)


e se nel menu a tendina volessi visualizzare solo gli anni che contengono degli eventi??? :rolleyes:

questa sarebbe un'ottima soluzione...


Comunque SEMPRE grazie!
 
Potresti creare un array con gli anni che hai sul db e usarlo per popolare la select

PHP:
<?php
include ("config.php"); 
include ("connect.php");  

// crei un array con gli anni disponibili nel db
$query = "SELECT YEAR(data) as anno FROM `eventi` GROUP BY anno DESC";
$result = mysql_query($query) or die(mysql_error());
$array_anni = array();
while ($anni = mysql_fetch_array($result)) {
    $array_anni[] = $anni['anno'];
}

// Se è stato scelto l'anno
if (isset($_POST['anno']) && $_POST['anno'] != "") {
    $anno = $_POST['anno'];
} else {
    // altrimenti impostiamo quello corrente
    $anno = date('Y');
}

// Costruiamo la query
$query = "SELECT * FROM eventi WHERE YEAR(data) = '$anno' ORDER BY CAST(data AS DATE)";

$result = mysql_query($query) or die(mysql_error());

echo "<form method='post' action='" . $_SERVER['PHP_SELF'] . "'>\n";


// al cambio verrà inviato il post
echo "<select name='anno' onchange='this.form.submit();'>\n";

// Cili l'array degli anni recuperati
foreach ($array_anni as $value) {
    echo "<option value='$value'";
    if ($anno == $value) {
        echo " selected='selected'";
    }
    echo ">$value</option>";
}

echo "</select>\n";

echo "</form>\n";
//echo "<tr> <th>Data</th> <th>   Evento   </th> <th>Luogo</th> </tr>";
// keeps getting the next row until there are no more to get

while ($row = mysql_fetch_array($result)) {
    // Print out the contents of each row into a table
    echo "<table  width='100%' border='1'>";
    echo"<tr>";
    echo "<th width='10%'>Data</th>";
    echo"<td>";
    echo $row['data'];
    echo"</td>";
    echo"</tr>";
    echo"<tr>";
    echo"<th width='10%'>Luogo</th>";
    echo"<td>";
    echo $row['luogo'];
    echo"</td>";
    echo"</tr>";
    echo"<tr>";
    echo"<th width='10%'>Evento</th>";
    echo"<td>";
    echo $row['evento'];
    echo"</td>";
    echo"</tr>";
    echo"</table>";

    echo"<br/><br/>";
}
?>
 

Discussioni simili