codeigniter pagination Numbers not displayed correcly - codeigniter

Codeigniter pagination Numbers not displayed correcly in result page,.
Plz help me.
/controller page code/
function search_all()
{
$this->load->view(‘prd’);
$config = array();
$config[‘base_url’] = base_url().’/product_search/search_all’;
$config[“total_rows”] = $this->search_product_model->record_count();
$config[“per_page”] = 8;
$config[“uri_segment”] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data[“results”] = $this->search_product_model->
get_search($config[“per_page”], $page);
$data[“links”] = $this->pagination->create_links();
//$this->load->view(“example1”, $data);
$this->load->view(‘asd’, $data);
}

I think there is an problem with this
$config[“total_rows”] = $this->search_product_model->record_count();
For this you are not passing the search keys such that it will count all the data which it contains.If you get the record count based on search key then it will be fine.Like this
$config[“total_rows”] = $this->search_product_model->record_count('//search parameters');
and in your model:
Public function record_count($search_params)
{
$this->db->where('product',$search_params);
return $this->db->count_all(“products”);
}
You can modify as your needs like pass "color","type" to your record_count() function and based on that,you suggested to get "count_all".I hope it will works for you

Try this
function search_all()
{
$config[‘base_url’] = base_url(’product_search/search_all’);
$config[“total_rows”] = $this->search_product_model->record_count();//2 in your case
$config[“per_page”] = 8;
$config[“uri_segment”] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data[“results”] = $this->search_product_model->
get_search($config[“per_page”], $page);
$data[“links”] = $this->pagination->create_links();
//$this->load->view(“example1”, $data);
$this->load->view(‘asd’, $data);
}
and at your model:
public function get_search($limit, $page)
{
//Add this...???
$this->db->limit($limit, $page); //or try $this->db->limit($page,$limit)
}

give search key in the record_count() function as parameters //You got 2 na.??
Then in placeof search_params put you same keys which are supplied to the record_count()

Related

Why is my pagination in codeigniter not working? (404 not found)

I already follow the step and tutorial. But it's not working.
I already follow tutorial from internet step by step. But it always show 404 page not found when I go to second page
This is my controller:
public function index()
{
$this->load->library('pagination');
$config['base_url'] = base_url().'index.php/blog';
$config['uri_segment'] = 2;
$config['per_page'] = 2;
$config['total_rows'] = $this->mod_blog->getAllData()->num_rows();
$this->pagination->initialize($config);
// $page = ($this->uri->segment(2)) ? ($this->uri->segment(2) - 1) : 0;
$page = $this->uri->segment(2);
$data = array(
'data' => $this->mod_blog->getData($config['per_page'],$page)->result(),
'pagination' => $this->pagination->create_links()
);
$this->load->view('layout/header');
$this->load->view('layout/sidebar');
$this->load->view('admin/blog',$data);
$this->load->view('layout/footer');
}
This is my Model:
function getData($limit,$start){
$this->db->select('*');
$this->db->from('berita');
$this->db->join('admin','admin.id_admin = berita.id_admin');
$this->db->limit($limit,$start);
$this->db->order_by('id_berita','desc');
return $this->db->get();
}
function getAllData(){
$this->db->select('*');
$this->db->from('berita');
$this->db->join('admin','admin.id_admin = berita.id_admin');
$this->db->order_by('id_berita','desc');
return $this->db->get();
}
Whoops, I found my mistake.
It's in the
$config['base_url'] = base_url().'index.php/blog';
You must point the config['base_url'] to your baseUrl/controllerName/functionName. So, I changed it into.
$config['base_url'] = base_url().'index.php/blog/index';
And then I adjust my Uri segment into 3
$config['uri_segment'] = 3;
$page = $this->uri->segment(3);

Codeigniter pagination is not passing the correct link value in url

