Page 1 of 1
LIST TABLE and saving with checkboxes
Posted: Sun Oct 29, 2006 11:28 am
by Gurzi
Hy all..
I'm doing one library database and at the first page(index.php) should appear the 10 most important books..
That list have to be made by admin who have one login to their private place.
My problem is when i select all the data on te table will appear one thing like this
SUBNAME,NAME,TITTLE,<input type="checkbox" name="">
so.. when the admin press submit how i know wich checkbox is checked if?
The problem still on the name ... i have to create one trick to autoname the checkboxes because the list of books can be more than 500 units... ..
Anyone have one ideia ?
ps : my english is a little bad
sorry

Posted: Sun Oct 29, 2006 11:39 am
by John Cartwright
typically you want to store all the checkboxes in an array, ala..
Code: Select all
if (isset($_POST['books']) && count($_POST['books']))
{
echo '<pre>';
print_r($_POST['books']);
echo '</pre>';
}
while ($row = mysql_fetch_assoc($result))
{
echo $row['book_name'];
echo '<input type="checkbox" name="books['.$row['book_id'].']" '. (isset($_POST['books'][$row['book_id']]) ? 'checked = "checked"' : '').'> <br />';
}
Try modifying this code to suit your needs, and as you can see all your checked checkboxes will be stored in $_POST['books'] as an array. You'll also notice that by checking for the existance of the $_POST['books']['id_number'] within the designated checkbox, it will automatically re-check it by adding checked="checked".
Posted: Sun Oct 29, 2006 12:12 pm
by Gurzi
how the php knows what is $row['book_name']; ?

Posted: Sun Oct 29, 2006 12:20 pm
by John Cartwright
replace book_name with the column which stores the book name in mysql, that goes for book_id aswell
Posted: Sun Oct 29, 2006 12:36 pm
by Gurzi
Thanks, i'm trying to understand your code because i dont understand nothing about php:P
at this point i'm not understanding why the if(_$POST) dont work when i press submit..
it should echo "BOI" and is not.include("settings.php");
Code: Select all
// Destaques.php
// Aqui Pode colocar os destaques dos Livros no index.php
$connect = mysql_connect($server,$user,$pass);
$select_db = mysql_select_db($db);
$query = "SELECT Autor.Apelido,Autor.Nome,Livro.Titulo FROM Autor,Autoria,Livro WHERE Autor.IDAutor=Autoria.IDAutor AND Autoria.IDLivro=Livro.IDLivro ORDER BY Autor.Apelido";
$do_query=mysql_query($query);
$num = count($row);
echo "<table border='1' align='center'><FORM NAME='BOOKS' METHOD='POST'>
<TR><TD>APELIDO</TD><TD>NOME</TD><TD>LIVRO</TD><TD>DESTAQUES</TD></TR>";
while($row= mysql_fetch_array($do_query, MYSQL_NUM)){
printf("<tr><td> %s </td><td>%s</td><td>%s</td><td><input type='checkbox' name=><br>", $row[0],$row[1],$row[2]);
}
echo "<tr><td align='center' rowspan='3' colspan='4'><input type='submit' value='Destacar'></td></tr></table></form>";
if ($_POST)
{
ECHO "BOI";
EXIT(0);
}
Posted: Sun Oct 29, 2006 12:39 pm
by John Cartwright
compare my code to yours, and adjust accordingly.
Posted: Sun Oct 29, 2006 12:42 pm
by Gurzi
i tried but i dont understand this
echo '<input type="checkbox" name="books['.$row['book_id'].']" '. (isset($_POST['books'][$row['book_id']]) ? 'checked = "checked"> <br />';
}
there are to much [] and . it make me confusious
and .(isset($_POST[$_POST['books'][$row['book_id']] ? 'checked = "checked">
and is weird my code didn't work..
I expected the echo .. but ...

Posted: Sun Oct 29, 2006 12:47 pm
by John Cartwright
I said to only replace book_id with the column name which stores the book id's..
I made a couple adjustments to my post with the code, as I had a parse error. Take another look.