codeigniter pagination with dynamic url structure - php

In my codeigniter app i have an article module, which has three categories,
primary category
secondary category
tertiary category
list of articles
Now the first page shows an accordion with the heirarchy above except the list of articles, the user can click on expand or click on a category name directly.
So if a user clicks on primary category name all the articles will be listed paginated based on codeigniter's inbuilt pagination library.
Likewise if a tertiary category is clicked articles in that alone will be listed. The problem is all the articles listing is handled by a single function and hence i have a route like this.
$route['article/(:any)/(:any)/(:any)/(:any)'] = "articles/articles/show_article/$4";
$route['article/(:any)/(:any)/(:any)'] = "articles/articles/find_type/$3";
$route['article/(:any)/(:any)'] = "articles/articles/find_type/$2";
$route['article/(:any)'] = "articles/articles/find_type/$1";
and my find_type method is
function find_type($str)
{
$rawstr = $str;
$str = deurl($str);
$ch = 1;
$id = 0;
$query = $this->db->get_where("articles_primary",array("primary_name"=>$str));
if($query->num_rows==1) {
$ch = 1;
$id = $query->row('id');
}
$query = $this->db->get_where("articles_secondary",array("secondary_name"=>$str));
if($query->num_rows==1) {
$ch = 2;
$id = $query->row('id');
}
$query = $this->db->get_where("articles_tertiary",array("tertiary_name"=>$str));
if($query->num_rows==1) {
$ch = 3;
$id = $query->row('id');
}
$query = $this->db->get_where("articles_list",array("url_title"=>$rawstr));
if($query->num_rows==1) {
$ch = 4;
}
switch ($ch)
{
case 1:
$this->show_articles_list($id,"primary",$rawstr);
break;
case 2:
$this->show_articles_list($id,"secondary",$rawstr);
break;
case 3:
$this->show_articles_list($id,"tertiary",$rawstr);
break;
case 4:
$this->show_article($rawstr);
break;
}
}
and finally my list articles is
function show_articles_list($id,$tbl,$rawstr)
{
$query = $this->db->get_where("articles_list",array($tbl."_id"=>$id));
$this->load->library('pagination');
$config['base_url'] = current_url();
$config['total_rows'] = $query->num_rows;
$config['per_page'] = 9;
$this->pagination->initialize($config);
$page = $this->uri->segment(3);
if($page=="") { $page = 0; }
$this->db->limit(9,$page);
$data["query"] = $this->db->get_where("articles_list",array($tbl."_id"=>$id));
$this->template->write_view('maincontent', 'articles/articles_list',$data);
//$this->template->write_view('subcontent', 'articles/related_articles',$data);
$this->template->write("title","Laws",true);
$this->template->write_view('rightcontent','general/include/query_document_tab');
$this->template->render();
}
as you can see the routes are based on uri segments and hence the pagination is not working. Is there anyway i can tweak the pagination to work with the current setup i have?

How about adding another parameter to the functions and changing the routes file accordingly:
function find_type($str, $page=0){
...
$this->show_articles_list($id,"primary",$rawstr, $page);
etc...
...
}
function show_articles_list($id,$tbl,$rawstr,$page)...
Routes:
$route['article/(:any)/(:any)/(:any)/(:any)'] = "articles/articles/show_article/$4/4";
$route['article/(:any)/(:any)/(:any)'] = "articles/articles/find_type/$3/3";
$route['article/(:any)/(:any)'] = "articles/articles/find_type/$2/2";
$route['article/(:any)'] = "articles/articles/find_type/$1/1";
You'd use that parameter to denote the uri segment for pagination.
OR
You can just use $this->uri->total_segments().

Related

Pagination not working on my application - CodeIgniter

I have been able to develop a pagination for my page which contains about 25000 records. The page paginates in 100's but for now, when i click on the pagination link to move to the next page, it leads me to the first page again. But the URI on my browser shows the per page number like customers/100 . What could i be doing wrong below
Controller
$config['base_url'] = base_url() . 'customers/index/';
$config["total_rows"] = $customers
$config["per_page"] = 100;
$config['num_links'] = 10;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data['items'] = $this->customer->get_customer_all($config["per_page"]);
Model
public function get_customer_all($limit = null) {
$this->db->select('*');
$this->db->from('courses');
if($limit!=''){
$this->db->limit($limit);
}
$query = $this->db->get();
return ($query) ? $query->result() : false;
}
Why do you not use $page variable?
For getting data partially you need both limit and offset.
limit for count of customers per page and offset for understanding what page you are on.
So try to change you request to model like this:
$data['items'] = $this->customer->get_customer_all($config["per_page"], $page);
And update model like this (with some changes):
public function get_customer_all($limit = 0, $offset = 0) {
return $this->db->select('*')->
from('courses')->
limit($limit)->
offset($offset)->
get()->
result_array();
}

Pagination with codeigniter URI issue

