我正在嘗試添加嵌套的 foreach 回圈,并希望在頂部貓的表格行下顯示與 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();
}
uj5u.com熱心網友回復:
你需要有這樣的表結構:
<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 切換嵌套表:
<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>
注意:您需要將嵌套表的 id 設定為cat-id-{ID OF MAIN CAT},data-cat-id將切換按鈕的屬性設定為{ID OF MAIN CAT}.
這是作業小提琴: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; ?>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/431249.html
