Jump to content

[SOLVED] Repopulating checkboxes from mysql database


upperbid

Recommended Posts

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.

Link to comment
Share on other sites

$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>";
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.