dennismonsewicz Posted October 6, 2008 Share Posted October 6, 2008 I have the following script that echoes out a checkbox like this: <?php $checkbox_qry = mysql_query("SELECT * FROM has_had_projects WHERE project = '" . $results->project . "'")or die(mysql_error()); $field = mysql_num_fields($checkbox_qry); while($row = mysql_fetch_assoc($checkbox_qry)) { for($i = 2; $i < $field; $i++) { $names = mysql_field_name($checkbox_qry, $i); $chk = $row[$names]==1?'checked="checked"':''; $numbers = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0); $title .= '<div><input type="checkbox" name="checkboxes[' . $names . ']" class="checkbox" id="' . $names . '" ' . $chk . ' value="1" /> <label for="checkboxes[' . $names . ']">' . ucwords(str_replace($numbers, '', $names)) . '</label></div>'; } echo $title; } ?> Is there anyway to save all of the names in an array and when the form is submitted the script will traverse through the array and print out the names of the checkboxes that weren't checked? Quote Link to comment https://forums.phpfreaks.com/topic/127223-solved-array-help/ Share on other sites More sharing options...
ibechane Posted October 6, 2008 Share Posted October 6, 2008 Just so you know, if you use mysql_fetch_row instead of mysql_fetch_assoc, you don't need to use mysql_field_name. You can just write $row[1] and $row[2] (or $row[$i]). To solve your problem, what you can do is upon receiving the data (before you initially insert it into the database), create an array with all of the checked items, something like: $checked_items = array('item1', 'item3', 'item4'); Then, serialize() and escape the array. What serialize() does is turn your array into a string that can later be turned back into an array with unserialize(). The reason you want to do this is that you can't save an array into a database, but you can save it in its serialized form because serialize() returns a string representation. $serialized_array = serialize($checked_items); Make sure that before you put it in the database that you escape any quotes or mysql-unfriendly characters. // this step must be done after the mysql_connect() function $serialized_array = mysql_real_escape_string($serialized_array); // insert $serialized array into database When you want to retrieve the array, just use unserialize() on that serialized string and it will reconstruct the array that you originally had. As far as your question about checking check-boxes, it's much easier to do it by including Javascript. Here's an example: <html> <body> <form name="myform"> <input type="checkbox" name="first" /> <input type="checkbox" name="second" /> </form> </body> <script language="Javascript" type="text/javascript"> document.myform.second.checked = true; </script> </html> If you run this code, you'll see that the second checkbox is checked. In my Javascript, since I've already printed out all of the checkboxes unchecked in the HTML, all I had to do was specify which checkbox I want checked. It would be very easy to use a PHP forloop to generate the "document.myform.WHATEVER.checked = true;" over the array that you unserialized, which contained the names of the boxes that were checked. Hope that make sense. Quote Link to comment https://forums.phpfreaks.com/topic/127223-solved-array-help/#findComment-658059 Share on other sites More sharing options...
dennismonsewicz Posted October 6, 2008 Author Share Posted October 6, 2008 ok its really early for me lol... too many late nights burning the midnight oil! Gotta love pizza and Mt. Dew! What I am trying to do is this: I am printing out the checkboxes. And it checks the DB for a value of 1. If it is 1 then it checks the box. If it returns a 0 then it leaves the box unchecked. The reasoning behind this, is that I have built a custom CMS that allows project managers to go in an add/delete/edit projects and the checkboxes allows for the PM to see where a project is in the process. What happens is, is that a box is checked at a certain point in the project but if you go in and try to edit the checkbox and say, the PM made a mistake and the project isn't that far in the process and needs to try to backup and uncheck a box it is not updating the DB to set its checked value to 0. Make sense? I need it when a user unchecks the box to update the value in the DB with a 0 if it was a 1. Quote Link to comment https://forums.phpfreaks.com/topic/127223-solved-array-help/#findComment-658073 Share on other sites More sharing options...
ibechane Posted October 6, 2008 Share Posted October 6, 2008 If you want to construct an array, you can use a while loop and to cycle through your mysql results and assign values to an array. Quote Link to comment https://forums.phpfreaks.com/topic/127223-solved-array-help/#findComment-658079 Share on other sites More sharing options...
dennismonsewicz Posted October 6, 2008 Author Share Posted October 6, 2008 can you give me an example? Quote Link to comment https://forums.phpfreaks.com/topic/127223-solved-array-help/#findComment-658080 Share on other sites More sharing options...
ibechane Posted October 6, 2008 Share Posted October 6, 2008 what I mean is something like $myarray = array(); $i = 0; while ($newrow = mysql_fetch_row($chkbox_query)) { $myarray[$i][1] = $newrow[1]; $myarray[$i][2] = $newrow[2]; $i++; } Then, you'll have an array with everything you need, so it might be easier to work with the html outputting. You can use a for loop or foreach loop to get the items back out. Quote Link to comment https://forums.phpfreaks.com/topic/127223-solved-array-help/#findComment-658108 Share on other sites More sharing options...
sasa Posted October 6, 2008 Share Posted October 6, 2008 change $title .= '<div><input type="checkbox" name="checkboxes[' . $names . ']" class="checkbox" id="' . $names . '" ' . $chk . ' value="1" /> <label for="checkboxes[' . $names . ']">' . ucwords(str_replace($numbers, '', $names)) . '</label></div>' to $title .= '<div><input type="hidden" name="checkboxes[' . $names . ']" value="0"><input type="checkbox" name="checkboxes[' . $names . ']" class="checkbox" id="' . $names . '" ' . $chk . ' value="1" /> <label for="checkboxes[' . $names . ']">' . ucwords(str_replace($numbers, '', $names)) . '</label></div>' and tray to print_r($_POST['checkboxes']); Quote Link to comment https://forums.phpfreaks.com/topic/127223-solved-array-help/#findComment-658128 Share on other sites More sharing options...
dennismonsewicz Posted October 6, 2008 Author Share Posted October 6, 2008 but if i change the value to hidden the checkboxes will not be displayed on the screen Quote Link to comment https://forums.phpfreaks.com/topic/127223-solved-array-help/#findComment-658139 Share on other sites More sharing options...
kenrbnsn Posted October 6, 2008 Share Posted October 6, 2008 The hidden values are just there to initialize the returned values, if a checkbox isn't checked. All the checkboxes will appear on the screen. Ken Quote Link to comment https://forums.phpfreaks.com/topic/127223-solved-array-help/#findComment-658147 Share on other sites More sharing options...
dennismonsewicz Posted October 6, 2008 Author Share Posted October 6, 2008 HOLY CRAP!!!! It works! Man HUGE KUDOS! I have been racking my brain for 6 days on this... thanks! Quote Link to comment https://forums.phpfreaks.com/topic/127223-solved-array-help/#findComment-658154 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.