Ciao a tutti,
questo è il mio primo post qui quindi scusatemi per eventuali errori o imprecisioni.
Dovrei realizzare un semplice script in php/mysql che, partendo da una lista di nomi, possa assegnare ad ognuno di essi uno "stato" a scelta tra 3 opzioni, per gestire le presenze di quest'ultimi.
Mi spiego meglio.
nel DB ho 2 tabelle:
personale (id, nome)
presenza (id, data, nome, presenza)
faccio la query per estrarre i nomi e tento di inserire inserisco i dati in un form con pulsanti "radio"
Codice PHP:
$query = "SELECT * FROM personale";
$result = mysql_query($query);
if (!$result) { echo 'ERRORE: ' . mysql_error();
exit;
}
echo "<table style=\"border:1px solid black;border-collapse:collapse;\"><form action=\"./presenza.php\" method=\"POST\">";
while($row = mysql_fetch_array($result)){
echo "<tr><td style=\"border-bottom:1px solid black;width:180px;\"><input type=\"text\" name=\"personale\" value=\"".$row['nome'] ."\"></td><td style=\"border-bottom:1px solid black;width:300px;\"><input type=\"radio\" name=\"presenza\" value=\"presente\">Presente<input type=\"radio\" name=\"presenza\" value=\"assente\">Assente<input type=\"radio\" name=\"presenza\" value=\"ingiustificato\">Ingiustificato</td></tr>"; } echo "<tr><td></td><td><input type=\"submit\" value=\"Invia\"></form></td></tr></table>";
non capisco come devo terminare il form perchè così riesco a selezionare solo un bottone tra tutte le righe invece io ho bisogno che ogni riga sia a sè stante.
In seguito nel file "presenza.php" mi areno proprio. Ho trovato qualche esempio su google e provato ad adattarlo ma mi dà sempre errore sul costrutto foreach!!
Codice PHP:
$sql_start = 'INSERT INTO `presenze` (nome,presenza) VALUES ';
// We'll use this at the beginning of each query
$sql_array = array(); // This is where we'll queue up the rows
$queue_num = 20; // How many rows should be queued at once?
foreach ($_POST['personale'] as $row=>$name) {
$personale = $name; $presenza = $_POST['presenza'][$row];
$sql_array[] = '(' . $personale . ', ' . $presenza . ')'; // Add a new entry to the queue
if (count($sql_array) >= $queue_num) { // We have reached the queue limit mysql_query($sql_start . implode(', ', $sql_array)); // Insert those that are queued up $sql_array = array(); // Erase the queue } } if (count($sql_array) > 0) // There are rows left over
{ mysql_query($sql_start . implode(', ', $sql_array)); }
Avete qualche suggerimento??
Ciao a tutti e grazie Roberto