January 11, 201115 yr Hi, I have a set of checkboxes a bit like a checklist and I then want the items that aren't checked to displayed. On the first page, I have this code: <? while($row = mysql_fetch_array($result)) { ?> <form action="packed.php" method="post"> <div> <label><input type="checkbox" name="<? echo stripslashes($row['checklistitem']); ?>" value="yes"> <? echo stripslashes($row['checklistitem']); ?> </label> </div> This basically takes an array from a database. The info is then posted to the next page which hopefully will display the boxes which are 'unticked' or unchecked if you will. The code I have on the second page is: <?php $query_string = ""; if ($_POST) { $kv = array(); foreach ($_POST as $key => $value) { $kv[] = "$key"; } $query_string = join("<br />", $kv); } else { $query_string = $_SERVER['QUERY_STRING']; } echo $query_string; ?> At the moment, it returns the array items that were checked but I want the unchecked ones to be shown. Is that possible? Thanks very much! -Steve
January 11, 201115 yr Hi Steve, On the second page I would loop through the database results again and check to see which ones are not in the post array. That will then give you all of the un-ticked one. The code would be somethign similar to this <?php // Set a blank array to hold all of the unticked items $unticked = array(); // Assign the post values to a variable $ticked = $_POST; // Loop through the database results while($row = mysql_fetch_array($result)) { // Check if the current item is not in the ticked array if(!in_array($row['checklistitem'], $ticked)) { // The item does not appear in the ticked array so the user must have left it unticked // Add it to the unticked array $unticked[] = $row['checklistitem']; } } // Check if there are any unticked items and display them if so if(count($unticked) > 0) { // Display checkboxes } ?> Edited January 11, 201115 yr by vertmonkee
January 12, 201115 yr Author Thanks so much for replying vertmonkee! I just tried to run this script and added some basic echos to see if it's displaying the right output. My problems are: • Checking boxes on the first page doesn't seem to effect the code below. It always states 'There are boxes unchecked' even if all the checkboxes are ticked on the previous page? • I don't know how to show which check boxes haven't been checked? Please help if you can! Thankyou!! <?php // Set a blank array to hold all of the unticked items $unticked = array(); // Assign the post values to a variable $ticked = $_POST; // Loop through the database results while($row = mysql_fetch_array($result)) { // Check if the current item is not in the ticked array if(!in_array($row['checklistitem'], $ticked)) { // The item does not appear in the ticked array so the user must have left it unticked // Add it to the unticked array $unticked[] = $row['checklistitem']; } } // Check if there are any unticked items and display them if so if(count($unticked) > 0) { echo "There are boxes unchecked!"; } else { echo "You've checked all the boxes!"; } ?>
January 12, 201115 yr I just put together a quick script to try and test this, let me know if you would like to see it. I just used a coded array rather than pulling it from a database. I think the problem occurs where we check if the value is in the array. All the values of the array are yes. What actuall needs to happen is to check the array key. I have just commented out the old if statement and put the new one in place. Let me know if this works. <?php // Set a blank array to hold all of the unticked items $unticked = array(); // Assign the post values to a variable $ticked = $_POST; // Loop through the database results while($row = mysql_fetch_array($result)) { // Check if the current item is not in the ticked array //if(!in_array($row['checklistitem'], $ticked)) { if(!array_key_exists($row['checklistitem'], $ticked)) { // The item does not appear in the ticked array so the user must have left it unticked // Add it to the unticked array $unticked[] = $row['checklistitem']; } } // Check if there are any unticked items and display them if so if(count($unticked) > 0) { echo "There are boxes unchecked!"; } else { echo "You've checked all the boxes!"; } ?> Edited January 12, 201115 yr by vertmonkee
January 12, 201115 yr Author Hmm.. Still getting the same thing How annoying! (not you my script! ) I was considering using the 'array_diff' but have yet to get that working.. Cheers for your help dude!
January 12, 201115 yr I've attached my test files with the hard coded array. checkboxes1.php is a form which loops through the array and then the form submits to checboxes2.php Hopefully that should give you a better idea of what I did. Let me know how that goes. checkboxes1.php checkboxes2.php
Create an account or sign in to comment