Ho una tabella(movietype) in un db con i seguenti valori:
movietype_id movietype_label
1 Drammatico
2 Avventura
3 Commedia
4 Guerra
5 Azione
6 Bambini
7 Horror
Scrivo il seguente codice:
<?php
$db= mysql_connect('localhost','bismark','bismark')or die ('Connessione non riuscita. Controllare i parametri');
mysql_select_db('moviesite',$db)or die (mysql_error($db));
?>
<html>
<head>
<title>Aggiungi Film</title>
</head>
<body>
<form action="commit.php" method="post">
<table>
<tr>
<td>Movie Name</td>
<td><input type="text" name="movie_name"/></td>
</tr><tr>
<td>Movie Type</td>
<td><select name="movie_type">
<?php
//Seleziona il tipo di film
$query='SELECT
movietype_id,movietype_label
FROM movietype
ORDER BY
movietype_label';
$result= mysql_query($query,$db)or die (mysql_error($db));
//Popola le opzioni della select con i risultati
while($row=mysql_fetch_assoc($result)){
foreach($row as $value){
echo'<option value="'.$row['movietype_id'].'">';
echo $row['movietype_label'].'</option>';
}
}
?>
</select></td>
</tr>
</table>
</form>
</body>
</html>
Non mi è chiaro il ciclo while:
while($row=mysql_fetch_assoc($result)){
foreach($row as $value){
echo'<option value="'.$row['movietype_id'].'">';
echo $row['movietype_label'].'</option>';
}
}
Perchè mette anche il ciclo foreach? Non è superfluo? Cosa fa esattamente il ciclo foreach?