nimzie Posted January 3, 2008 Share Posted January 3, 2008 I have created an HTML table by looping through a database. My checkboxes are all created by naming them as: <input type=\"checkbox\" name=\"checkbox_" . $i . "\" > </td>" . Which creates checkbox_1 - checkbox_x So , when they click the button and my form action comes in to play, I don't know how to loop through these checkboxes and check values in each row to insert / update the database and do other processing with it. I'm not sure if I should re-run the query to get the value of $i again and use that in the loop or if there is another way to look at the $POST var and check all the checkbox_x values til I don't have one? I appreciate any help with this. Thanks, Adam Quote Link to comment https://forums.phpfreaks.com/topic/84283-solved-processing-a-table-of-checkboxes-records/ Share on other sites More sharing options...
rajivgonsalves Posted January 3, 2008 Share Posted January 3, 2008 you should of done <input type=\"checkbox\" name=\"checkbox[]\" > and the in the next page $arrCheckbox = $_POST['checkbox']; Quote Link to comment https://forums.phpfreaks.com/topic/84283-solved-processing-a-table-of-checkboxes-records/#findComment-429211 Share on other sites More sharing options...
nimzie Posted January 4, 2008 Author Share Posted January 4, 2008 so by calling name=checkbox[], I am making it an array name? I'm not understanding the syntax... Then, in the next line, I'm getting the checkbox array from post as $array = $POST['checkbox'] - and then I can loop through it? Please help me to understand the top line of syntax and how this array happens - or if this is what is really going on here... Thanks, Adam Quote Link to comment https://forums.phpfreaks.com/topic/84283-solved-processing-a-table-of-checkboxes-records/#findComment-430389 Share on other sites More sharing options...
nimzie Posted January 4, 2008 Author Share Posted January 4, 2008 and to build the checkbox[] array as I go through the data and set out the controls .. how would I do this in to the array? Thanks for any help Adam Quote Link to comment https://forums.phpfreaks.com/topic/84283-solved-processing-a-table-of-checkboxes-records/#findComment-430433 Share on other sites More sharing options...
marcus Posted January 4, 2008 Share Posted January 4, 2008 <input type="checkbox" name="name[]" value="1"><br> <input type="checkbox" name="name[]" value="2"><br> <input type="checkbox" name="name[]" value="3"><br> <input type="checkbox" name="name[]" value="4"><br> <input type="checkbox" name="name[]" value="5"><br> <?php $name = $_POST['name']; if(count($name) > 0){ foreach($name AS $names){ echo $names . "<br>\n"; } }else { echo "Please select one!\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/84283-solved-processing-a-table-of-checkboxes-records/#findComment-430436 Share on other sites More sharing options...
nimzie Posted January 4, 2008 Author Share Posted January 4, 2008 if I were loopng through the result of a query, how would I create the checkboxes in this case? Thanks for your help so far, this is great Quote Link to comment https://forums.phpfreaks.com/topic/84283-solved-processing-a-table-of-checkboxes-records/#findComment-430696 Share on other sites More sharing options...
nimzie Posted January 6, 2008 Author Share Posted January 6, 2008 you should of done <input type=\"checkbox\" name=\"checkbox[]\" > and the in the next page $arrCheckbox = $_POST['checkbox']; Can you illustrate going through array results and creating a checkbox for each record in a loop? I'm having a hard time understanding how checkbox[] etc will all have unique values etc so I can loop through later... Thanks, Adam Quote Link to comment https://forums.phpfreaks.com/topic/84283-solved-processing-a-table-of-checkboxes-records/#findComment-431793 Share on other sites More sharing options...
Ken2k7 Posted January 6, 2008 Share Posted January 6, 2008 mimzie, the creation of the checkboxes is done exactly as you did it in the first post. You just need to replace the name attribute with an array. So like you can generate all the checkboxes and then put in a submit button. Use php to check if the submit button is clicked. If so, you can use $_POST['checkbox'] to get the entire array and store that in a variable. The 'value' field that you assign to <input> (like the one mgallforever posted) will be the values in the array. You can loop through that and get the values. Quote Link to comment https://forums.phpfreaks.com/topic/84283-solved-processing-a-table-of-checkboxes-records/#findComment-431939 Share on other sites More sharing options...
nimzie Posted January 9, 2008 Author Share Posted January 9, 2008 I have gotten a chance to work on this piece a little again and ran in to something else... foreach($name AS &$names){ $key = $key + 1; echo "Key is " . $key . "<br />"; if ( $key = 1 ){ $BatchIDs = "($names"; } else if ( $key < $theCount ){ $BatchIDs .= "$names,"; } else { $BatchIDs .= "$names)"; } //else }//foreach } //if(count)>0 else { echo "There are no orders to process. Please check the applicable orders to be sent for processing.\n"; } This echos 1,2,2,2,2,2. It should echo 1,2,3,4,5,6 according to the # of iterations in the loop. I found a writeup about a bug related to foreach in php but it doesn't speak of a workaround for a situation like what I'm in. Should I even be using a foreach loop? I'm basically trying to build a SQL command as : SELECT * FROM `table`WHERE batchid IN ( 1, 2, 3, 5, 6 ) where the ( 1, 2, 3, 5, 6 ) is the $BatchIDs. Being new to php, I'm sure there is a cleaner way to do this. Thanks for all your help. I will be using php lots and have a lot of experience in Delphi which is WAY different than php, but I'll get it! - Adam Quote Link to comment https://forums.phpfreaks.com/topic/84283-solved-processing-a-table-of-checkboxes-records/#findComment-434524 Share on other sites More sharing options...
nimzie Posted January 9, 2008 Author Share Posted January 9, 2008 This is likely pretty hacky code, but does what I need: $name = $_POST['checkbox']; if(count($name) > 0){ $BatchIDs = "("; foreach($name AS $names){ $BatchIDs .= "$names,"; }//foreach $BatchIDs = substr_replace($BatchIDs ,"",-1); $BatchIDs = $BatchIDs . ")"; Anyone got any comments on how to make this sexier? Thanks, Adam Quote Link to comment https://forums.phpfreaks.com/topic/84283-solved-processing-a-table-of-checkboxes-records/#findComment-434687 Share on other sites More sharing options...
Ken2k7 Posted January 9, 2008 Share Posted January 9, 2008 Well if it works fine, leave it as is. As for a more simplified version (which isn't that much more simpler than it is): $name = $_POST['checkbox']; $BatchIDs = ""; if(count($name)){ foreach($name AS $names){ $BatchIDs .= "$names,"; }//foreach $BatchIDs = "(" . substr_replace($BatchIDs ,"",-1) . ")"; Quote Link to comment https://forums.phpfreaks.com/topic/84283-solved-processing-a-table-of-checkboxes-records/#findComment-434695 Share on other sites More sharing options...
nimzie Posted January 9, 2008 Author Share Posted January 9, 2008 Thank you Ken. Appreciate your suggestion Quote Link to comment https://forums.phpfreaks.com/topic/84283-solved-processing-a-table-of-checkboxes-records/#findComment-434891 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.