LIST TABLE and saving with checkboxes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Gurzi
Forum Commoner
Posts: 27
Joined: Wed Aug 02, 2006 4:04 pm

LIST TABLE and saving with checkboxes

Post 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 :P

sorry :D
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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".
Last edited by John Cartwright on Sun Oct 29, 2006 12:47 pm, edited 1 time in total.
Gurzi
Forum Commoner
Posts: 27
Joined: Wed Aug 02, 2006 4:04 pm

Post by Gurzi »

how the php knows what is $row['book_name']; ? :(
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

replace book_name with the column which stores the book name in mysql, that goes for book_id aswell
Gurzi
Forum Commoner
Posts: 27
Joined: Wed Aug 02, 2006 4:04 pm

Post 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);
} 
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

compare my code to yours, and adjust accordingly.
Gurzi
Forum Commoner
Posts: 27
Joined: Wed Aug 02, 2006 4:04 pm

Post 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 :P

and .(isset($_POST[$_POST['books'][$row['book_id']] ? 'checked = "checked">

and is weird my code didn't work..

I expected the echo .. but ... :(
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
Post Reply