Arvind Arvind - 1 month ago 8
MySQL Question

Retrive data from database and explode and based on that select checkbox

I am retrieving data from MySQL database and explode it, based on result i want to select multiple checkbox, I am pasting my PHP code and all the checkbox code as well as database so you can see which checkbox must be selected based on result.

<?php
$query25 = "SELECT * FROM es_availability_options WHERE es_id='4'";
$result25 = @mysql_query($query25);
echo mysql_error();
$row25=@mysql_fetch_array($result25);
$availability_options = $row25['availability_options'];
$availability_options = explode(', ', $availability_options);

?>

<div class="row">

<div class="col-md-4">

<p style="font-size:16px;"><input name="availability_options[]" type="checkbox" value="Available by appointment"> Available by appointment</p>
<p style="font-size:16px;"><input name="availability_options[]" type="checkbox" value="Pre bookings preferred but can be available at short notice"> Pre bookings preferred, but can be available at short notice</p>
<p style="font-size:16px;"><input name="availability_options[]" type="checkbox" value="Flexible hours by appointment"> Flexible hours by appointment</p>
<p style="font-size:16px;"><input name="availability_options[]" type="checkbox" value="Available for overseas travel"> Available for overseas travel</p>
</div>

<div class="col-md-4">
<p style="font-size:16px;"><input name="availability_options[]" type="checkbox" value="Available 7 days"> Available 7 days</p>
<p style="font-size:16px;"><input name="availability_options[]" type="checkbox" value="12 hours notice required"> 12 hours notice required</p>
<p style="font-size:16px;"><input name="availability_options[]" type="checkbox" value="Available outside my city only"> Available outside my city only</p>
</div>

<div class="col-md-4">
<p style="font-size:16px;"><input name="availability_options[]" type="checkbox" value="Available 24 hours"> Available 24 hours</p>
<p style="font-size:16px;"><input name="availability_options[]" type="checkbox" value="24 hours notice required"> 24 hours notice required</p>
<p style="font-size:16px;"><input name="availability_options[]" type="checkbox" value="Available on weekends only"> Available on weekends only</p>
</div>

</div><!-- end row -->


I have "Available 7 days, 12 hours notice required, Available outside my city only" these options in database column "availability_options".

Answer

you have almost done it. Just use in_array() with your form like this -

 <div class="row">

 <div class="col-md-4">

    <p style="font-size:16px;"><input name="availability_options[]" type="checkbox" value="Available by appointment"> Available by appointment</p>
    <p style="font-size:16px;"><input name="availability_options[]" type="checkbox" value="Pre bookings preferred but can be available at short notice"> Pre bookings preferred, but can be available at short notice</p>
    <p style="font-size:16px;"><input name="availability_options[]" type="checkbox" value="Flexible hours by appointment"> Flexible hours by appointment</p>
    <p style="font-size:16px;"><input name="availability_options[]" type="checkbox" value="Available for overseas travel"> Available for overseas travel</p>
</div>

<div class="col-md-4">
    <p style="font-size:16px;"><input name="availability_options[]" type="checkbox" value="Available 7 days" <?php if (in_array("Available 7 days", $availability_options)) {echo "checked"; }?>> Available 7 days</p>
    <p style="font-size:16px;"><input name="availability_options[]" type="checkbox" value="12 hours notice required" <?php if (in_array("12 hours notice required", $availability_options)) {echo "checked"; }?>> 12 hours notice required</p>
    <p style="font-size:16px;"><input name="availability_options[]" type="checkbox" value="Available outside my city only" <?php if (in_array("Available outside my city only", $availability_options)) {echo "checked"; }?>> Available outside my city only</p>
</div>

<div class="col-md-4">
    <p style="font-size:16px;"><input name="availability_options[]" type="checkbox" value="Available 24 hours"> Available 24 hours</p>
    <p style="font-size:16px;"><input name="availability_options[]" type="checkbox" value="24 hours notice required"> 24 hours notice required</p>
    <p style="font-size:16px;"><input name="availability_options[]" type="checkbox" value="Available on weekends only"> Available on weekends only</p>
</div>

Comments