Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Checkbox loop, I'm lost.
Old 12-20-2008, 10:27 AM Checkbox loop, I'm lost.
Sharkness's Avatar
Novice Talker

Posts: 13
Name: Mike M
Trades: 0
So I have a list of fonts on my website this have checkbox's to delete them... but I'm a little lost in how you would run the loop to delete them from a MySQL table

This is how I see it, you click Delete with 2 checkbox's ticked, this is grabbed by the delete page by using $_POST[ID] right but how do you use a foreach loop?

Code:
foreach($_POST['ID'] as $ID){

// some code

DB::query("DELETE FROM fonts WHERE ID='$_POST[ID][$ID]'");

}
Am I close to being correct, or am I going in completely the wrong direction?

Last edited by Sharkness; 12-20-2008 at 10:36 AM..
Sharkness is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 12-20-2008, 10:47 AM Re: Checkbox loop, I'm lost.
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
You can't use an array in double quotes like that.
PHP Code:
foreach($_POST['ID'] as $ID){

// some code

DB::query("DELETE FROM fonts WHERE ID='".$_POST['ID'][$ID]."'");


__________________
<?php ($helpfull>0)?$talkupation++ : '';?>
Insensus is offline
Reply With Quote
View Public Profile
 
Old 12-20-2008, 11:05 AM Re: Checkbox loop, I'm lost.
Sharkness's Avatar
Novice Talker

Posts: 13
Name: Mike M
Trades: 0
Thanks for the reply.

I tried running it and I get this error.

Warning: Invalid argument supplied for foreach()

Which is on the line of the foreach()
Sharkness is offline
Reply With Quote
View Public Profile
 
Old 12-20-2008, 12:10 PM Re: Checkbox loop, I'm lost.
Sharkness's Avatar
Novice Talker

Posts: 13
Name: Mike M
Trades: 0
Fixed, it was due to my checkbox's having the same name.
Sharkness is offline
Reply With Quote
View Public Profile
 
Old 12-20-2008, 04:22 PM Re: Checkbox loop, I'm lost.
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
To save some time, you should know that when you have a multi-dimensional array (like $_POST is), you can loop over it like this:
PHP Code:
foreach($_POST as $key=>$value) {
echo 
$key;//the name of the associative array.
echo $value;//the name of the value associated with it.

This is especially useful when the indexes are associative (as opposed to numerical). I use this type of loop a lot when patching together quick mail functions.
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.
wayfarer07 is online now
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 12-22-2008, 04:30 AM Re: Checkbox loop, I'm lost.
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
Note: you CAN use multidimensional arrays in strings if you delimit them correctly.
PHP Code:
DB::query("DELETE FROM fonts WHERE ID='{$_POST[ID][$ID]}'"); 
Pay attention to curly brackets. From version 5.0 if I am not mistaken you can even call class methods inline:
PHP Code:
DB::query("DELETE FROM fonts WHERE ID='{$object->method()}'"); 
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 12-24-2008, 09:45 AM Re: Checkbox loop, I'm lost.
Sharkness's Avatar
Novice Talker

Posts: 13
Name: Mike M
Trades: 0
Some brill info their guys, thanks its much appreciated, I shall have another look over it.
Sharkness is offline
Reply With Quote
View Public Profile
 
Old 12-25-2008, 12:31 AM Re: Checkbox loop, I'm lost.
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Also you can typecast a var to an array to avoid potential problems:

PHP Code:
foreach ((array)$_POST['ID'] AS $ID) . . . 
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Checkbox loop, I'm lost.
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.26515 seconds with 12 queries