如何读出嵌套foreach中的所有子类别
How to read out all subcategories in a nested foreach
我正在尝试添加嵌套的 foreach 循环,并希望在顶级猫的 table 行下显示与 to cat 相关的所有子猫,但不知道我该如何做要求
如果有人能提供帮助,非常感谢我搜索了很多但没有得到答案请查看我的代码并询问我该怎么做。
这是我的看法。
<div class="conatiner">
<br/>
<br/>
<button class="accordion">Top Category Name </button>
<div class="panel">
<p>Sub Catorgies list under this top category</p>
</div>
<br/>
</div>
<div class="pag">
<table class="table gap">
<thead>
<tr>
<th>Flag No.</th>
<th>Name</th>
<th>On/Off</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php if ($subcats): ?>
<?php foreach ($subcats as $key => $row): ?>
<tr >
<td><?php echo $row->flag_no?></td>
<td><?php echo $row->cat_name?></td>
<td>
<label class="switch">
<input type="checkbox">
<span class="slider round"></span>
</label>
</td>
<td class="action-cats">
<a style="margin-right: 30px; " href="<?= site_url('Products/delete_cat/' .$row->id);?>"><img src="<?php echo base_url('assets/images1/bin.png');?>" width="20px"> Delete </a>
<a href="<?= site_url('Products/edit_cat/' .$row->id);?>"><img src="<?php echo base_url('assets/images1/edit.png');?>" width="20px"> Edit </a>
</td>
<td>
<a href="#demo<?php echo $row->id?>" data-toggle="collapse">+</a>
</td>
</tr>
</tr>
<?php foreach ($subt as $row): ?>
<tr>
<?php if ($row->cat_id == $row->cat_id): ?>
<td id="demo<?php echo $row->id?>" class="collapse"> <?php echo $row->sub_cat?></td>
<td id="demo<?php echo $row->id?>" class="collapse"><a href="<?= site_url('Products/delete_sub_cat/' .$row->sub_id);?>"><img src="<?php echo base_url('assets/images1/bin.png');?>" width="20px"></a>
<a href="<?= site_url('Products/edit_sub_cat/' .$row->sub_id);?>"><img src="<?php echo base_url('assets/images1/edit.png');?>" width="20px"></a></td>
<?php endif ?>
</tr>
<?php continue;?>
<?php endforeach ?>
<?php endforeach ?>
<?php endif;?>
</tbody>
</table>
</div>
</div>
这是我的控制器。
public function categories()
{
$data['subcats']=$this->Pro_model->fetch_categories();
$data['subt']=$this->Pro_model->fetch_sub_categories();
$data['cat']=$this->Pro_model->fetch_cat();
$this->load->view('admin-new/categories.php', $data);
}
这是我的模型。
function fetch_categories(){
$this->db->select('*');
$this->db->from('subcat');
$this->db->join('categories', 'subcat.sub_id = categories.id ');
$this->db->group_by('subcat.sub_id');
$this->db->order_by('sflag_no', 'asc');
$query = $this->db->get();
return $query->result();
}
function fetch_sub_categories(){
$this->db->select('*');
$this->db->from('subcat');
$this->db->join('categories', 'subcat.cat_id = categories.id ');
$this->db->group_by('subcat.sub_id');
$this->db->order_by('sflag_no', 'asc');
$query = $this->db->get();
return $query->result();
}
您需要 table 结构如下:
<table border="2" bordercolor="green">
<tr>
<td>main cat 1
<table border="2" bordercolor="blue" id="cat-id-1">
<tr class="subRow">
<td>subcat 1 for table 1</td>
</tr>
<tr class="subRow">
<td>subcat 2 for table 1</td>
</tr>
<tr class="subRow">
<td>subcat 3 for table 1</td>
</tr>
</table>
</td>
<td><a herf="#" class="toggle" data-cat-id="1">+</a></td>
</tr>
<tr>
<td>main cat 2
<table border="2" bordercolor="blue" id="cat-id-2">
<tr class="subRow">
<td>subcat 1 for table 2</td>
</tr>
<tr class="subRow">
<td>subcat 2 for table 1</td>
</tr>
</table>
</td>
<td><a herf="#" class="toggle" data-cat-id="2">+</a></td>
</tr>
</table>
然后您可以使用 jQuery:
切换嵌套的 table
<script type="text/javascript">
$(document).ready(function() {
$('.toggle').on('click', function() {
// Get the id of the table that is going to toggle
catId = $(this).data('cat-id');
// Select the sub-table to toggle
table = $('#cat-id-'+catId);
table.toggle();
});
})
</script>
注意:您需要将嵌套的 table 的 id 设置为 cat-id-{ID OF MAIN CAT}
,将切换按钮的 data-cat-id
属性设置为 {ID OF MAIN CAT}
。
这是工作 fiddle:https://jsfiddle.net/5vp9a24r/2/
至于数据部分,不确定您的数据库结构,但我认为您应该首先获取主要类别并在其上加入子类别。像这样:
function getCategories() {
$this->db->select('*');
$this->db->from('categories');
$this->db->join('subcat', 'subcat.cat_id = categories.id ');
$this->db->group_by('categories.id');
$query = $this->db->get();
return $query->result();
}
在你的模型中使用这个方法,你应该得到也有各自子类别的类别。
更新:
嵌入 foreach 的示例。
<table>
<?php foreach ($categories as $categoryItem) : ?>
<tr>
<td>main cat 1
<table id="cat-id-<?= $categoryItem->id; ?>">
<?php foreach ($categoryItem->subCats as $subcatItem) : ?>
<tr class="subRow">
<td><?= $subcatItem->title ?></td>
</tr>
<?php endforeach; ?>
</table>
</td>
<td><a herf="#" class="toggle" data-cat-id="<?= $categoryItem->id ?>">+</a></td>
</tr>
<?php endforeach; ?>
我正在尝试添加嵌套的 foreach 循环,并希望在顶级猫的 table 行下显示与 to cat 相关的所有子猫,但不知道我该如何做要求
如果有人能提供帮助,非常感谢我搜索了很多但没有得到答案请查看我的代码并询问我该怎么做。
这是我的看法。
<div class="conatiner">
<br/>
<br/>
<button class="accordion">Top Category Name </button>
<div class="panel">
<p>Sub Catorgies list under this top category</p>
</div>
<br/>
</div>
<div class="pag">
<table class="table gap">
<thead>
<tr>
<th>Flag No.</th>
<th>Name</th>
<th>On/Off</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php if ($subcats): ?>
<?php foreach ($subcats as $key => $row): ?>
<tr >
<td><?php echo $row->flag_no?></td>
<td><?php echo $row->cat_name?></td>
<td>
<label class="switch">
<input type="checkbox">
<span class="slider round"></span>
</label>
</td>
<td class="action-cats">
<a style="margin-right: 30px; " href="<?= site_url('Products/delete_cat/' .$row->id);?>"><img src="<?php echo base_url('assets/images1/bin.png');?>" width="20px"> Delete </a>
<a href="<?= site_url('Products/edit_cat/' .$row->id);?>"><img src="<?php echo base_url('assets/images1/edit.png');?>" width="20px"> Edit </a>
</td>
<td>
<a href="#demo<?php echo $row->id?>" data-toggle="collapse">+</a>
</td>
</tr>
</tr>
<?php foreach ($subt as $row): ?>
<tr>
<?php if ($row->cat_id == $row->cat_id): ?>
<td id="demo<?php echo $row->id?>" class="collapse"> <?php echo $row->sub_cat?></td>
<td id="demo<?php echo $row->id?>" class="collapse"><a href="<?= site_url('Products/delete_sub_cat/' .$row->sub_id);?>"><img src="<?php echo base_url('assets/images1/bin.png');?>" width="20px"></a>
<a href="<?= site_url('Products/edit_sub_cat/' .$row->sub_id);?>"><img src="<?php echo base_url('assets/images1/edit.png');?>" width="20px"></a></td>
<?php endif ?>
</tr>
<?php continue;?>
<?php endforeach ?>
<?php endforeach ?>
<?php endif;?>
</tbody>
</table>
</div>
</div>
这是我的控制器。
public function categories()
{
$data['subcats']=$this->Pro_model->fetch_categories();
$data['subt']=$this->Pro_model->fetch_sub_categories();
$data['cat']=$this->Pro_model->fetch_cat();
$this->load->view('admin-new/categories.php', $data);
}
这是我的模型。
function fetch_categories(){
$this->db->select('*');
$this->db->from('subcat');
$this->db->join('categories', 'subcat.sub_id = categories.id ');
$this->db->group_by('subcat.sub_id');
$this->db->order_by('sflag_no', 'asc');
$query = $this->db->get();
return $query->result();
}
function fetch_sub_categories(){
$this->db->select('*');
$this->db->from('subcat');
$this->db->join('categories', 'subcat.cat_id = categories.id ');
$this->db->group_by('subcat.sub_id');
$this->db->order_by('sflag_no', 'asc');
$query = $this->db->get();
return $query->result();
}
您需要 table 结构如下:
<table border="2" bordercolor="green">
<tr>
<td>main cat 1
<table border="2" bordercolor="blue" id="cat-id-1">
<tr class="subRow">
<td>subcat 1 for table 1</td>
</tr>
<tr class="subRow">
<td>subcat 2 for table 1</td>
</tr>
<tr class="subRow">
<td>subcat 3 for table 1</td>
</tr>
</table>
</td>
<td><a herf="#" class="toggle" data-cat-id="1">+</a></td>
</tr>
<tr>
<td>main cat 2
<table border="2" bordercolor="blue" id="cat-id-2">
<tr class="subRow">
<td>subcat 1 for table 2</td>
</tr>
<tr class="subRow">
<td>subcat 2 for table 1</td>
</tr>
</table>
</td>
<td><a herf="#" class="toggle" data-cat-id="2">+</a></td>
</tr>
</table>
然后您可以使用 jQuery:
切换嵌套的 table<script type="text/javascript">
$(document).ready(function() {
$('.toggle').on('click', function() {
// Get the id of the table that is going to toggle
catId = $(this).data('cat-id');
// Select the sub-table to toggle
table = $('#cat-id-'+catId);
table.toggle();
});
})
</script>
注意:您需要将嵌套的 table 的 id 设置为 cat-id-{ID OF MAIN CAT}
,将切换按钮的 data-cat-id
属性设置为 {ID OF MAIN CAT}
。
这是工作 fiddle:https://jsfiddle.net/5vp9a24r/2/
至于数据部分,不确定您的数据库结构,但我认为您应该首先获取主要类别并在其上加入子类别。像这样:
function getCategories() {
$this->db->select('*');
$this->db->from('categories');
$this->db->join('subcat', 'subcat.cat_id = categories.id ');
$this->db->group_by('categories.id');
$query = $this->db->get();
return $query->result();
}
在你的模型中使用这个方法,你应该得到也有各自子类别的类别。
更新: 嵌入 foreach 的示例。
<table>
<?php foreach ($categories as $categoryItem) : ?>
<tr>
<td>main cat 1
<table id="cat-id-<?= $categoryItem->id; ?>">
<?php foreach ($categoryItem->subCats as $subcatItem) : ?>
<tr class="subRow">
<td><?= $subcatItem->title ?></td>
</tr>
<?php endforeach; ?>
</table>
</td>
<td><a herf="#" class="toggle" data-cat-id="<?= $categoryItem->id ?>">+</a></td>
</tr>
<?php endforeach; ?>