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);
Related
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;
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;
//...
}
I am trying to implement a load more feature in a web app I am building.
When the app loads, it makes an ajax call to controller which retrieves the data from the database and then encodes them in JSON.
I am making use of limits and offsets in mysql query and I more data to be loaded as the user scrolls down.
This is the method in the controller which the ajax call is made to
function latest_pheeds($offset = 0) {
//Confirm if a user is logged before allowing access
if($this->isLogged())
{
//Limit
$limit = 20;
//user id
$user_id = $this->session->userdata('user_id');
//load pheeds
$dt = $this->pheed_model->get_latest_pheeds($limit,$offset);
$data = $dt['pheeds']; //data
$total = $dt['total']; //Total no of rows of data
$return['pheeds'] = $data;
echo json_encode($return); //encode in json
} else {
$this->output->set_status_header('401',"Attempting Unauthorized Access");
}
}
How do I break this data into pages, so I can simply pass the page no as argument to method via the ajax call to implement the load more with jQuery?
Calculate $offset by multiplying (page number - 1) with $limit:
$offset = ($page - 1) * $limit;
So if $page is 1, then it will be (1 - 1) * 20 = 0 * 20 = 0,
if $page is 2, then it will be (2 - 1) * 20 = 1 * 20 = 20
Also rename function parameter $offset to $page
[EDIT]
Also what i would do is to query one row more, than needed, so i can detect if there is next page, eg:
$dt = $this->pheed_model->get_latest_pheeds($limit + 1,$offset);
$next_page = false;
if (count($dt) > $limit) {
$next_page = true;
$dt = array_pop($dt); // Excluding the last row so it is same size as $limit
}
// More code....
$return['next_page_exists'] = $next_page;
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().
I am looking for a php pagination class, I have used a rather simple one in the past and it is no longer supported.
I was wondering if anyone had any recommendations ?
It seems pointless to build my own when there are probably so many good ones out there.
After more searching I decided that before I use a frameworked version I should fully understand what is involved in a paginator. So I built one myself. Thanks for the suggestions though!
I would suggest Zend_Paginator for the following reasons
It's loosely coupled and doesn't require the entire library.
The ZF community is larger than the PEAR community and is actively running security audits on code, and releasing maintenance versions.
It separates data sources by using the Adapter Pattern, and there are numerous examples of front end UI pattern implementations in the documentation.
Have you tried PEAR::Pager? Usage examples here.
you can try this:
Zebra_Pagination, a generic, Twitter Bootstrap compatible, pagination class written in PHP
check the link below:
http://stefangabos.ro/php-libraries/zebra-pagination
// pagination class
class Pagination
{
// database handle
private $dbh;
// total records in table
private $total_records;
// limit of items per page
private $limit;
// total number of pages needed
private $total_pages;
// first and back links
private $firstBack;
// next and last links
private $nextLast;
// where are we among all pages?
private $where;
public function __construct($dbh) {
$this->dbh = $dbh;
}
// determines the total number of records in table
public function totalRecords($query, array $params)
{
$stmt = $this->dbh->prepare($query);
$stmt->execute($params);
$this->total_records = $stmt->fetchAll(PDO::FETCH_COLUMN)[0];
if (!$this->total_records) {
echo 'No records found!';
return;
}
}
// sets limit and number of pages
public function setLimit($limit)
{
$this->limit = $limit;
// determines how many pages there will be
if (!empty($this->total_records)) {
$this->total_pages = ceil($this->total_records / $this->limit);
}
}
// determine what the current page is also, it returns the current page
public function page()
{
$pageno = (int)(isset($_GET['pageno'])) ? $_GET['pageno'] : $pageno = 1;
// out of range check
if ($pageno > $this->total_pages) {
$pageno = $this->total_pages;
} elseif ($pageno < 1) {
$pageno = 1;
}
// links
if ($pageno > 1) {
// backtrack
$prevpage = $pageno -1;
// 'first' and 'back' links
$this->firstBack = "<div class='first-back'><a href='$_SERVER[PHP_SELF]?pageno=1'>First</a> <a href='$_SERVER[PHP_SELF]?pageno=$prevpage'>Back</a></div>";
}
$this->where = "<div class='page-count'>(Page $pageno of $this->total_pages)</div>";
if ($pageno < $this->total_pages) {
// forward
$nextpage = $pageno + 1;
// 'next' and 'last' links
$this->nextLast = "<div class='next-last'><a href='$_SERVER[PHP_SELF]?pageno=$nextpage'>Next</a> <a href='$_SERVER[PHP_SELF]?pageno=$this->total_pages'>Last</a></div>";
}
return $pageno;
}
// get first and back links
public function firstBack()
{
return $this->firstBack;
}
// get next and last links
public function nextLast()
{
return $this->nextLast;
}
// get where we are among pages
public function where()
{
return $this->where;
}
}
Use:
$pagination = new Pagination($dbh);
$pagination->totalRecords('SELECT COUNT(*) FROM `photos` WHERE `user` = :user', array(':user' => $_SESSION['id']));
$pagination->setLimit(12);
$pagination->page();
echo $pagination->firstBack();
echo $pagination->where();
echo $pagination->nextLast();
Result:
<div class='first-back'><a href='/xampp/web_development/new_study_2014/imagebox2016/app/public/test.php?pageno=1'>First</a> <a href='/xampp/web_development/new_study_2014/imagebox2016/app/public/test.php?pageno=3'>Back</a></div>
<div class='page-count'>(Page 4 of 6)</div>
<div class='next-last'><a href='/xampp/web_development/new_study_2014/imagebox2016/app/public/test.php?pageno=5'>Next</a> <a href='/xampp/web_development/new_study_2014/imagebox2016/app/public/test.php?pageno=6'>Last</a></div>
public function make_pagination()
{
$total = 0;
$query = "SELECT COUNT(downloads.dn_id) FROM downloads WHERE downloads.dn_type = 'audios'";
$stmt = $this->conn->prepare($query);
$stmt->execute();
$total = $stmt->fetchColumn();
//echo 'row_count = ' . $total;
// How many items to list per page
$limit = 11;
// How many pages will there be
$pages = ceil($total / $limit);
// What page are we currently on?
$page = min($pages, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array(
'options' => array(
'default' => 1,
'min_range' => 1,
),
)));
// Calculate the offset for the query
$offset = ($page - 1) * $limit;
// Some information to display to the user
$start = $offset + 1;
$end = min(($offset + $limit), $total);
// The "back" link
$prevlink = ($page > 1) ? '« ‹' : '<span class="disabled">«</span> <span class="disabled">‹</span>';
// The "forward" link
$nextlink = ($page < $pages) ? '› »' : '<span class="disabled">›</span> <span class="disabled">»</span>';
// Display the paging information
echo '<div id="paging"><p>'.$prevlink.' Page '.$page.' of '.$pages. ' pages'. $nextlink.' </p></div>';
//prepare the page query
$query2 = "
SELECT * FROM downloads, map_artists, song_artists
WHERE map_artists.dn_id = downloads.dn_id
AND song_artists.artist_id = map_artists.artist_id
AND downloads.dn_type = 'audios' GROUP BY downloads.dn_id
ORDER BY downloads.dn_time DESC LIMIT :limit OFFSET :offset ";
$stmt2 = $this->conn->prepare($query2);
$stmt2->bindParam(':limit', $limit, PDO::PARAM_INT);
$stmt2->bindParam(':offset', $offset, PDO::PARAM_INT);
$stmt2->execute();
// Do we have any results?
if ($stmt2->rowCount() > 0) {
// Define how we want to fetch the results
$stmt2->setFetchMode(PDO::FETCH_ASSOC);
$iterator = new IteratorIterator($stmt2);
// Display the results
foreach ($iterator as $row) {
echo '<p>'. $row['dn_title'].' - '. $row['artist_name'].'</p>';
}
} else {
echo '<p>No results could be displayed.</p>';
}
}
Its Very possible that your SQL SELECT statement query may 1000 result into thousand of records. But its is not good idea to display all the results on one page. So we can divide this result into many pages as per requirement as pagination Class .
PAGINATE DATA WITH PAGINATION CLASS VERY EASY
pagination Class helps to generate paging
How To Use Pagination Class
visit this link for more info
http://utlearn.com/2017/02/15/pagination-class-use-pagination-class/
<?php
/**
* #package pagination class
* #version 1.0
*/
/*
#class Name: pagination
#Author: Ahmed Mohamed
#Version: 1.0
#Author URI: https://www.fb.com/100002349977660
#Website URI: http://www.utlearn.com
#class page URI: http://utlearn.com/2017/02/15/pagination-class-use-pagination-class
*/
include_once 'libs/config.php';
include_once 'libs/Database.php';
include_once 'libs/Model.php';
include_once 'libs/pagination.php';
if(!empty($_GET["page"]) and is_numeric($_GET["page"])){
$page = htmlspecialchars(strip_tags($_GET["page"]));
} else {
$page = 1;
}
// news = table name / you page URL / current page / true or false for full query
// its false i just use table name
$pag = new pagination("news", URL."?page=", 3, $page, false);
$pagination = $pag->pagination();
$data = $pag->data();
?>
<news>
<?php foreach($data as $news){ ?>
<header><h1><?=$news->title ?></h1> | <span><?=$news->date ?></span></header>
<div>
<?=$news->content ?>
</div>
<?php } ?>
</news>
<?=$pagination ?>