Codeigniter Pagination is not working correctly - php

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)

Related

Codeigniter pagination issue when using where clause

Codeigniter pagination with the passing parameter to where clause links not working
Here is my Model
public function record_count($category) {
return $this->db->where('sub_category_name',$category)->count_all_results("categories_item");
}
public function fetch_departments($category,$limit, $start) {
$this->db->limit($limit, $start);
$query=$this->db->where('sub_category_name',$category)->get("categories_item");
if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
$data[] = $row;
}
return $data;
}
return false;
}
//Here is Controller
public function categories_item()
{
$this->load->database();
$this->load->library('pagination');
$this->load->model('Home_model');
$config = array();
$config["base_url"] = base_url() . "Home/categories_item";
$category=$this->uri->segment(3);
$this->db->where('sub_category_name',$category);
$config['total_rows'] = $this->db->get('categories_item')->num_rows();
$config['use_page_numbers'] = TRUE;
$config["per_page"] = 1;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? ($this->uri->segment(3)) : 0;
$data["link"] = $this->pagination->create_links();
if( !$data["results"] = $this->Home_model->fetch_departments($category,$config["per_page"], $page))
{
error_reporting(1);
}
else
{
$this->load->view("category_item_list",$data);
}
}

Pagination not showing in Codeigniter

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>

where clause not working in codigniter

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();

Pagination in Codeigniter returns error

My problem is that pagination is not working, its perfectly shows the number of pages << 1 2 3 >> etc... but when I tried to go on page 2 then it gives me an error like:
The requested URL /mysite/welcome/index/2 was not found on this server.
Here is my Model called: Country.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Country extends CI_Model
{
public function __construct() {
parent::__construct();
}
public function record_count() {
return $this->db->count_all("news");
}
public function fetch_countries($limit, $start) {
$this->db->limit($limit, $start);
$query = $this->db->get("news");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
}
Here is my Controller which shows the news on the page, and the pagination, but unfortunately as I said when I try to go for page 2 then it returns an error message.
My Controller called Welcome.php
class Welcome extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* #see https://codeigniter.com/user_guide/general/urls.html
*/
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
$this->load->helper('url_helper');
$this->load->model("Country");
$this->load->library("pagination");
}
public function index()
{
$config = array();
$config["base_url"] = base_url('') . "welcome/index";
$config["total_rows"] = $this->Country->record_count();
$config["per_page"] = 2;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["results"] = $this->Country->
fetch_countries($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
$data['news'] = $this->news_model->get_news();
$data['title'] = 'News archive';
$this->load->view('index', $data);
}
public function view($slug = NULL)
{
$data['news_item'] = $this->news_model->get_news($slug);
if (empty($data['news_item']))
{
show_404();
}
$data['title'] = $data['news_item']['title'];
$this->load->view('view', $data);
}
}
The routes are the default one
$route['default_controller'] = 'welcome';
Also I can see my news and pagination on the homepage:
localhost/mysite
I hope I was clear, please help me out why the pagination is not working.
Thanks for your kind help.
do like this
Controller
public function managecategory() {
$this->load->helper(array('form', 'url'));
$this->load->view('moderator/templates/header');
$this->load->view('moderator/templates/sidebar');
$parent = '0';
$data['catdata'] = $this->b2bcategory_model->form_select($parent);
$this->load->library('pagination');
// bootstrap style for maintaining pagination
$config = array();
$config["base_url"] = base_url() . "moderator/B2BCategory/managecategory";
$config["total_rows"] = $this->b2bcategory_model->record_count();
$config["per_page"] = 20;
$config["uri_segment"] = 4;
$config['full_tag_open'] = '<ul class="pagination">';
$config['full_tag_close'] = '</ul>';
$config['first_link'] = 'first';
$config['last_link'] = 'last';
$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
$config['prev_link'] = '&laquo';
$config['prev_tag_open'] = '<li class="prev">';
$config['prev_tag_close'] = '</li>';
$config['next_link'] = '&raquo';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '</li>';
$config['last_tag_open'] = '<li>';
$config['last_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="active"><a href="#">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$this->pagination->initialize($config);
$page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
$data["results"] = $this->b2bcategory_model->fetch_data($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
$this->load->view('moderator/managecategory', $data);
$this->load->view('moderator/templates/footer');
}
In Model
public function fetch_data($limit, $start) {
$this->db->limit($limit, $start);
$query = $this->db->get_where("jil_category", array('ctg_parent'=>'0'));
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}

How can I add a page?

Using CodeIgniter 2.0.2
I want to set up pagination for my website.
This is what I've in my controller:
$config = array();
$config["base_url"] = base_url() . "/credits";
$config['total_rows'] = 200;
$config['per_page'] = 2;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["results"] = $this->credits_model->fetch_countries($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
Model:
public function record_count() {
return $this->db->count_all("sadmin_credits");
}
public function fetch_countries($limit, $start) {
$this->db->limit($limit, $start);
$query = $this->db->get("sadmin_credits");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
I need some help in getting it to work. It shows the numbers but when I click on them it just shows the same results nothing changes.
try this
public function credits($offset='')
{
$total_rows = $this->credits_model->record_count();
$config['base_url'] = base_url() . "/credits";
$config['total_rows'] = $total_rows;
$config['per_page'] = 2;
$this->pagination->cur_page = $offset;
$this->pagination->initialize($config);
$data["results"] = $this->credits_model->fetch_countries($config["per_page"], $offset);
$data["links"] = $this->pagination->create_links();
}
or check this tutorials
http://only4ututorials.blogspot.in/2014/04/pagination-with-codeigniter.html

Categories