请帮我解决这个问题。 我试图根据数据库中正在运行的信息填充复选框,但问题是由于某些未知原因重复复选框。
<li class="treeview">
<a href="#"><i class="fa fa-tachometer"></i> <span>Dashboard</span></a>
<ul class="treeview-menu">
<li style="padding-left: 5px;">
<a href="#"><i class="active fa fa-search"></i> <span>Searches</span> <i class="fa fa-angle-left pull-right"></i></a>
<ul class="treeview-menu">
<div id="checkbox-container">
<li style="padding-left: 10px;">
<input type="checkbox" id="selectAll" onclick='selectAll(this.checked)'/>
<span style="color: #fff;">SELECT ALL</span>
</li>
<?php foreach ($query as $search_row) { ?>
<?php foreach($checkSearch as $s) { ?>
<?php
$box = 0;
if($search_row->searchid == $s->idsearch){
$box = 1;
}
?>
<form method="post" id="the-dash">
<li style="padding-left: 10px;">
<input type="checkbox" id="searches" class="searchid" onclick="getSearchId()" value="<?php echo $search_row->searchid; ?>" name="searchName" <?php echo ($box == 1 ? 'checked' : ''); ?>>
<span id="searchname" style="color: #fff; "><?php echo $search_row->searchname; ?> <?php echo '<br>'; ?><span>
</li>
</form>
<?php } ?>
<?php } ?>
</div>
</ul>
</li>
</ul>
</li>
答案 0 :(得分:0)
试试这个
在您的控制器中,将ID存储在数组中
$idsearchs = array();
foreach($checkSearch as $s) {
$idsearchs[] = $s->idsearch;
}
在您的视图中
<?php foreach ($query as $search_row) {
$box = in_array($search_row->searchid, $idsearchs) ? 1 : 0;
?>
<li style="padding-left: 10px;">
<form method="post" id="the-dash">
<input type="checkbox" id="searches" class="searchid" onclick="getSearchId()" value="<?php echo $search_row->searchid; ?>" name="searchName" <?php echo ($box == 1) ? 'checked="checked"' : ''; ?>>
<span id="searchname" style="color: #fff; "><?php echo $search_row->searchname; ?> <?php echo '<br>'; ?><span>
</form>
</li>
<?php } ?>
你已经使用$ query和foreach使用$ checkSearch进行了foreach,因为你得到了重复的复选框。