pagination in codeigniter - weird working - codeigniter

My pagination not working properly.
For example my $count = 3, $limit = 1 so i should have 3 pages
And this pagination look like this: 1 2 3 >
1 - Its not clickable and always bold (as active)
2 - When i click on it my uri segment(4) change on '1', dont change on bold (as active)
3 - When i click on it my uri segment(4) change on '2', dont change on bold (as active)
' > ' - Working only when i dont have uri segment(4) and when i click segment(4) change on '1' but when i click again its not working. When segment(4) = '2' and click it change on '1'.
' < ' - doesnt show at all
Controller:
$value = $this->uri->segment(3);
$this->load->library('pagination');
$limit = 1;
$offset = $this->uri->segment(4);
$this->db->limit($limit, $offset);
$this->db->where('when_data >='. date('Y-m-d'));
$this->db->select('*');
$this->db->from('meeting');
$this->db->join('category', 'category.id = meeting.id_cat');
$this->db->join('users', 'users.id = meeting.id_p');
$data['meeting'] = $query = $this->db->get();
$count = $query->num_rows();
$config['base_url'] = base_url() . 'main/kategoria/'. $value;
$config['total_rows'] = $count;
$config['per_page'] = $limit;
$config['first_link'] = 'First';
$config['last_link'] = 'Last';
$config['cur_tag_open'] = '<b>';
$config['cur_tag_close'] = '</b>';
$config['next_link'] = '>';
$config['prev_link'] = '<' ;
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$this->load->view('spotkania/kategoria', $data);
View:
<?php echo $pagination; ?>
Any idea?

you missed to add this config
$config['uri_segment'] = 4;//your case its 4.

Related

Link Pagination does'nt shown on codeigniter

I have to add pagination to my site and the link does not appear / shown
I Have an controller like this :
$data['a'] = 9;
$data['title'] = 'Articles';
$data['content'] = 'home/articles';
$this->load->library('pagination');
$q = $this->db->get_where('articles',array('article_category_id' => 18));
$config['uri_segment'] = 2;
$config['total_rows'] = $q->num_rows();
$config['per_page'] = 3;
$config['base_url'] = base_url() . 'article/';
$this->pagination->initialize($config);
$this->db->order_by('id','desc');
$this->db->limit($config['per_page'],$this->uri->segment(2));
$q = $this->db->get_where('articles',array('article_category_id' => 18));
$data['blogs'] = $q;
$data['pagination'] = $this->pagination->create_links();
$this->load->view('home/index', $data);
and view like this :
//Body of the site
Halaman : <?php echo $pagination; ?>
Site View

Clicking on CI Pagination links produces wrong queries