This is my first time working with pagination in codeigniter and I'm a little confused. I believe my problem may have something to do with the URI segments offset variable.
Here's my controller. I have replaced the $config["base_url"] with the full URL so you can see how many URI segments I have.
My controller
$gutterId = $this->gutter->convertGutterNameToId($name);
$this->load->library("pagination");
$config = array();
$config["base_url"] = "http://localhost/gutter/g/random/"; //base_url() . "/g/$name";
$config["total_rows"] = $this->gutter->countThreadRows($gutterId);
$config["per_page"] = 1;
$config["uri_segment"] = 5;
$this->pagination->initialize($config);
$limit = 1;
$offset = ($this->uri->segment(5)) ? $this->uri->segment(5) : 0;
$data['threads'] = $this->gutter->grabThreads($limit, $offset, $gutterId);
$data['title'] = $name;
echo $this->pagination->create_links();
And my model.
public function grabThreads($limit, $offset, $gutterId){
$query = $this->db->limit($limit, $offset)->order_by('thread_id', 'DESC')->get_where('threads', array('thread_sub_gutter' => $gutterId));
return $query->result();
}
So this is giving me one result on the http://localhost/gutter/g/random/ page which leads me to believe the query is working correctly. However, when I navigate to http://localhost/gutter/g/random/1 I get the following 404 error
The page you requested was not found.
You will need to route the request to your controller.
Should be something like this:
$route['g/(:any)/(:any)'] = 'g/index/$1/$2';
Also if your page number is going to be in the third segment, do this:
$config[‘uri_segment’] = 3;

codeigniter pagination link is not working when i select the link

