Welcome to WebmasterWorld Guest from 50.17.74.162

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Using Arrays and Checkboxes

     

Robert Poole

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

5+ Year Member



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

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

WebmasterWorld Senior Member 10+ Year Member



<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

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

WebmasterWorld Senior Member 10+ Year Member



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

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

Robert Poole

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

5+ Year Member



High five Habtom, great work!

Robert Poole

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

5+ Year Member



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

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

WebmasterWorld Senior Member dreamcatcher is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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

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

5+ Year Member



Thanks Dreamcatcher, you were right on point.

Robert Poole

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

5+ Year Member



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?

 

Featured Threads

Hot Threads This Week

Hot Threads This Month