upperbid Posted June 29, 2009 Share Posted June 29, 2009 I am having a difficult time repopulating user checkbox selections from a mysql database. It is currently stored with selections as numbers in one field called PaymentMethods in the specified table. I have figured out how to successfully explode it and get the results that are there (it only stores the numbers of those selected and ignores blanks ones when writing to the database). My problem is I'm trying to have all the checkboxes appear with the user database results resulting in the proper checkboxes being checked. So far I have this: $pmethods = explode("¦¦", $r2["PaymentMethods"]); $choices = array('1'=>"Cashiers Check",'2' => "Money Order",'3'=>"Personal Check",'4'=>"Credit Card",'8'=>"PayPal"); foreach ($choices as $key6 => $value6) { $checked = ($pmethods == $key6) ? ' checked="checked"' : ''; echo "<input type=\"checkbox\" name=\"methods[]\" value=\"$key6\" $checked />$value6"; echo "</br>"; } This only shows the checkboxes and none of them checked. My other attempt has been: foreach ($pmethods as $key => $value2) { if ($value2=="10"){ print "<input type=\"checkbox\" name=\"methods[]\" value=\"Cash\" checked=\"checked\" />Cash"; } if (($value2=="1") OR ($value2=="3")){ print "<input type=\"checkbox\" name=\"methods[]\" value=\"Check\" checked=\"checked\" />Check"; } if ($value2=="2"){ print "<input type=\"checkbox\" name=\"methods[]\" value=\"Money Order\" checked=\"checked\" />Money Order"; } if (($value2=="4") OR ($value2=="5") OR ($value2=="6") OR ($value2=="7")){ print "<input type=\"checkbox\" name=\"methods[]\" value=\"Credit Card\" checked=\"checked\" />Credit Card"; } if ($value2=="8"){ print "<input type=\"checkbox\" name=\"methods[]\" value=\"Paypal\" checked=\"checked\" />PayPal"; } } The problem with this method is that it only shows the checked boxes and not the unchecked ones which need to be shown as well. Any help would be appreciated. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/164048-solved-repopulating-checkboxes-from-mysql-database/ Share on other sites More sharing options...
haku Posted June 29, 2009 Share Posted June 29, 2009 Can you repost that and use code tags around your code? It makes it much easier for us to read. You can get code tags by using the # button above the text box you type your post into. Quote Link to comment https://forums.phpfreaks.com/topic/164048-solved-repopulating-checkboxes-from-mysql-database/#findComment-865381 Share on other sites More sharing options...
cunoodle2 Posted June 29, 2009 Share Posted June 29, 2009 Is "PaymentMethods" of type int?? If so get rid of all those double quotes in the if statements. Change if ($value2=="2") to if ($value2==2) Quote Link to comment https://forums.phpfreaks.com/topic/164048-solved-repopulating-checkboxes-from-mysql-database/#findComment-865428 Share on other sites More sharing options...
sasa Posted June 29, 2009 Share Posted June 29, 2009 $pmethods = explode("¦¦", $r2["PaymentMethods"]); $choices = array('1'=>"Cashiers Check",'2' => "Money Order",'3'=>"Personal Check",'4'=>"Credit Card",'8'=>"PayPal"); foreach ($choices as $key6 => $value6) { $checked = in_array($key6, $pmethods) ? ' checked="checked"' : ''; echo "<input type=\"checkbox\" name=\"methods[]\" value=\"$key6\" $checked />$value6"; echo "</br>"; } Quote Link to comment https://forums.phpfreaks.com/topic/164048-solved-repopulating-checkboxes-from-mysql-database/#findComment-865447 Share on other sites More sharing options...
upperbid Posted June 29, 2009 Author Share Posted June 29, 2009 Thanks Sasa, that worked great. There is only one other related issue. The original implode that saves to the database has 15 choices entered by numbers 1-15 while the exploded data needs to be populated in only 5 choices (some numbers are now ignored and not relevant, others combined. For example, Cashier's Check is #1 and Personal Check is #3 (I want to show a new box as "checks" checked if either one or both of these numbers are returned). The same with VISA, Master Card, AE, etc., which any or all returned should result in one checkbox called "Credit Card" being checked. My confusion is how to modify the above code to instruct it to do this with the numbers I choose going to the array check box I want them to so that only one checked box appears even when there are two or more numbers applied. Quote Link to comment https://forums.phpfreaks.com/topic/164048-solved-repopulating-checkboxes-from-mysql-database/#findComment-865472 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.