0
我想檢索一個表中的數據和另一個表中的一些數據的數量,我也想知道如何在單個foreach循環中顯示這些數據。在PHP中檢索兩個表中的數據Codeigniter
型號
public function counter(){
$this->db->where('package_id', $id);
$this->db->from('gt_package');
$cnt = $this->db->count_all_results();
}
控制器
public function package_category_listing()
{
$data['records'] = $this->gt_package_model->counter();
$query = $this->db->get("gt_package_category");
$data['records'] = $query->result();
$this->load->view('admin/package/package_category_listing',$data);
}
查看部分:
<?php foreach ($records as $r) { ?>
<div class="col-sm-6 col-md-3 col-xs-12">
<div class="bg-purple">
<h4 class="text-white">
<?php echo $r->category_name; ?>
</h4>
<span>200</span> Packages
<br>
<a style="margin-top: 10px;" href="<?php echo site_url() . "/gt_package/listing/" . $r->category_id ?>" class="btn btn-default">View Packages</a>
</div>
</div>
<?php } ?>
變化的代碼'$ CNT = $這 - > db-> count_all_results();'to'$ query = $ this-> db-> get('gt_package'); $ cnt = $ query-> num_rows();' –
如何在視圖中調用 –
連接表'gt_package'和表'gt_package_category'的任何外鍵。如果是的話加入這兩張表 –