kevinak Posted September 5, 2008 Share Posted September 5, 2008 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 https://forums.phpfreaks.com/topic/122889-for-each-loops-and-id-numbers/ Share on other sites More sharing options...
ToonMariner Posted September 5, 2008 Share Posted September 5, 2008 <?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 https://forums.phpfreaks.com/topic/122889-for-each-loops-and-id-numbers/#findComment-634684 Share on other sites More sharing options...
discomatt Posted September 5, 2008 Share Posted September 5, 2008 I think you mean <?php echo '<tr><td><input type="checkbox" name="petID['.$row[0].']"><img src="http://whimpsters.net/pets.php/'.$row[0].'"></td>'; ?> Link to comment https://forums.phpfreaks.com/topic/122889-for-each-loops-and-id-numbers/#findComment-634690 Share on other sites More sharing options...
kevinak Posted September 5, 2008 Author Share Posted September 5, 2008 Wait aren't I setting it to..? <?php echo '<tr><td><input type="checkbox" name=".$petID['$row[0]']."><img src="http://whimpsters.net/pets.php/'.$row[0].'"></td>'; ?> And so let's say we have $petID[4] and it's checked. Would $petID[4] = true? Link to comment https://forums.phpfreaks.com/topic/122889-for-each-loops-and-id-numbers/#findComment-634693 Share on other sites More sharing options...
discomatt Posted September 5, 2008 Share Posted September 5, 2008 Well, you might want togive the checkbox a value of '1' or something... but you'd be checking for the existence of $_POST['petID'][4] Link to comment https://forums.phpfreaks.com/topic/122889-for-each-loops-and-id-numbers/#findComment-634715 Share on other sites More sharing options...
kevinak Posted September 5, 2008 Author Share Posted September 5, 2008 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 https://forums.phpfreaks.com/topic/122889-for-each-loops-and-id-numbers/#findComment-634729 Share on other sites More sharing options...
discomatt Posted September 5, 2008 Share Posted September 5, 2008 yeah row[$id] will work, then just use foreach( $_POST['row'] as $id => $val ) { // $val will be 1 or whatever the checkboxes values is // $id will be a pet id that was checked. } Link to comment https://forums.phpfreaks.com/topic/122889-for-each-loops-and-id-numbers/#findComment-634815 Share on other sites More sharing options...
ToonMariner Posted September 8, 2008 Share Posted September 8, 2008 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 https://forums.phpfreaks.com/topic/122889-for-each-loops-and-id-numbers/#findComment-637030 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.