I'm new at CodeIgniter, but I want to ask about pagination and filtering.
I did them and they works great aside from each other, but I have two pages with filters and pagination too. They gives me some errors.
-When I click Filter, the url is like this: company/components/all/names_of_filters
so the filters works at uri segment(4).
=========================================================================But when I load pagination it shows me
company/components/all/pagination so the filters crashes with pagination. I declared uri segment(5) for loaded the pagination but the same thing. The idea is that the filter is changing from database and it is an dynamic part. How can I declare it on
$conf[base_url]=(company/components/all/...../) and put in those dashes an function that is concatenate an function that changes names filter?! ---------------------------------------------------------------------------------------------------------
*Has any method or not? *
Best :)
Put this file
myhelper_helper.php in helpers folder
function configPagination($url,$total_rows,$segment,$per_page=10)
{
$CI = get_instance();
$CI->load->library('pagination');
$config['base_url'] = site_url($url);
$config['total_rows'] = $total_rows;
$config['per_page'] = $per_page;
$config['uri_segment'] = $segment;
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="active"><a href="#">';
$config['cur_tag_close'] = '</a></li>';
$config['num_links'] = 3;
$config['next_tag_open'] = "<li>";
$config['next_tag_close'] = "</li>";
$config['prev_tag_open'] = "<li>";
$config['prev_tag_close'] = "</li>";
$config['first_link'] = FALSE;
$config['last_link'] = FALSE;
$CI->pagination->initialize($config);
return $CI->pagination->create_links();
}
Example of pagination using session library...
Controller:
Users.php
public function __construct()
{
parent::__construct();
$this->per_page = 20; // How many records per page...
$this->load->helper('myhelper');
$this->load->library('session');
$this->load->model('users_model');
}
function list($start=0)
{
if(isset($_POST['filter_status']))
{
$this->session->set_userdata('filter_status',$this->input->post('filter_status'));
}
$data['users'] = $this->users_model->get_all($start,$this->per_page);
$total = $this->users_model->count_all();
$data['pages'] = configPagination('users/list',$total,5,$this->per_page);
$data['start'] = $start;
$this->load->view('users_view',$data);
}
And Finally in your model Users_model.php
function __construct()
{
parent::__construct();
$this->load->database();
}
function get_all($start,$limit) {
if($this->session->userdata('filter_status')!='')
{
$this->db->where('status',$this->session->userdata('filter_status'));
}
$query = $this->db->get('users', $limit, $start);
return $query;
function count_all()
{
if($this->session->userdata('filter_status')!='')
{
$this->db->where('status',$this->session->userdata('filter_status'));
}
$query = $this->db->get('users');
return $query->num_rows();
}
In your view, something like this
<form action="" method="post" id="filter_form">
<div class="form-group">
<label>Status: </label>
<div class="controls">
<?php $status = array(0=>"Inactive", 1=>"Inactive",-1=>"Banned");?>
<select id="status-select" name="filter_status" class="form-contro">
<option value="">All</option>
<?php foreach ($status as $key=>$value) {
$sel = ($key==$this->session->userdata('filter_status'))?'selected="selected"':'';
?>
<option value="<?php echo $key;?>" <?php echo $sel;?>><?php echo $value ?></option>
<?php } ?>
</select>
<input type="submit" Value="Filter" />
</div></form>
Controller
$this->load->library('pagination');
$pagination_config['base_url'] = base_url('/webs/webs1/all/');
$pagination_config['total_rows'] = $this->model_m->count_all();
$pagination_config['per_page'] = 2;
$pagination_offset = $this->uri->segment(5);
$this->pagination->initialize($pagination_config);
$data['pagination_links'] = $this->pagination->create_links();
//end
//fetch categories from the database
$data['categories'] = $this->model_m->get_categories();
$categoryID = $this->uri->segment(4);
if($categoryID == null){
redirect('backend/subcategories/all/filterAll');
}
if(!isset($categoryID) || ( $categoryID == "filterAll" )) {
$data['subcategories'] = $this->model_m->get_all_subcategories();
} else {
$data['subcategories'] = $this->model_m->get_by_category($categoryID);
}
$data['selected_category'] = $categoryID;
$data['view'] = $this->load->view($view,$data,true);
$this->load->view($viewlayout,$data);
MODEL
public function get_by_category($id){
$query = $this->db->get_where('items', array('prod_id' => $id));
if ($query->num_rows() > 0) { return $query->result(); }
return false;
}
public function get_all_subcategories($catID = NULL,$limit = NULL ,$offset = NULL ) {
if(isset($catID) && is_int($catID)) {
if(isset($limit) && isset($offset)){
$result = $this->db->get_where('items',array('prod_id' => $catID),$limit,$offset);
return $result->result();
}
$result = $this->db->get_where('items',array('prod_id' => $catID));
return $result->result();
}
if(isset($limit) && isset($offset)){
$result = $this->db->get('items',$limit,$offset);
return $result->result();
}
$result = $this->db->get('items');
return $result->result();
}
public function count_all() {
return $this->db->get('items')->num_rows();
}
Related
I am working on CodeIgniter project but pagination is not showing in my view file.
I autoload the pagination library
I have total 7 rows in a database,& per_page set 5 rows.I also check to print_r($config) & its shows data well.
This is my controller:
public function cust_list()
{
$this->load->model('Cus_model');
$config =
[
'base_url'=>base_url('index.php/Cus_controller/cust_list'),
'per_page'=>5,
'totle_rows'=>$this->Cus_model->num_rows(),
];
$this->pagination->initialize($config);
$customer=$this->Cus_model->customer_list($config['per_page'],$this->uri->segment(4));
$this->load->view('salonadmin/customerview',['customers'=>$customer]);
}
This is my Cus_model:
public function customer_list($limit,$offset)
{
$user_id=$this->session->userdata('user_id');
$query=$this->db
->select('*')
->from('customer')
->limit( $limit,$offset )
->get();
return $query->result();
}
public function num_rows()
{
$user_id=$this->session->userdata('user_id');
$query=$this->db
->select('*')
->from('customer')
->get();
return $query->num_rows();
}
& this is my view:
<?= $this->pagination->create_links();?>
Controller
public function cust_list() {
$this->load->model('cus_model');
$config["base_url"] = base_url() . "cus_controller/cust_list/";
$config["total_rows"] = $this->cus_model->num_rows();
$config["per_page"] = 5;
$config['cur_tag_open'] = ' <a class="current">';
$config['cur_tag_close'] = '</a>';
$config['next_link'] = 'Next';
$config['prev_link'] = 'Previous';
$this->pagination->initialize($config);
// You should change your parameter according your url segment like 3 / 4
if($this->uri->segment(3)){
$page = $this->uri->segment(3);
}else{
$page = 0;
}
$this->data["customers"] = $this->cus_model-
>customer_list($config["per_page"], $page);
$str_links = $this->pagination->create_links();
$this->data["links"] = explode(' ',$str_links );
$this->load->view('salonadmin/customerview',$this->data);
}
Model
public function customer_list($limit,$offset){
$query = $this->db->select('*')->from('customer')->limit( $limit,$offset
)->get()->result();
return $query;
}
public function num_rows()
{
$query = $this->db->select('*')->from('customer')->get()->num_rows();
return $query;
}
View
<-- Pagination Link -->
<ul class="pagination pull-right">
<?php foreach ($links as $link) {
echo "<li>". $link."</li>";
} ?>
</ul>
I am stuck on this problem. I'm facing the issue at my URL. I am getting &per_page= in my URL.
I am using pagination in CI in which I had used following code but not able to remove it.
MY URL: http://localhost/abroad_education/entrance/index/0&per_page=3
Expected URL: http://localhost/abroad_education/entrance/index/0/3
Controller
class Entrance extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->helper('form');
$this->load->model('Pagination_model',"pgn");
}
public function index()
{
if($this->input->post('title') !="")
{
$title = trim($this->input->post('title'));
}
else{
$title = str_replace("%20",' ',($this->uri->segment(3))?$this->uri->segment(3):0);
}
$data['search_title']=$title;
print_r($title);
$allrecord = $this->pgn->allrecord($title);
$baseurl = base_url().$this->router->class.'/'.$this->router->method."/".$title;
$paging=array();
$paging['base_url'] =$baseurl;
$paging['total_rows'] = $allrecord;
$paging['per_page'] = 3;
$paging['uri_segment']= 4;
$paging['num_links'] = 8;
$paging['first_link'] = 'First';
$paging['first_tag_open'] = '<li>>';
$paging['first_tag_close'] = '</li>';
$paging['num_tag_open'] = '<li>';
$paging['num_tag_close'] = '</li>';
$paging['prev_link'] = 'Prev';
$paging['prev_tag_open'] = '<li>';
$paging['prev_tag_close'] = '</li>';
$paging['next_link'] = 'Next';
$paging['next_tag_open'] = '<li>';
$paging['next_tag_close'] = '</li>';
$paging['last_link'] = 'Last';
$paging['last_tag_open'] = '<li>';
$paging['last_tag_close'] = '</li>';
$paging['cur_tag_open'] = '<li class="active"><a href="javascript:void(0);">';
$paging['cur_tag_close'] = '</a></li>';
$this->pagination->initialize($paging);
$data['limit'] = $paging['per_page'];
$data['number_page'] = $paging['per_page'];
$data['offset'] = ($this->uri->segment(4)) ? $this->uri->segment(4):'0/';
$data['nav'] = $this->pagination->create_links();
$data['datas'] = $this->pgn->data_list($data['limit'],$data['offset'],$title);
$data['include'] = 'frontend/entrance/entrance';
$this->load->view('frontend/container', $data);
}
}
Model
class Pagination_model extends CI_Model{
function __construct(){
parent::__construct();
$this->load->library(array('session','pagination'));
$this->load->helper('url');
$this->load->database();
}
public function allrecord($title){
if(!empty($title)){
$this->db->like('ee_name',$title);
}
$this->db->select('*');
$this->db->from('vm_entrance_exams');
$rs = $this->db->get();
return $rs->num_rows();
}
public function data_list($limit,$offset,$title){
if(!empty($title)){
$this->db->like('ee_name',$title);
}
$this->db->select('*');
$this->db->from('vm_entrance_exams');
$this->db->order_by('ee_id','ee_desc');
$this->db->limit($limit,$offset);
$rs = $this->db->get();
return $rs->result_array();
}
}
Check that you have 'enable_query_strings' set to false in /application/config.php
$config['enable_query_strings'] = FALSE;
From the docs.
If you have $config['enable_query_strings'] set to TRUE your links
will automatically be re-written using Query Strings. This option can
also be explicitly set.
My Controller:-
public function index($id,$page=0) {
$config = array();
$config["base_url"] = base_url() . "index.php/categorypage/index/".$id."/";
$total_row = $this->Categorymodel->record_count($id);
$config["total_rows"] = $total_row;
$config["per_page"] = 20;
$config['use_page_numbers'] = TRUE;
$config['num_links'] = $total_row;
$config['cur_tag_open'] = ' <a class="current">';
$config['cur_tag_close'] = '</a>';
$config['next_link'] = 'Next';
$config['prev_link'] = 'Previous';
$this->pagination->initialize($config);
$page = $this->uri->segment(3) ;
$data["results"] = $this->Categorymodel->fetch_data($config["per_page"], $page,$id);
$str_links = $this->pagination->create_links();
$data["links"] = explode(' ',$str_links );
$this->load->view('frontend/template/other_head');
$this->load->view('frontend/template/category_nav');
$this->load->view('frontend/category_content',$data);
$this->load->view('frontend/template/footer');
}
My Model
class Categorymodel extends CI_Model {
// Count all record of table "contact_info" in database.
public function record_count($catid) {
//$this->db->where('cat_id',$catid);
$query = $this->db->get("tbl_product");
return $query->num_rows();
}
// Fetch data according to per_page limit.
public function fetch_data($limit,$page, $pageid) {
$this->db->limit(12,$limit);
$this->db->where('cat_id', $pageid);
$query = $this->db->get("tbl_product");
//echo $this->db->last_query();exit;
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
}
I can't get the "Offset" value, and The Pagination Url is not fetching other value. Please help me solve this problem.
It seems like you have bug here:
$this->db->limit(12,$limit);
Should be limit($limit, $page)
I am showing products in a page with a where clause. I am selecting everything from the product table where the category is vegetable, but it's not working. I am new in CodeIgniter. I also checked many questions on the Internet, but nothing is working. If there is anything in this code that is 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() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
}
?>
Controller method :
cutomise your links as per your requirements and base_url as 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 as first param then it 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() as $row)
{
$data[] = $row;
}
return $data;
}
}
// total count method
public function countRows($table) {
if (empty($table)) return false;
return $this->db->count_all($table);
}
last but not the least :
add this 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');
}
try with this...
$config['total_rows']= $this->db->get_where('product', array('title' => $vegetable))
->num_rows();
Hi all I am a new of codeigniter I have problam with pagination on codeigniter I try to search google and youtube but I can not do it who can help me please
this my code model:
<?php
class Product_model extends CI_Model {
function get_product($category="") {
$this->db->select('*');
$this->db->from('product');
if($category) {
$this->db->where('category',$category);
}
$query = $this->db->get();
return $query->result();
}
function get_total($category="") {
$this->db->select('count(*) AS num_row');
$this->db->from('product');
if($category) {
$this->db->where('category',$category);
}
$this->db->limit(1);
$query = $this->db->get();
return $query->row()->num_row;
}
}
and this my controoler code:
public function __construct()
{
parent::__construct();
$this->load->model('product_model','product');
}
public function menu()
{
$data = array();
$data['burger'] = $this->product->get_product('burger');
$total_burger = $this->product->get_total('burger');
$limit_burger = 1;
$link_burger = 'http://localhost/mbl/site/menu/burger';
$data['pagination_burger'] = $this->pagination($total_burger,$limit_burger,$link_burger);
$this->load->view('header_view');
$this->load->view('nav_view');
$this->load->view('content_view');
$this->load->view('content_left_view',$data);
$this->load->view('content_right_view');
$this->load->view('footer_view');
}
private function pagination($total ,$per_page ,$link) {
$config['base_url'] = $link;
$config['total_rows'] = $total;
$config['per_page'] = $per_page;
$config['page_query_string'] = TRUE;
$this->pagination->initialize($config);
return $this->pagination->create_links();
}
and my view:
<h1>Burger</h1>
<ul>
<?php foreach($burger as $val) { ?>
<li><?php echo $val->title; ?></li>
<?php } ?>
</ul>
<?php echo $pagination_burger; ?>
In Codigniter it is very simple
$this->load->library('pagination');
load this library
$config['base_url'] = 'http://example.com/index.php/test/page/';
$config['total_rows'] = 200;
$config['per_page'] = 20;
$this->pagination->initialize($config);
echo $this->pagination->create_links();
or
check this url