In this post we will see how to check values from the database and check the checkboxes if the value is matched.
<?php
$str1 = 'one,two,three,four,five'; // this will be our database field. eg. $product['accessories']
$str = explode(',',$str1); // this will be our exploded string
echo '<pre>';
print_r($str);
echo '</pre>';
foreach($str as $s)
{
if($s=='two') // check for first string
{
echo "<input type='checkbox' id='chk' name='chk' checked='checked' />".$s;
}
elseif($s=='four') // check for second string and so on
{
echo "<input type='checkbox' id='chk' name='chk' checked='checked' />".$s;
}
else // else is compulsory. it will be an unchecked checkbox
{
echo "<input type='checkbox' id='chk' name='chk' />".$s;
}
}
?>
this will return 5 checkboxes with value one, two, three,... and with 2 checkboxes checked viz., two and four. Others will be unchecked.
<?php
$str1 = 'one,two,three,four,five'; // this will be our database field. eg. $product['accessories']
$str = explode(',',$str1); // this will be our exploded string
echo '<pre>';
print_r($str);
echo '</pre>';
foreach($str as $s)
{
if($s=='two') // check for first string
{
echo "<input type='checkbox' id='chk' name='chk' checked='checked' />".$s;
}
elseif($s=='four') // check for second string and so on
{
echo "<input type='checkbox' id='chk' name='chk' checked='checked' />".$s;
}
else // else is compulsory. it will be an unchecked checkbox
{
echo "<input type='checkbox' id='chk' name='chk' />".$s;
}
}
?>
this will return 5 checkboxes with value one, two, three,... and with 2 checkboxes checked viz., two and four. Others will be unchecked.
Comments
Post a Comment
Please feel free to comment. I would love to hear feedback.