Jump to content

For each loops and ID numbers


kevinak

Recommended Posts

Ok well I have a script that is going to generate a table with images and checkboxes in each cell.

Got everything to generate properly and such.

 

But I need a way to

1. Check if one of these boxes are checked

2. If checked I need to get the ID out of them.

 

Here's a bit of my code.

 

//Here search
$pets = "SELECT Pet_ID FROM pets WHERE user_id = $userid";
$resultpets = mysql_query($pets) or die ("Error in query: $query. ".mysql_error());

//do while loop
while($row = mysql_fetch_row($resultpets)) {        

//This generates the cell with image and checkbox
echo "<tr><td><input type=checkbox name=".$row[0]."><img src=http://whimpsters.net/pets.php/".$row[0]."></td>";

//here is where my problem is.. I don't know how to store the pet id and isset into one array.
$tradeID[$row[0]] = isset($_POST["$row[0]"]);

 

More on that.

 

//this is basically all I want to be able to do
foreach( $tradeID as $key => $value){
//need to grab the Pet_ID somehow and check if it is checked 
}

 

Problem is that I get "Invalid argument supplied for foreach()" returned

 

I don't know how confusing that was. But let me know if you have any ideas

 

Thank you for reading.

 

 

Link to comment
Share on other sites

<?php
echo "<tr><td><input type=checkbox name=".$row[0]."><img src=http://whimpsters.net/pets.php/".$row[0]."></td>";
?>

 

change to...

 

<?php
echo '<tr><td><input type="checkbox" name="'petID.$row[0].']"><img src="http://whimpsters.net/pets.php/'.$row[0].'"></td>';
?>

 

the in your processing script you can check against $_POST['petID'] which will be an array of all the IDs you have checked.

 

use print_r($_POST['petID']) to see what's in the array...

Link to comment
Share on other sites

well can I just set the value of the each checkbox generated to row[0] or the pet ID number?

 

Then extract that with a for each loop?

 

Or how can I just check them all and pull out the ID's that are checked?

 

That's what I'm really trying to do here.

Link to comment
Share on other sites

apologies - yes my original post should have been "petID['.row[0].']"  you can set what ever value you like - all you need to do is check whick keys (if you use array_keys($_POST['petID']) - you'll get them!) have been passed...

 

radio buttons and check boxes are slightly different in that they are only set when they have been checked - unlik such things as text inputs which are still set but have an empty value if no text were entered...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.