从具有2个型号的控制器功能查看数据

时间:2013-07-22 09:32:42

标签: php codeigniter

我试图通过控制器和型号从DB获取Banner图像。

我的控制器:

class Home extends CI_Controller {
    //put your code here
    public function __construct() {
        parent::__construct();
        $this->load->model('home_model');
    }

    public function index() {
        $data = array();

        $data['bannerInfo'] = $this->home_model->selectBanner($banner_id);
        $data['result'] = $this->home_model->selectCategory($category_id);
        $data['banner'] = $this->load->view('banner',$data, TRUE);
        $data['maincontent'] = $this->load->view('home_message',$data,TRUE);
        $data['title'] = 'NZ Furniture Products ';
        $data['keywords'] = 'furniture bangladesh';

        $this->load->view('home', $data);
   }

我的模特课:

class Home_Model extends CI_Model {
    // put your code here
    public function selectCategory($category_id)
    {
        $this->db->select('*');
        $this->db->from('tbl_category');
        $this->db->order_by("category_id", "desc"); 
        $query_result=  $this->db->get();
        $results = $query_result->result();

        return $results;
    } 

    public function selectBanner($banner_id)
    {
        $this->db->select('*');
        $this->db->from('tbl_banner');
        $this->db->where('banner_id',$banner_id);
        //$this->db->order_by("product_id", "desc"); 
        $query_result = $this->db->get();
        $results = $query_result->result();

        return $results;
    } 
$

查看:

home_message :::

<?php foreach ($result as $values)  { ?>
<div class="single_product">
<div class="product_image">
        <a href="<?php echo base_url(); ?>home/category/<?php echo   $values->category_id ?>"><img src="<?php echo base_url();?><?php echo $values->category_image ?>" /></a> 
    </div>
    <span class="category_title"> <a href="<?php echo base_url(); ?>home/category/<?php echo $values->category_id ?>"> <?php echo $values->category_name ?></a> </span>
 </div>

<?php } ?>

横幅

<div>
<?php foreach ($bannerInfo as $values) {   ?>
<?php echo $values->banner_image ?>
<?php } ?>
</div>

1 个答案:

答案 0 :(得分:0)

如果要显示所有横幅广告。你必须删除where子句。默认排序是desc。

控制器文件:

class Home extends CI_Controller 
{
  public function __construct() {
    parent :: __construct();
    $this->load->model('home_model');
  }

  public function index() {
    $data = array();
    $data['bannerInfo'] = $this->home_model->selectBanner();
    $data['result'] = $this->home_model->selectCategory($category_id);
    $data['banner'] = $this->load->view('banner', $data, TRUE);
    $data['maincontent'] = $this->load->view('home_message', $data, TRUE);
    $data['title'] = 'NZ Furniture Products ';
    $data['keywords'] = 'furniture bangladesh';
    $this->load->view('home', $data);
  }
}

模型文件:

class Home_Model extends CI_Model
{
  public function selectCategory($category_id)
  {
    $this->db->select('*');
    $this->db->from('tbl_category');
    $this->db->order_by("category_id", "desc"); 
    $query_result = $this->db->get();
    $results = $query_result->result();
    return $results;
  } 

  public function selectBanner($order_by = "desc")
  {
    $this->db->select('*');
    $this->db->from('tbl_banner'); 
    $this->db->order_by("banner_id", $order_by);
    return $this->db->get()->result();
  }
}