How can I add a page? - php

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

Related

how to works start and limit per page records in codeigniter php

I tried many ways but not working pagination. I have used segment but not working may be in this pagination between data working without pagination perfect.
Controller
class Modelcategory extends CI_Controller {
public function __construct() {
parent:: __construct();
$this->load->helper("url");
$this->load->model("Db_model");
$this->load->library("pagination");
}
function index(){
$msg=$this->input->get('msg');
$action=$this->input->get('action');
$resultArr='';
if(empty($action)){
$config = array();
$config['base_url'] = base_url('index.php/modelcategory');
$config['total_rows'] = $this->Db_model->get_count('model_category');
$config["per_page"] = 2;
$config["uri_segment"] = 1;
$this->pagination->initialize($config);
$page = ($this->uri->segment(1)) ? $this->uri->segment(1) : 0;
$data["links"] = $this->pagination->create_links();
$data['resultArr']=$this->Db_model->GetAllData('model_category',$config["per_page"], $page);
print_r($data['resultArr']);
}
if($msg == 'success'){
$msg ="Record inserted Successfully!";
}
$data['msg']=$msg;
$data['action']=$action;
$this->load->view('includes/header');
$this->load->view('modelcat',$data);
$this->load->view('includes/footer');
}
}
Model
function GetAllData($Table,$limit = NULL, $start = NULL){ //Insert Data into Database
$this->db->select("*");
$this->db->from($Table);
$this->db->limit($limit, $start);
$query = $this->db->get();
if ($query->num_rows() > 0 ){
return $query->result_array();
}else{
return false;
}
}
public function get_count($Table) {
return $this->db->count_all($Table);
}
VIEW
if(!empty($resultArr)){
$count=1;
foreach ($resultArr as $key => $value) {
echo $count++." ".$value['name']."<br>";
}
echo $this->pagination->create_links();
}
Let me know where is actually issue, I am using Codeigniter version 3.1.10
I figure out the issue, solution is in the 'base_url()' and with uri_segment.
Controller function :
function index(){
$msg=$this->input->get('msg');
$action=$this->input->get('action');
$resultArr='';
if(empty($action)){
$config = array();
$config['base_url'] = base_url('index.php/modelcategory/index');
$config['total_rows'] = $this->Db_model->get_count('model_category');
$config["per_page"] = 2;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["links"] = $this->pagination->create_links();
$data['resultArr']=$this->Db_model->GetAllData('model_category',$config["per_page"], $page);
print_r($data['resultArr']);
}
if($msg == 'success'){
$msg ="Record inserted Successfully!";
}
$data['msg']=$msg;
$data['action']=$action;
$this->load->view('includes/header');
$this->load->view('modelcat',$data);
$this->load->view('includes/footer');
}

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

Trouble with Codeigniter pagination

I am trying to implement pagination in my codeigniter application. But i got stucked. http://www.sitepoint.com/pagination-with-codeigniter/ This is the tutorial i followed.
My controller
public function index() {
$this->load->library('pagination');
$config = array();
$config["base_url"] = base_url().'adminroles/';
$config["total_rows"] = $this->M_admin_roles->record_count();
$config["per_page"] = 20;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["results"] = $this->M_admin_roles->
fetch_data($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
$data = array();
$data['view_file'] = 'v_listRoles';
echo Modules::run($this->template, $data);
}
My model
public function fetch_data($limit, $start) {
$this->db->limit($limit,$start);
$query = $this->db->get("roles");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
public function record_count() {
return $this->db->count_all("roles");
}
Iam rendering the view simply as
<p><?php echo $links; ?></p>
But shows error in view file. Error is "Undefined variable: links". What Iam doing wrong here?Is there anything to add as additionanal. Iam using HMVC Codeigniter. Please help me to fix this.
You have written
<p><?php echo $links; ?></p> in your view file,
but you have to write the following code in your view file
echo $this->pagination->create_links();
You are initialize $data on wrong place.
Try this -
public function index() {
$data = array();
$this->load->library('pagination');
$config = array();
$config["base_url"] = base_url().'adminroles/';
$config["total_rows"] = $this->M_admin_roles->record_count();
$config["per_page"] = 20;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["results"] = $this->M_admin_roles->
fetch_data($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
$data['view_file'] = 'v_listRoles';
echo Modules::run($this->template, $data);
}

How to Give Proper Pagination To Data Table using Pagination library in Codeigniter

I am Working pagination in CodeIgniter. I see tutorials and user manual also. but I can't find the way to do it properly they call the database in Controller for pagination. I want a proper solution.
This is The Controller
public function index() {
$this->load->library('pagination');
$config['base_url'] = 'index.php/Emp';
$config['total_rows'] = 200;
$config['per_page'] = 2;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
$data = array();
$data['showEmployeeTable']=$this->Em->selectEmployeeData(); //FOR SHOWING A EMPLOYEE DATABASE TABLE
if($query = $this->Em->getDesignationData()) //grabs all record
{
$data['records'] = $query;
}
if($query = $this->Em->getCityRecord()) //grabs all record
{
$data['records_city'] = $query;
}
//$this->load->view('emp_update', $data);
$this->load->view('erv', $data);
}
This are My models
public function getDesignationData() {
$query = $this->db->get('emp_desig'); //model created for get data
return $query->result();
}
public function getCityRecord() {
$query = $this->db->get('city'); //model created for get data
return $query->result();
}
// ***************** VEDDING PLAN MODELS **********************************************************
public function selectEmployeeData() {
$query = $this->db->get('emp_manage');
return $query;
}
So how can I show Proper Pagination on the View Page? Kindly answer me step by step. I am a newbie in Codeigniter.
And this in the view.
<?php echo $this->pagination->create_links(); ?>
Your controllers should be
public function index() {
$this->load->library('pagination');
$config = array();
$config['base_url'] = 'index.php/Emp';
$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['showEmployeeTable']=$this->Em->selectEmployeeData($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
if($query = $this->Em->getDesignationData()) //grabs all record
{
$data['records'] = $query;
}
if($query = $this->Em->getCityRecord()) //grabs all record
{
$data['records_city'] = $query;
}
$this->load->view('erv', $data);
}
Your model should be
public function selectEmployeeData($limit, $start) {
$this->db->limit($limit, $start);
$query = $this->db->get('emp_manage');
return $query;
}
And in your views add following line
<?php echo $links; ?>
see this code and make some changes as per your requirement:
You controller should be:
function list($offset=0)
{
$num_rows=$this->db->count_all("table_name");
$config['base_url'] = site_url('list');
$config['total_rows'] = $num_rows;
$config['per_page'] = 100;
$config['num_links'] = round($config['total_rows']/$config['per_page']);
//$config['use_page_numbers'] = TRUE;
$config['page_query_string']=TRUE;
$this->pagination->initialize($config);
$data["results"] = $this->Em->selectEmployeeData($config["per_page"], $page);
$this->load->view('viewfile',$data);
}
Your Model :
function selectEmployeeData($limit,$offset)//fetch all data with pagination
{
$data = array();
$this->db->limit($limit, $offset);
$Q = $this->db->get('table');
if($Q->num_rows() > 0)
{
foreach ($Q->result_array() as $row)
{
$data[] = $row;
}
}
$Q->free_result();
return $data;
}
In View :
<?php echo $this->pagination->create_links();?>

Codeigniter pagination on function index

I edited this after figuring out a few things but is this a good way if I want my links on index? Without the function page, it will not work correctly if the base_url is test/index, but test/test will work.
controller
class Test extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('Test_model');
$this->load->library('pagination');
}
public function index()
{
$page['title'] = '';
$page['file'] = 'test/index';
$config['base_url'] = base_url().'test/page';
$config['total_rows'] = $this->Test_model->record_count();
$config['per_page'] = 2;
$config['num_links'] = 5;
$offset = $this->uri->segment(3,0);
$this->pagination->initialize($config);
$page['data']['items'] = $this->Test_model->getItems($config['per_page'], $offset);
$page['data']['pagination'] = $this->pagination->create_links();
$this->load->view('template', $page);
}
public function page()
{
$page['title'] = '';
$page['file'] = 'test/index';
$config['base_url'] = base_url().'test/page';
$config['total_rows'] = $this->Test_model->record_count();
$config['per_page'] = 2;
$config['num_links'] = 5;
$offset = $this->uri->segment(3,0);
$this->pagination->initialize($config);
$page['data']['items'] = $this->Test_model->getItems($config['per_page'], $offset);
$page['data']['pagination'] = $this->pagination->create_links();
$this->load->view('template', $page);
}
}
model
public function record_count()
{
return $this->db->count_all('item');
}
public function getItems($limit, $offset)
{
$query = $this->db->get('item', $limit, $offset);
$result = $query->result();
return $result;
}
view
<h2><?=$pagination; ?></h2>
<table>
<?php foreach($items as $item) { ?>
<tr><td><?=$item->name?></td></tr>
<?php } ?>
Try this:
function __construct()
{
parent::__construct();
$this->load->helper('array');
$this->load->helper('url');
$this->load->library('pagination');
$this->load->model('Test_model','',TRUE);
}
function index($uri_segment="3")
{
$config['base_url'] = base_url('test/index');
$config['total_rows'] = $this->Test_model->record_count();
$config['per_page'] = 5;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
$page['data']['products'] = $this->Test_model->getItems($config['per_page'],$this->uri->segment(3));
$page['data']['pagination']= $this->pagination->create_links();
$page['title'] = '';
$page['file'] = 'test/index';
$this->load->view('template', $page);
}
If the generated listing shows random pages on clicking next page,it should be taking next page number rather than next id(+5). in such case, add
$config['use_page_numbers'] = TRUE;
before initializing the pagination config.
In controller you have to update this
public function index()
{
$this->load->library('pagination');
$config['base_url'] = base_url().'test/index'; // use test/test and it works
$config['total_rows'] = $this->Test_model->record_count();
$config['per_page'] = 5;
$config['num_links'] = 10;
$config['uri_segment'] = 3;
$offset = $this->uri->segment(3,0);
$this->pagination->initialize($config);
$page['data']['products'] = $this->Test_model->getItems($config['per_page'], $offset);
$page['data']['pagination'] = $this->pagination->create_links();
$page['title'] = '';
$page['file'] = 'test/index';
$this->load->view('template', $page);
}
here is helpful link for you
Codeigniter pagination

Categories