I have created a pagination in codeigniter, In that I have totally 13 records and i want to display 5 records in each page. When i click on the pagination link number 2,It should pass value 2 in url and it show the next 5 records but I am getting value 5 in url instead of 2.
My controller code:
public function activities($page_num = 1){
$config =array();
$config['base_url'] = base_url().'admin/skills/activities';
$config['per_page'] = 5;
$config['total_rows'] = $this->skill_model->all_activities(0,'',''); // I will get the total count from my model
if(empty($page_num))
$page_num = 1;
$limit_end = ($page_num-1) * $config['per_page']; //end limit
$limit_start = $config['per_page']; // start limit
$this->pagination->first_url = $config['base_url'].'/1';
$this->pagination->initialize($config);
$data['activity_list'] = $this->skill_model->all_activities(1,$limit_start,$limit_end);
}
This is my view code:
<?php echo '<div class="pagination" style="float:right;">'.$this->pagination->create_links().'</div>'; ?>
My Model part:
function all_activities($flag,$limit_start,$limit_end){
$this->db->select('*');
$this->db->from('skills_activities');
//echo $limit_start." ".$limit_end;
if($flag == 1 ){
$this->db->limit($limit_start, $limit_end);
$query = $this->db->get();
return $query->result_array();
} else {
$query = $this->db->get();
return $query->num_rows();
}
}
When i click on pagination link I am getting the following url:
http://192.168.1.97/projects/homecare/admin/skills/activities/5
but I should get:
http://192.168.1.97/projects/homecare/admin/skills/activities/2
I don't know where i have done a mistake.
can anybody help me?
Thanks in advance.
$config['use_page_numbers'] = true;
This will produce page number in the url instead of record number. By default its false.
So you controller will look like this
public function activities($page_num = 1)
{
$config =array();
$config['base_url'] = base_url().'admin/skills/activities';
$config['per_page'] = 5;
$config['use_page_numbers'] = true;//you missed this line
$config['total_rows'] = $this->skill_model->all_activities(0,'',''); // I will get the total count from my model
if(empty($page_num)) $page_num = 1;
$limit_end = ($page_num-1) * $config['per_page']; //end limit
$limit_start = $config['per_page']; // start limit
$this->pagination->first_url = $config['base_url'].'/1';
$this->pagination->initialize($config);

pagination codeigniter always starts at page 2 and active link is always at page 2

i'm new to codeigniter and i'm using codeigniter pagination by getting query result from Category Id, but the page always starts at page 2 .
how do i fix the pagination to set the start page at number page 1?
public function kategori($kat=''){
$config['base_url'] = base_url().'index.php/lowongan/kategori/'.$kat;
$data['db']=$this->query->katPostingan($kat);
$config['total_rows'] = $data['db']->num_rows();
$config['per_page'] = 1;
$config['uri_segment'] = 4;
$this->pagination->initialize($config);
$data2['paging'] = $this->pagination->create_links();
$page = ($this->uri->segment($config['uri_segment'])) ? $this->uri->segment($config['uri_segment']) : 0;
$data2['records'] = $this->query->katPos($kat, $config['per_page'], $page);
$this->load->view( 'user/hal_kategori',$data2);
}
and my model
public function katPos($kat='',$limit,$start){
$this->db->where("id_kat", $kat);
$this->db->limit($limit,$start);
$query = $this->db->get("showKat");
return $query;
}
public function katPostingan($kat=''){
$this->db->where("id_kat", $kat);
$query = $this->db->get("showKat");
return $query;
}
try a different way of solution,
controller(index())
$this->load->library('pagination');
$config['base_url'] = site_url().'classname/index';
$config['per_page'] = $this->config->item('pagination_number');
$config['uri_segment'] = 4;
$data['count']=$this->example_model->count();
$config['total_rows'] =$data['count'];
$data['classname']=$this->example_model->getall($this->config->item('pagination_number'),$this->uri->segment(4));
$this->pagination->initialize($config);
$data['page_link']=$this->pagination->create_links();
$this->load->view('users/classname.php',$data);
model
function getall($num,$offset)
{
$this->db->from('table_name');
$this->db->limit($num,$offset);
$query = $this->db->get();
return $query->result_array();
}
function count()
{
$this->db->from('table_name');
$query = $this->db->get();
$rowcount = $query->num_rows();
return( $rowcount);
}
View
<div class="pagination"> <?php echo $page_link;?> </div>

codeigniter pagination pulling item more then once

This is a very irritating issue. I have my codeigniter pagination set up and so I thought working, but looking at it closer it seems that on the last page it's pulling in previous results to fill the page in.
So say I want ten per page and have fourteen results. The first page has ten results, and so does the second. When it should be the first has ten and the second has four. It would be fine if it was just repeating one result, but it's irritating to have to scroll through six previous results. Any help would be much appreciated.
in my controller I have the pagination code
$config = array();
$config["base_url"] = base_url()."myStories/".$id;
$config["total_rows"] = $this->data_model->my_count();
$config["per_page"] = 10;
$config["uri_segment"] = 3;
$config['num_links'] = 2;
$choice = $config["total_rows"] / $config["per_page"];
//$config["num_links"] = round($choice);
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$this->load->view('userStory_view', array(
'query' => $this->data_model->pullMyStories($config['per_page'], $page),
'links' => $this->pagination->create_links(),
'user' => $this->users_model->getUser($this->user->user_id),
));
and then in my model I have the count and then the actual results coming back
public function my_count() {
//This counts all the stories that belong to that author
$author = $this->uri->segment(2);
$this->db->where('author', $author);
$this->db->where(array('approved !=' => 'd'));
$query = $this->db->get('story_tbl');
return $query->num_rows();
}
public function pullMyStories($limit, $start){
//This pulls back all the stories that belong to that author
$this->db->limit($limit, $start);
$this->db->order_by("date", "desc");
$author = $this->uri->segment(2);
$this->db->where(array('approved !=' => 'd'));
$this->db->where('author', $author);
$story = $this->db->get('story_tbl');
return $story->result();
}
the route I have set up that does work
$route['myStories/(:any)'] = "story/viewStories/$1";
I thought initially that my count was count was wrong, but even with a count of 14, 20 results come back.
For further information I am more than positive that my baseUrl is correct. I have modified my .htaccess to get rid of the index.php and have edited my route file to make the controller disappear from the url. To try and make it easy to remember for the user.
I am also very sure that the uri segments are correct. If they were not correct then my page would not be coming up at all.
I have tried all the normal solutions and nothing has worked. That is why I am asking here and why I have placed a bounty on this question.
var $base_url = ''; // The page we are linking to
var $prefix = ''; // A custom prefix added to the path.
var $suffix = ''; // A custom suffix added to the path.
var $total_rows = 0; // Total number of items (database results)
var $per_page = 10; // Max number of items you want shown per page
var $num_links = 2; // Number of "digit" links to show before/after the currently viewed page
var $cur_page = 0; // The current page being viewed
var $use_page_numbers = FALSE; // Use page number for segment instead of offset
var $first_link = 'First';
var $next_link = '->';
var $prev_link = '<-';
var $last_link = 'Last';
var $uri_segment = 2;
var $full_tag_open = '';
var $full_tag_close = '';
var $first_tag_open = '';
var $first_tag_close = ' ';
var $last_tag_open = ' ';
var $last_tag_close = '';
var $first_url = ''; // Alternative URL for the First Page.
var $cur_tag_open = ' <strong>';
var $cur_tag_close = '</strong>';
var $next_tag_open = ' ';
var $next_tag_close = ' ';
var $prev_tag_open = ' ';
var $prev_tag_close = '';
var $num_tag_open = ' ';
var $num_tag_close = '';
var $page_query_string = FALSE;
var $query_string_segment = 'per_page';
var $display_pages = TRUE;
var $anchor_class = '';
Your problem is that you are passing the wrong parameters to your pullMyStories method. On the first page you will be apply the following limit to your query
LIMIT 0,10
Then on the second page
LIMIT 1,10
Then on the third
LIMIT 2,10
So you pagination is only moving forward one item at a time instead of ten. So you need to change this
'query' => $this->data_model->pullMyStories($config['per_page'], $page),
To this
'query' => $this->data_model->pullMyStories($config['per_page'], ($page * $config['per_page'])),
I recently tried so hard to about ci pagination.
I think, your codes right.
What exactly uri string on second page?.. And this listing function is index() ?
Try if it works for you:
$config = array();
$config["base_url"] = base_url()."myStories/"; #change in base url
$config["total_rows"] = $this->data_model->my_count();
$config["per_page"] = 10;
$config["uri_segment"] = 3;
$config['num_links'] = 2;
#$choice = $config["total_rows"] / $config["per_page"];
//$config["num_links"] = round($choice);
$this->pagination->initialize($config);
#$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0; #no need of calculation here
$this->load->view('userStory_view', array(
'query' => $this->data_model->pullMyStories($this->uri->segment(3)), #change here send the offset directly
'links' => $this->pagination->create_links(),
'user' => $this->users_model->getUser($this->user->user_id),
));
function pullMyStories($offset = 0){
$story = $this->db->where('author', $this->uri->segment(2))->where('approved != d')->order_by("date", "desc")->get('story_tbl', 10, $offset);
return $story->result();
}
Try this. You just need to change the base_url in the correct way. Also be careful and check if you are getting a right number for uri_segment. If not, you can change the number, and get the correct.
// $config = array();
// MUST CHNGE IT I just tell the with simple example. If you have index.php, c name and method name.
$config["base_url"] = base_url()."index.php/controller_name/function_name/";
$config["total_rows"] = $this->data_model->my_count();
$config["per_page"] = 10;
$config["uri_segment"] = 3; // BE CARFULL with uri_segment. You need to print it, and be shre that you are getting the right number
$config['num_links'] = 2;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$this->load->view('userStory_view', array(
'query' => $this->data_model->pullMyStories($config['per_page'], $page),
'links' => $this->pagination->create_links(),
'user' => $this->users_model->getUser($this->user->user_id),
));
I update my code, I comment and $config = array();
I did it today in my computer this, and it works. I know that you maybe checked it hundred times, but Check in details it again.
UPDATE with my example:
function index() {
$data['page_title'] = "Items";
$config['base_url'] = base_url() .'index.php/items/index/';
$config['total_rows'] = $this->items_model->count_items(); // Count items from DB
$config['per_page'] = 10;
$config['uri_segment'] = 3;
// Customize
$config['next_link'] = FALSE;
$config['prev_link'] = FALSE;
$config['first_link'] = 'first';
$config['last_link'] = 'last';
$config['cur_tag_open'] = '<a class="current">';
$config['cur_tag_close'] = '</a>';
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data['items'] = $this->items_model->get_items($config["per_page"], $page);
$this->load->view('invoice-items', $data);
}
In your controller:
$page = $this->uri->segment(3,0);
Then:
public function pullMyStories($limit, $start){
$author = $this->uri->segment(2);
$this->db
->select('*')
->where('approved !=', 'd')
->where('author', $author)
->order_by('date', 'DESC')
->limit($limit, $start);
$story = $this->db->get('story_tbl');
return $story->result();
Also where do you load the library using the line below?
$this->load->library('pagination');
I'm just gonna throw this in and hope it helps in some way, because I've tested and it works for me. I've made the following assumptions for testing:
id (as in "myStories/".$id in the controller) is taken from $this->uri->segment(2)
story_tbl fields I created were id, author, date
My test files were as follows:
Controller MyStories.php:
public function index()
{
//loading library, model, and assumptions
$this->load->library('pagination');
$this->load->model('m_page', 'data_model');
$id = $this->uri->segment(2);
//
//
//Your code from here...
//
//
$config = array();
$config["base_url"] = $this->config->site_url("myStories/".$id);
$config["total_rows"] = $this->data_model->my_count();
$config["per_page"] = 3;
$config["uri_segment"] = 3;
$config["num_links"] = round($config["total_rows"] / $config["per_page"]);
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data = array();
$data['query'] = $this->data_model->pullMyStories($config['per_page'], $page);
$data['links'] = $this->pagination->create_links();
$data['config'] = $config;
//$data['user'] = $this->users_model->getUser($this->user->user_id);
$this->load->view('userStory_view', $data);
}
BTW, you really want to be using the site_url and not base_url when defining the pagination base_url.
I also commented out the 'user' data in the controller simply because you never gave any info on that.
My test view, userStory_view.php:
<?php echo $links;?>
<!-- -->
<hr>
total rows: <?php echo $config['total_rows'];?>
<hr>
<?php echo $this->db->last_query();?>
<hr>
<!-- -->
<?php foreach ($query as $row):?>
<?php echo $row->author.'<hr>';?>
<?php endforeach;?>
I made no changes to your Model, so no need to show that here.
I added the following line to routes.php:
$route['myStories/(:any)'] = "myStories";
As I said, everything worked for me. Besides my formatting, the only changes I really made were the use of site_url() instead of base_url(), and the commenting of the user data. So pretty stuck as to why you're having issues I'm afraid.
As far as it seems your overall pagination functionality seems to correct. what i want check would the sql query returned in each function and parameters passed to those function coz $author variable has to global and try
$this->db->last_query();
on your both functions my_count() and pullMyStories($limit, $start) and check those function returning right results.
U might also can try writing direct sql query with changing parameters like.
$sql = "SELECT * FROM some_table WHERE author = ? LIMIT ?, ?";
$this->db->query($sql, array($author, $start, $limit));
As i am seeing this would be mostly of query might have been wrong .
Hope this helps.

codeigniter pagination error 404

I have Admin controller in codeigniter
class Admin extends CI_Controller {
function __construct() {
parent::__construct();
if (!$this->tank_auth->is_logged_in()) redirect('login');
$this->load->library('pagination');
}
function index() {
$offset = $this->uri->segment(2);
$config['per_page'] = 3;
$data['sitetitle'] = 'Výpis jobů';
$data['listings'] = $this->Jobs_model->get_listings(0,$user_id = FALSE,$config['per_page'],$offset);
$config['uri_segment'] = 2;
$config['base_url'] = base_url().'admin/';
$config['total_rows'] = $this->db->count_all_results('jobs');
$this->pagination->initialize($config);
$this->template->set('title', 'Domovská stránka');
$this->template->load('template', 'site', $data);
}
}
and Jobs_model
function get_listings($category, $user_id = false, $limit = 0, $offset = 0) {
$data = array();
$this->db->order_by('id', 'desc');
$q = $this->db->get('jobs');
if ($category) {
$options = array('category' => $category);
$this->db->order_by('id', 'desc');
$this->db->where('category', $category);
$q = $this->db->get('jobs', $limit, $offset);
}
else {
$query = $this->db->order_by('id', 'desc');
if ($user_id) $query = $query->where('user_id', $user_id);
$q = $query->get('jobs',$limit, $offset);
}
if ($q->num_rows() > 0) {
foreach ($q->result_array() as $row) {
$data[] = $row;
}
}
$q->free_result();
return $data;
}
first page in paginatiom obtain data, but links generating in pagination localhost/sitename/admin/3 produce 404 error.
Where is problem in my script
You need to change:
$config['base_url'] = base_url().'admin/';
To:
$config['base_url'] = base_url().'admin/index/';
If you need the url to be like admin/3, you can use a route or _remap.
Side note: consider using this to get your page number rather than the URI class:
function index($offset = 0) {
// your code
}
It will do the same thing, but it's convenient to use the controller method arguments when possible.
You need to do some change.
$config['base_url']=base_url().'admin/index';
change
$config['uri_segment'] = 3;
check if you are using .htaccess or not. If you aren't, then $config['base_url'] in above should be
$config['base_url']=base_url().'index.php/admin/index';
If you get 404 error on codigniter paggination please check follwing things :
1 ) Check your controller it's index or somthing else if it's index then base_url should be :
base_url().'admin/index/pages' and if it's somethings else it should be :
base_url().'admin/somethingelse/pages'
2 ) Check 'uri_segment' by $this->uri->segment(4) so by this you got the page number then else will do the codiegniter.

Resources