我是php编程的新手,我不知道这段代码是否正确。我在那里找一些并写下这段代码。在这里我使用爆炸功能,其结果返回两次。
<?php
$SlNo = 1;
foreach ($record as $r2) { // this is for fetching all form first table
?>
<?php
foreach ($selectdata as $r) { // this is to fetch imploded record from second table
$test = $r->package_id;
}
$explode = explode(',', $test);
foreach ($explode as $r1) {
// echo $r1. "<br/>";
if ($r2->package_name == $r1) {
?>
<div class="col-sm-4">
<div class="checkbox">
<input id="checkbox<?php echo $SlNo++ ; ?>" type="checkbox"
name="package[]" value="<?php echo $r1; ?>" checked="checked"> <label
for="checkbox<?php echo $SlNo++ ; ?>"> <?php echo $r1; ?></label>
</div>
</div>
<?php
} else {
?>
<div class="col-sm-4">
<div class="checkbox">
<input id="checkbox<?php echo $SlNo++ ; ?>" type="checkbox"
name="package[]" value="<?php echo $r2->package_name; ?>"> <label
for="checkbox<?php echo $SlNo++ ; ?>"> <?php echo $r2->package_name; ?></label>
</div>
</div>
<?php
}
}
}
?>
答案 0 :(得分:1)
根据上述信息,你说$selectdata
作为包价值。在for循环之前爆炸它。
<?php
foreach($selectdata as $r)
{
$t= $r->package_id;
}
$explode = explode(',',$t);
$SlNo=1;
foreach ($record as $r2)
{ // this is for fetching all form first table
//echo $r1. "<br/>";
if(in_array($r2->package_name, $explode))
{
?>
<div class="col-sm-4">
<div class="checkbox">
<input id="checkbox<?php echo $SlNo++ ; ?>" type="checkbox" name="package[]" value="<?php foreach ($explode as $e){ echo $e; }?>" checked="checked">
<label for="checkbox<?php echo $SlNo++ ; ?>"> <?php echo $r2->package_name; ?></label>
</div>
</div>
<?php
}
else
{
?>
<div class="col-sm-4">
<div class="checkbox">
<input id="checkbox<?php echo $SlNo++ ; ?>" type="checkbox" name="package[]" value="<?php echo $r2->package_name; ?>">
<label for="checkbox<?php echo $SlNo++ ; ?>"> <?php echo $r2->package_name; ?></label>
</div>
</div>
<?php
}
}
?>