homepage Welcome to WebmasterWorld Guest from 54.166.105.24
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Using Arrays and Checkboxes
Robert Poole




msg:3426726
 12:05 pm on Aug 20, 2007 (gmt 0)

Okay, first of all I'm completely useless with Arrays which is probably why I'm getting my ass kicked by this one, but anyway, here it is...

I generate a form using a database select statement so I get rows with checkboxes generated in a while loop like...

<input type='checkbox' name='id[]' value='".$row['id']."'>

and then for each checkbox which is checked I want to update the database for that item, like...

$query = ("UPDATE database SET printed = 'YES', printdate = '".date("d-m-Y")."' WHERE id='---ARRAY CLEVERNESS GOES HERE---'");

but I can't get it to work. I can get it to work with the last item that is checked but not for multiple checked items.

Any help would be appreciated.

Cheers

 

Habtom




msg:3426728
 12:10 pm on Aug 20, 2007 (gmt 0)

<input type='checkbox' name='id[]' value='".$row['id']."'>

and then for each checkbox which is checked I want to update the database for that item, like...

$id_req = $_REQUEST['id'];
foreach($id_req as $id_rows){
$query = ("UPDATE database SET printed = 'YES', printdate = '".date("d-m-Y")."' WHERE id='. $id_rows .'");
}

I can get it to work with the last item that is checked but not for multiple checked items.

That is just because you the variable contains the last assigned value or whatever :)

Any help would be appreciated.

Hope that helped.

Habtom

Habtom




msg:3426729
 12:12 pm on Aug 20, 2007 (gmt 0)

. . . not to mention that $query should be executed inside the loop itself

foreach (...) {
$query = "UPDATE . . . "
mysql_query($query);
}

Robert Poole




msg:3426798
 1:19 pm on Aug 20, 2007 (gmt 0)

High five Habtom, great work!

Robert Poole




msg:3435494
 2:24 pm on Aug 29, 2007 (gmt 0)

Okay... follow up problem!

After getting word that my table needed one more field of a similar nature I duplicated the code, so now there are two rows of checkboxes for each row, controlled by one update button. The SQL queries are now like:

$id_req = $_REQUEST['id'];
foreach($id_req as $id_rows)
{
$query = ("UPDATE hhiporders SET printed = 'YES', printdate = '".date("d-m-Y")."' WHERE id='".$id_rows."'");
mysql_query($query) or die('Error, insert query failed');
}

$arch_req = $_REQUEST['arch'];
foreach($arch_req as $arch_rows)
{
$query = ("UPDATE hhiporders SET archived = '".date("d-m-Y")."' WHERE id='".$arch_rows."'");
mysql_query($query) or die('Error, insert query failed');
}

Now the funny thing is that the code works fine and does the necessary database updates, but it still returns

Warning: Invalid argument supplied for foreach() in /...printed.php on line 36

which is the first of the two foreach statements. So why's it complaining? It works fine, I just want it not to send this error message back to me...

Any ideas?

dreamcatcher




msg:3435545
 3:08 pm on Aug 29, 2007 (gmt 0)

Hi Robert,

One of the sets of checkboxes will be empty, so the array will be empty. You can`t access an empty array using a foreach loop. You need to test the array has data before you access it:


$id_req = $_REQUEST['id'];
if (!empty($id_req))
{
foreach($id_req as $id_rows)
{
$query = ("UPDATE hhiporders SET printed = 'YES', printdate = '".date("d-m-Y")."' WHERE id='".$id_rows."'");
mysql_query($query) or die('Error, insert query failed');
}
}


$arch_req = $_REQUEST['arch'];
if (!empty($arch_req))
foreach($arch_req as $arch_rows)
{
$query = ("UPDATE hhiporders SET archived = '".date("d-m-Y")."' WHERE id='".$arch_rows."'");
mysql_query($query) or die('Error, insert query failed');
}
}

dc

Robert Poole




msg:3435568
 3:28 pm on Aug 29, 2007 (gmt 0)

Thanks Dreamcatcher, you were right on point.

Robert Poole




msg:3440897
 4:08 pm on Sep 4, 2007 (gmt 0)

Okay, the saga continues...

I now have to do a similar thing but now with text field input instead of a checkbox... so is there a way of reading in the value of a text field and maintain the ID value generated from the database?

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved