从PHP Codeigniter中的两个表中检索数据

时间:2017-09-23 05:26:19

标签: php codeigniter

我想从一个表中检索数据的数量,从另一个表中检索一些数据,我也想知道如何在一个foreach循环中显示这些数据。

  

模型

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>&nbsp;&nbsp;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 } ?>

1 个答案:

答案 0 :(得分:0)

根据您的问题将数据模型传递给控制器​​代码

型号:

 public function counter()
{
        $this->db->where('package_id', $id);
        $this->db->from('gt_package');
        $query = $this->db->get('gt_package');
        $cnt = $query->num_rows();
        return $cnt; 
}

控制器:

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 );
    if($res){
        $data['result'] = $res;
        $this->load->view('manage_accounts_view', $data);

    } else {

        echo "Fail";

    }
    print_r($data['result']);
}