2016-07-11 10 views
0

where句のあるページに商品を表示しています。カテゴリが野菜である製品表からすべてを選択していますが、機能していません。私はCodeIgniterの新機能です。私もインターネット上で多くの質問をチェックしましたが、何も動いていません。このコードに何か間違っているものがある場合は、教えてください。where節がcodigniterで機能しない

my table

コントローラ部:

<?php 
class products_list extends CI_Controller 
{ 
function __construct() { 
     parent::__construct(); 
     $this->load->helper('url');  //to load css base url 
     $this->load->helper('form'); 
     $this->load->library('form_validation'); 
     $this->load->model("pagination_model"); 
     $this->load->library("pagination"); 
     $this->load->library('table'); 
} 


function vegetables(){ 

    $this ->load->view('includes/header1'); 

    $config['base_url']='http://localhost/mah/index.php/products_list/vegetables'; 

    $vegetable="Vegetable"; 
    $config['total_rows']= $this->db->get_where('product', $vegetable)->num_rows(); 
    $config['per_page'] = 12; 
    $config['num_links'] = 20; 
    $config['full_tag_open']='<div id="pagination">'; 
    $config['full_tag_close']='</div>'; 

    $this->pagination->initialize($config); 
    $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0; 
    $data["results"] = $this->pagination_model-> 
      fetch_product($config["per_page"], $page); 
    $data["links"] = $this->pagination->create_links(); 
    $this->load->view("product_display", $data); 

    $this ->load->view('includes/footer'); 

} 

public function article($id) 
{ 
    $this ->load->view('includes/header'); 
    $this->load->model('articles_model', 'product'); 
    $article = $this->product->find($id); 
    $this->load->view('product_details', compact('article')); 
} 

} 
?> 

モデルの一部:

<?php 
class pagination_model extends CI_Model 
{ 
    public function __construct() { 
     parent::__construct(); 
    } 

    public function record_count() { 
     $where="category='vegetable'"; 
     return $this->db->count_all("product"); 
    } 

    public function fetch_product($limit, $start) { 
     $this->db->limit($limit, $start); 

     $query = $this->db->get("product"); 

     if ($query->num_rows() > 0) { 
      foreach ($query->result() as $row) { 
       $data[] = $row; 
      } 
      return $data; 
     } 
     return false; 
    } 
} 
?> 

答えて

0

コントローラ方法:あなたの要件とBASE_URLごとなどにも

public function vegetables() { 
    $data = array('title' => 'Products'); 
    $config = array(); 
    $config['base_url'] = base_url().'products_list/vegetables'; 
    $config['total_rows'] = $this->pagination_model->countRows('products'); 
    $config['per_page'] = 5; 

    $config['attributes'] = array('class' =>'item'); 

    $config['first_link'] = 'First'; 
    $config['cur_tag_open'] = '<a class="active item">'; 
    $config['cur_tag_close'] = '</a>'; 

    $config['next_link'] = 'Next'; 
    $config['last_link'] = 'Last'; 
    $config['prev_link'] = 'Previous'; 


    $this->pagination->initialize($config); 

    $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0; 
    $data["products"] = $this->pagination_model->getProducts('',$config["per_page"],$page); 
    $data["categoriesData"] = $this->pagination_model->getProducts('vegetable',$config["per_page"],$page); 
    $data["links"] = $this->pagination->create_links(); 
    print_r($data);die; 
    //$this->load->view('templates/header',$data); 
    $this->load->view('product_display',$data); 
    //$this->load->view('templates/footer');  
} 

あなたのリンクcutomise

あなたのモデル:

getProductsは、データの両方のタイプをフェッチ

1:もしuがc最初のパラメータとしてategoryを返し、カテゴリデータを返します。

2:それ以外の場合はテーブル全体を取得します。

public function getProducts($category = NULL, $limit = NULL, $start = NULL) 
    { 
    $limit = $limit == NULL ? '' : $limit; 
    $start = $start == NULL ? '' : $start; 
    $this->db->order_by('id', 'DESC'); 
    $this->db->limit($limit, $start); 
    if (empty($category)) 
    { 
     $query = $this->db->get('products'); 
    } 
    else 
    { 
     $query = $this->db->get_where('products', array('category' => $category)); 
    } 


    if ($query->num_rows() > 0) 
    { 
     foreach ($query->result_array() as $row) 
     { 
      $data[] = $row; 
     } 
     return $data; 
    } 
} 
// total count method 

public function countRows($table) { 
    if (empty($table)) return false; 
    return $this->db->count_all($table); 
} 

少なくとも最後のではなく:

は、このコントローラ__contructで()メソッド

public function __construct() 
{ 
    parent::__construct(); 
    $this->load->helper(array('url','text','html','form')); 
    $this->load->library(array('session','form_validation','pagination')); 
    $this->load->database(); 
    $this->load->model('YourModel'); 
} 
0

が...これを試してください

$config['total_rows']= $this->db->get_where('product', array('title' => $vegetable))->num_rows(); 
+0

を追加し、私はあなたの代わりにtitle'、 'の' category'列を使用する必要があると思います。 –

関連する問題