我正在从 MySQL 数据库检索数据并分解它,根据结果我想选择多个复选框,我粘贴我的 PHP 代码和所有复选框代码以及数据库,以便您可以看到必须根据以下情况选择哪个复选框结果。
<?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 -->
我在数据库列“availability_options”中设置了“7 天可用,需要提前 12 小时通知,仅在我的城市之外可用”这些选项。
最佳答案
你几乎已经完成了。只需将 in_array() 与您的表单一起使用,如下所示 -
<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>
关于php - 从数据库检索数据并分解并基于该选择复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39968152/