Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

list with checkboxes

Codes here !

Moderators: macek, egami, gesf

list with checkboxes

Postby tranquillo » Thu Jun 05, 2003 4:56 pm

Hi...

I've got a litle problem.. I thought I could handle it myself, but I just gave up a couple of sec's ago...

I'm building a admin page for a website. the thing I like to do now, but can't seem to figure out how, is to make a list of all the entrys in a table in the database (i.e. news) and with each entry I'd like a checkbox. so the user can just check the box and press on the submit button to delete the entry.

I got this far:
---------------------
if ($myrow = mysql_fetch_array($result)) {
$rb=0;
do
{
printf("
<p><label class='p_rubrik'><input type='checkbox' name='$rb' value='%s'>
%s</label><br><br>
<div class='p_text'>%s</div><br>
<div class='p_text'>%s</div></p>\n", $myrow["ID"], $myrow["rubrik"], $myrow["datum"], $myrow["text"]);
// echo "$rb";
$rb++;
} while ($myrow = mysql_fetch_array($result));
}
--------------
and I made a couple of brave atempts to solve the problem but faild...

this is what I'm trying to do:
----------
if (mysql result)
print out entrys with a checkbox($checkbox=$id)
while
there are rows

if(box is checked)
store the checked boxes somewhere
$count++

for(i=0;i<$count;i++)
query:delete from nyheter where id=($checkbox);
--------------
can anyone give me a hint?
tranquillo
New php-forum User
New php-forum User
 
Posts: 74
Joined: Sun Nov 24, 2002 6:48 pm
Location: katrineholm

Postby Redcircle » Fri Jun 06, 2003 5:25 am

make sure every checkbox has the SAME name and add [] to the end when you post it will be in an array that php can use.. Any ones that are checked will be put into an array and all you have to do is loop through the array.

also I would just use a plain echo not printf it's easier to understand. Also stay away from the do-while extra code that's not necessary just use while.

an example of some code using your example would be.

Code: Select all
while ($myrow = mysql_fetch_array($result)) {
{
echo '<p><label class="p_rubrik"><input type="checkbox" name="delete[]" value="'.$myrow["ID"].'"'>'.$myrow["rubrik"].'</label><br><br>
<div class="p_text">'.$myrow["datum"].'</div><br>
<div class="p_text">'.$myrow["text"].'</div></p>';
}



the code that handles the form would be
Code: Select all
for($i=0; $i<sizeof($_POST['delete']); $i++)
{
$query = 'delete from nyheter where id = '.$_POST['delete'][$i];
}


hope that gives you some help
User avatar
Redcircle
Moderator
Moderator
 
Posts: 830
Joined: Tue Jan 21, 2003 10:42 pm
Location: Michigan USA

Postby tranquillo » Fri Jun 06, 2003 6:05 am

thanks. I think it helped a lot... I'm not done messing with the code, but so far it looks good. had to do a couple of minor changes, but thanks a lot!
tranquillo
New php-forum User
New php-forum User
 
Posts: 74
Joined: Sun Nov 24, 2002 6:48 pm
Location: katrineholm

Postby tranquillo » Fri Jun 06, 2003 6:42 am

got it now... thanks a lot!!!

this is how I did it:
------------------------------

if($_POST['delete'])
{
for($i=0; $i<sizeof($_POST['delete']); $i++)
{
$query = 'delete from nyheter where id = '.$_POST['delete'][$i];
$result2 = mysql_query($query);
}
}

$result = mysql_query("SELECT * FROM nyheter order by ID desc",$db);

if ($myrow = mysql_fetch_array($result)) {
$rb=0;

do
{
echo '<p><label class="p_rubrik">
<input type="checkbox" name="delete[]" value="'.$myrow["ID"].'">
'.$myrow["rubrik"].'</label><br><br>
<div class="p_text">'.$myrow["datum"].'</div><br>
<div class="p_text">'.$myrow["text"].'</div></p>';
}
while ($myrow = mysql_fetch_array($result));
}
?>
tranquillo
New php-forum User
New php-forum User
 
Posts: 74
Joined: Sun Nov 24, 2002 6:48 pm
Location: katrineholm


Return to mySQL & php coding

Who is online

Users browsing this forum: Google [Bot] and 3 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron