php - where clause not working in codigniter -
i showing products in page clause. selecting product table category vegetable, it's not working. new in codeigniter. checked many questions on internet, nothing working. if there in code wrong, please tell me.
the controller part:
<?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')); } } ?>
the model part:
<?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() $row) { $data[] = $row; } return $data; } return false; } } ?>
controller method :
cutomise links per requirements , base_url well
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'); }
your model :
getproducts fetch both type of data
1: if u provide category first param return categories data
2: otherwise fetch whole table;
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() $row) { $data[] = $row; } return $data; } } // total count method public function countrows($table) { if (empty($table)) return false; return $this->db->count_all($table); }
last not least :
add in controller __contruct() method
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'); }
Comments
Post a Comment