Ho un database MySQL contenente una tabella (entries) strutturata in due campi:
type | text
dove "type" può avere 5 valori, e "text" è una stringa di testo.
Tramite un form di checkboxes vorrei selezionare dal database le stringhe con uno o più type:
codice:
<html>
<head>
<title>select</title>
<META http-equiv="Content-Type" content="text/html; charset=latin-1">
</head>
<body bgcolor="#F5FAE6">
[img]img/pencil.png[/img]
<center>
<h2><p align="center">Make your test</p></h2>
</center>
<form action="output.php" method="POST">
✔Select the type(s) of exercise you need:
<table border="1" cellpadding='4' cellspacing='4' style='border-collapse: collapse' bordercolor='#9999DD'>
<tr><td><input type="checkbox" name="type[]" value="abc"/> multiple choice</td></tr>
<tr><td><input type="checkbox" name="type[]" value="error"/> mistake correction</td></tr>
<tr><td><input type="checkbox" name="type[]" value="cloze"/> cloze</td></tr>
<tr><td><input type="checkbox" name="type[]" value="makeq"/> make a question</td></tr>
<tr><td><input type="checkbox" name="type[]" value="trans"/> translate (IT-->EN)</td></tr>
</table>
<p align="center">
<input type="submit" name= "get" value="get your entries!"/>
</p>
</form>
</body>
</html>
Purtroppo, il seguente codice PHP mi restituisce solo le stringhe relative ad un "type", ignorando del tutto la selezione multipla di più checkboxes:
Codice PHP:
<html>
<head>
<title>output</title>
<META http-equiv="Content-Type" content="text/html; charset=latin-1">
</head>
<body bgcolor="#EBEBFF">
<center>
<h2><p align="center">Here are the entries for your test!</p></h2>
</center>
<?
$objConnect = mysql_connect("localhost","root","password") or die("No DB to select.");
$objDB = mysql_select_db("exercises");
$tipo = $_POST['type'];
for ($i=0; $i<sizeof($tipo);$i++) {
$strSQL = "SELECT * FROM entries WHERE type = '".$tipo[$i]."'";
$objQuery = mysql_query($strSQL);
}
?>
<?
$tipo=$_POST['type'];
if (isset($tipo)){
echo "You selected the exercise type(s):"."
";
foreach ($tipo as $key => $value )
echo "- ".$value."
";
}
else{
echo "You didn't check anything."."
";
}
?>
<?
if (isset($_POST['type'])){
while ($row = mysql_fetch_array($objQuery)) {
echo $row['text']." --> ".$row['focus']."
";
}
}
else{
echo "Please select at least one type.";
}
?>
<?
mysql_close($objConnect);
?>
</body>
</html>
Avete qualche suggerimento? Forse sto sbagliando a formulare la query?
Grazie.