Request yo to help in pagination links .
In my database i have 3 records i want to display single record per page. When I select the next numeric of pagination link, data is not being fetched.Thing is that when I click on number 2 of pagination link, echo var_dump() shows Result is empty and I am not getting any values for echo $data->email.But for the first time when i search i am able to display single record, problem is only with next link of pagination So what might be the error? I'm not able to get an answer,and I'm not sure what happens, so I am posting my code below please go through it and and help me.
Request you to help me.
**HERE STARTS MY CONTROLLER**
public function users($limit=1,$offset = 0)
{
$this->load->helper('url');
$data = array();
$look = $this->input->post('look');
$age = $this->input->post('age');
$age_from = $this->input->post('age_from');
$age_to = $this->input->post('age_to');
$se_ct = $this->input->post('sect');
$subsect = $this->input->post('subsect');
$coun_try = $this->input->post('country');
$sta_te = $this->input->post('state');
$ci_ty = $this->input->post('city');
$qualification = $this->input->post('qualification');
$results = $this->searchresultss->login($look, $age, $age_to, $age_from, $se_ct, $subsect, $coun_try, $sta_te, $ci_ty, $qualification);
$this->load->helper('url');
$config = array();
$config['base_url'] = base_url().'searchresult/users';
$config['total_rows'] = count($results);
$config['per_page'] = $limit;
$this->load->library('pagination', $config);
$data['pagination_links'] = $this->pagination->create_links();
$data['results'] = array_slice($results, $offset, $limit);
$this->load->view('searchresult', $data);
$this->load->view('includes/khelp');
$this->load->view('includes/kfooter');
**HERE STARTS MY MODEL PAGE**
Class Searchresultss extends CI_Model
{
public function login($look, $age, $age_to, $age_from, $se_ct, $subsect, $coun_try, $sta_te, $ci_ty, $qualification)
{
return $this->db->query("SELECT *
FROM users
WHERE gender = '$look'
And status='1'")->result();
}
}
**HERE START MY VIEW PAGE**
echo var_dump($_POST);
if (empty($results)) {
echo 'Results set is empty';
} else
{
foreach ($results as $data) {
echo $data->email.'<br />';
}
}
echo $pagination_links;
The problem lies in the fact that the pagination links do not include the POST variables (as well as the fact that hyper links are requested via GET).
I recommend you do a var_dump() on $_GET and $_POST and the problem will become more obvious.
A possible solution would be to include the post variables as url parameters. So for example
$config['base_url'] = base_url().'searchresult/users/look_param/age_param/etcetc';
However you would need to add functionality to handle the above.

CodeIgniter pagination always shows page 1

I already using the pagination library in nearly ten modules, with no problems, but it fails in last one (and the most important).
My routing for this section is:
$route['candidate/sort/(:any)/(:any)/page/(:num)'] = 'candidate/sort/$1/$2/$3';
My Controller
public function sort($type, $id, $page = 1) {
/* Load Config */
$data = $this->data;
$data['sub_active'] = 'candidate';
$data['type'] = $type;
/* Get Candidates */
$total = $this->candidates->getTotal($type, $id);
if(($this->limit >= $total) && ($page > 1)) {
$data['candidates'] = $this->candidates->getCandidates(1, $this->limit, $type, $id);
}elseif(((($this->limit * $page) - $this->limit) >= $total) && ($page > 1)) {
$data['candidates'] = $this->candidates->getCandidates(ceil($total / $this->limit), $this->limit, $type, $id);
}else{
$data['candidates'] = $this->candidates->getCandidates($page, $this->limit, $type, $id);
}
/* Pagination */
$this->load->library('pagination');
# Config Pagination
$data['cms']['tables']['total_rows'] = $total;
$data['cms']['tables']['per_page'] = $this->limit;
$data['cms']['tables']['first_url'] = base_url($data['sub_active'].'/sort'.'/'.$type.'/'.$id);
$data['cms']['tables']['base_url'] = base_url($data['sub_active'].'/sort'.'/'.$type.'/'.$id.'/page');
$data['page'] = $page;
$data['total_pages'] = ceil($total / $this->limit);
$data['total'] = $total;
# Initialize Pagination
$this->pagination->initialize($data['cms']['tables']);
$data['pagination'] = $this->pagination->create_links();
/* Display Template */
$this->twig->display('pages/list_candidate.htm', $data);
}
Base first url = myweb.com/candidate/sort/$type/$id and base url = myweb.com/candidate/sort/$type/$id/page
But the pagination doesn’t work, it always the same page (page 1 on this case). I'm using this same schema in other controllers and it works fine, only fails with this.
Thanks in advance.
I finally found the answer:
$config['uri_segment'] = 6;
That's because codeigniter does not detect well the URL.
Nice, I am glad you found the answer Tunnecino!
But I think its need to be further explained , why
According to our wonderful CI user guide, here is how we determine the uri_segment so not to make the same mistake again :)
$this->uri->segment(n)
Permits you to retrieve a specific segment. Where n is the segment number you wish to retrieve. Segments are numbered from left to right. For example, if your full URL is this:
http://example.com/index.php/news/local/metro/crime_is_up
The segment numbers would be this:
1.news
2.local
3.metro
4.crime_is_up
By default the function returns FALSE (boolean) if the segment does not exist. There is an optional second parameter that permits you to set your own default value if the segment is missing. For example, this would tell the function to return the number zero in the event of failure:
$product_id = $this->uri->segment(3, 0);

In Codeigniter Pagination's generated page links, page 1 is always selected

I'm about to pull my hair over this!
On initial load of my page with pagination (by CI), all rows are displayed, even if I only want 3. On click of other pages, however, it works fine (the correct rows are displayed), but Page 1 is always "selected" (not clickable), even if I click on Page 2, 3, etc.
Any ideas?
My CONTROLLER:
function album($type, $album_id, $album_name) {
$this->load->library('pagination');
$config['base_url'] = base_url("photo_store/album/$type/$album_id/$album_name/");
$config['total_rows'] = $this->Media_model->get_photos($album_id, 'display_date DESC', NULL, NULL, TRUE);
$config['per_page'] = 3;
$this->pagination->initialize($config);
$album_photos = $this->Media_model->get_photos($album_id, 'display_date DESC', $config['per_page'], $this->uri->segment(6), FALSE);
$this->_load_view(array(
/* some other variables here */
'album_photos' => $album_photos
));
)
private function _load_view($more_data) {
$data = array_merge($more_data, array( /* some other variables here */ ));
$this->load->view('template', $data);
}
My MODEL:
public function get_photos($album_id=NULL, $order_by='display_date DESC', $limit=NULL, $offset=NULL, $count=FALSE) {
$result = array();
$query = $this->db->select('medium.*')->join('medium', "$this->item.medium_id = medium.id", 'inner')->order_by($order_by);
$limit = $limit ? $limit : '0';
$offset = $offset ? $offset : '0';
if ($limit!=='0' && $offset!=='0') {
$query->limit($limit, $offset);
}
if ($album_id) { $result = $query->get_where($this->item, array('album_id' => $album_id)); }
else { $result = $query->get($this->item); }
if ($count){ return $result->num_rows(); }
else { return $result->result(); }
}
My VIEW:
foreach ($album_photos as $photo) {
//display photos here
}
echo $this->pagination->create_links();
You can just add this to the config array so the pagination knows where to find the current page:
$config['uri_segment'] = 4;
I believe part of the problem is coming in here:
if ($limit!=='0' && $offset!=='0') {
$query->limit($limit, $offset);
}
Since you don't have an else part for your statement, the query is never limited for that first page.
I suggest you change that code to
if ($limit!=='0') {
$query->limit($limit, $offset);
}
or even just
$query->limit($limit, $offset);
since $limit should theoretically never be null or 0 because you've set it to 3. $offset, unless set, should be 0 so you could replace null with it in your model's function,
When there are more than 3 uri segments passed in the url, selected page of pagination will not be displayed correctly, it will highlight the first page all the time.
Pagination is working, but the selected page is not diplayed correctly.
To solve this, solution:
go to Pagination.php file which is located at system->libraries->Pagination.php
just simply set
var $uri_segment = 4;// or 5 or 6;
It will work.
You can just add this to the config array so the pagination knows where to find the current page:
$config['uri_segment'] = 4; // Your appropriate uri segment: 5 or 6
Try to code your controller like below:
public function index($page=''){
//...
$page = ($page!='')? $page : 0;
$config["cur_page"] = $page;
//...
}

Categories