I'm currently having problem when clicking on they pagination link of my site using CodeIgniter. The search function works fine, however, when I click on any of the pagination links, it seems like it does the filter again and again.
For example:
I choose "Rent" as my choice to search for:
Then click search I get 27 results. After that I click on the "2" pagination button at the bottom:
I get another 73 results and so on and so forth.
My Controller:
$this->load->library('pagination');
$search['base_url'] = base_url() . 'page_search/';
$search['per_page'] = 5;
$search['num_links'] = 5;
$search['num_tag_open'] = '<li>';
$search['num_tag_close'] = '</li>';
$search['first_link'] = 'First';
$search['first_tag_open'] = '<li class="first">';
$search['first_tag_close'] = '</li>';
$search['last_tag_open'] = '<li class="last">';
$search['last_tag_close'] = '</li>';
$search['next_tag_open'] = '<li>';
$search['next_tag_close'] = '</li>';
$search['prev_tag_open'] = '<li>';
$search['prev_tag_close'] = '</li>';
$search['last_link'] = 'Last';
$search['next_link'] = '»';
$search['prev_link'] = '«';
$search['full_tag_open'] = '<div class="pagination pagination-centered"><ul>';
$search['full_tag_close'] = '</ul></div>';
$search['cur_tag_open'] = '<li class="active"><a href="#">';
$search['cur_tag_close'] = '</a></li>';
$search['total_rows'] = $this->db->get_where( 'listing', $where )->num_rows();
$search['uri_segment'] = 2;
$config['use_page_numbers'] = TRUE;
$this->pagination->initialize( $search );
$this->db->order_by("id", "desc");
$this->db->where( $where );
$data['results'] = $this->db->get( 'listing', $search['per_page'], $this->uri->segment(2);
$data['rows'] = $this->db->get_where( 'listing', $where )->num_rows();
$data['content'] = 'website/page-search';
$this->load->view('template/website/theme', $data);
Which $where is the query which I am sure it works fine.
UPDATE:
Also I have this in my route:
$route['page_search/(:num)'] = "page_search/index/$1";
Here's the link to the live site http://angkor21.com/
You are posting your search form and making your $where statement, thats cool, but in page 2, is your $where statement working correctly? I think this is where you need to watch out for. Print your generated query in the second page like this:
$this->db->last_query();
You need to somehow pass on these search parameter(s) to the next page to correctly generate the $where condition. For example:
if( $this->input->post(null) ){ #if the form is submitted
$saleType = $this->input->post('sale_type');
$propType = $this->input->post('prop_type');
$city = $this->input->post('city');
$district = $this->input->post('district');
$commune = $this->input->post('commune');
}else{
$saleType = $this->uri->segment(3);
$propType = $this->uri->segment(5);
$city = $this->uri->segment(7);
$district = $this->uri->segment(9);
$commune = $this->uri->segment(11);
}
$search['base_url'] = base_url() . 'page_search/sale_type/'.$saleType.'/prop_type/'.$propType.'/city/'.$city.'/district/'.$district.'/commune/'.$commune.'/page/';
$search['uri_segment'] = 13;

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's pagination offset not working

In CI's pagination, the data index should follow the offset. For example : if the limit is 10 then the 2nd index should have 10 offset, which means the index will start from 11 to 20.
I followed some tutorials, but i still cant get this offset thing works correctly. My index always reseted to 1 each time i click the differet pagination index.
This is my pagination code, note that i have tried to echo the $offset and the value is true (in 2nd index = 10, in 3rd index = 20, with $limit = 10) so i dont know why its not working:
public function index($offset = 0) {
//check authorization
if(!isset($_SESSION['username']))
redirect('backend_umat/login');
// the $offset value is true, but the index is still reseted to 1
echo $offset;
$limit = 10;
$result = $this->umat_m->get_umat($limit, $offset);
//pagination
$config['base_url'] = site_url('/backend_umat/index');
$config['total_rows'] = $result['num_rows'];
$config['per_page'] = $limit;
$config['uri_segment'] = 3;
$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
And this is my model :
public function get_umat($limit, $offset) {
$this->db->select('*')->from('msumat')->limit($limit, $offset)->
join('mskelas', 'msumat.kelas_id = mskelas.kelas_id');
$q = $this->db->get();
$result['rows'] = $q->result();
$result['num_rows'] = $this->db->select('*')->from('msumat')->
join('mskelas', 'msumat.kelas_id = mskelas.kelas_id')
->get()->num_rows();
return $result;
Thanks for your help :D
public function index() {
//check authorization
if(!isset($_SESSION['username']))
redirect('backend_umat/login');
// the $offset value is true, but the index is still reseted to 1
//pagination
$config['base_url'] = base_url().'backend_umat/index';
// basically you need a separate query to return only numrows
$config['total_rows'] = ?;
$config['per_page'] = 10;
$config['uri_segment'] = 3;
$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$offset = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$result = $this->umat_m->get_umat( $config['per_page'], $offset);
$data['pagination'] = $this->pagination->create_links();
Here try this, i changed some code hope this works. Basically i intitialized first the pagination config, before calling anything from the model. Just do a separate query to get the numrows.
Controller
site.com/<controller>/index/<page>
public function index($offset = 0) {
//check authorization
if(!isset($_SESSION['username']))
redirect('backend_umat/login');
$limit = 10;
$offset = (int) $offset;
$result = $this->umat_m->get_umat($limit, $offset);
//pagination
$config['base_url'] = site_url('/backend_umat/index');
$config['total_rows'] = $result['num_rows'];
$config['per_page'] = $limit;
$config['uri_segment'] = 3;
$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
Model
public function get_umat($limit, $offset) {
$result['rows'] = $this->db
->select('SQL_CALC_FOUND_ROWS, msumat.*, mskelas.*', FALSE)
->limit($limit, $offset == 1 ? 0 : $offset)
->join('mskelas', 'msumat.kelas_id = mskelas.kelas_id')
->get('msumat')
->result();
$req = $this->db->query('SELECT FOUND_ROWS()')->row_array();
$result['num_rows'] = $req['FOUND_ROWS()'];
return $result;
}
To not have to rewrite a second sql req, you can use SQL_CALC_FOUND_ROWS to retrieve the total if the request did not contain a LIMIT statement. But this can be slower than two requests.
I used FALSE as second arguments in the select() in order that CI not tries to protect field or table names with backticks.
->select('*'): is useless if you want all fields, CI will do it by default if select method was not called.
->get()->num_rows(): use instead ->count_all_results([table]).
First, thanks to tomexsans and lighta for their help. Sorry, i made a "stupid" mistake - which took about 2-3 hours to realize -, the index is not following the $offset because i FORGET to use that variable. I use an integer that i reset to 1 everytime the page load, so the index will reset to 1 forever :P

help me create pagination file in config

I want to create a pagination file in config folder to using for many pages.
But, i don't known how to set values of $config[], because they belong to data.
eg: $config['total_rows'] = ? $config['base_url'] = ?
please..help me ! thanks !
$this->load->library('pagination');
$this->load->library('table');
$config['base_url'] = base_url().'/site/big/';
$where = "bot = '2' OR bot = '0'";
$this->db->where($where);
$config['total_rows'] = $this->db->count_all_results('visitors');//query here for total rows
$config['per_page'] = 15;
//$config['num_links'] = 20;
$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$this->db->order_by('date', 'DESC');
$where = "bot = '2' OR bot = '0'";
$this->db->where($where);
$this->db->select('id, ip, date, page, host, agent, spammer, country, total, refer');
$data['records'] = $this->db->get('visitors', $config['per_page'], $this->uri->segment(3));
$this->table->set_heading('Id', 'IP', 'Date', 'Page', 'Host', 'Agent', 'Spam', 'Country', 'Total', 'Referer');
$this->load->view('site_view', $data);
Base url is your website url + the controller/function
Per page is how many rows you want to see on each page

Resources