I am trying to add a simple pagination to an image gallery but I reach a point where I don't get any error messages and it still is not working. I don't know where to look for a mistake.
My code is;
<?php
include 'core/init.php';
ini_set('display_errors', 'On'); // aan of uit
error_reporting(E_ALL);
?>
<body>
<div id="wrap">
<div id="header">
<h1>Rob Cnossen</h1>
</div>
<div id="titel">
<?php
if (isset($_GET['album_id'])) {
$album_id = $_GET['album_id'];
$album_data = $albums->album_data($album_id, 'name', 'description');
echo '<h2>', $album_data['name'], '</h2>';
$albums = $albums->get_albums();
$images = $images->get_images($album_id);
}
?>
</div>
<div id="sidebarleft">
<?php
if (empty($images)) {
echo 'Er zijn geen foto\'s in dit album';
} else {
foreach ($albums as $album) {
foreach ($images as $image) {
?><div id="fotoos"><?php
if ($image["album"] === $album["id"])
echo'<img src="uploads/thumbs/', $image["album"], '/', $image["img_name"],'" title="" /><div id="kruisje">_|</div>';
?></div><?php
}
}
}
?>
<div id="pagination">
<?php
$count_query = $db->prepare('SELECT * FROM images where album_id= ?');
$count_query->bindValue(1, $album_id);
try{
$count_query->execute();
}catch (PDOException $e){
die($e->getMessage());
}
$count = $count_query->fetchColumn();
if(isset($_GET['page'])){
$page = preg_replace("#[^0-9]#,",$_GET['page']);
}else{
$page = 1;
}
$limit = 2;
$lastPage = ceil($count/$limit);
if($page<1){
$page = 1;
}elseif($page>$lastPage){
$page = $lastPage;
}
$offset = ($page-1)*$limit;
$query = $db->prepare('SELECT image_id FROM images WHERE album_id= ? ORDER BY image_id DESC
LIMIT ?,?');
$query->bindValue(1, $album_id);
$query->bindParam(2, $offset, PDO::PARAM_INT);
$query->bindParam(3, $limit, PDO::PARAM_INT);
try{
$query->execute();
}catch (PDOException $e){
die($e->getMessage());
}
if($lastPage !=1){
if($page != $lastPage){
$next = $page + 1;
$pagination='Volgende';
}
if($page != $lastPage){
$prev = $page - 1;
$pagination.='Vorige';
}
}
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
echo $row['image_id'];//This echoes out two image id's, at the moment that is number 75 and 73
}
echo $pagination;
?>
</div>
</div>
</div>
</body>
The get_image function is;
public function get_images($album_id) {
$images = array();
$count_query = $this->db->prepare("SELECT `image_id`, `image_name`, `album_id`, `timestamp`, `ext` FROM `images` WHERE `album_id`=? ORDER BY `timestamp` DESC");
$count_query->bindValue(1, $album_id);
try{
$count_query->execute();
while ($images_row = $count_query->fetch(PDO::FETCH_ASSOC)) {
$images[] = array(
'id' => $images_row['image_id'],
'img_name' => $images_row['image_name'],
'album' => $images_row['album_id'],
'timestamp' => $images_row['timestamp'],
'ext' => $images_row['ext']
);
}
return $images;
}catch(PDOException $e){
die($e->getMessage());
}
}
I think there is something wrong in the $pagination variable but I don't know what.I got seven images in this album and the $limit is standing on 2, still all seven images is showing up. And if I click on a pagination link the url shows my that I clicked on a pagination link but that's all. If I click for example vife times on a next or previous link the url can show my this;http://www.robcnossen.nl/view_album.php?album_id=8?page=2?page=2?page=0?page=2?page=0
The site is http://www.robcnossen.nl/view_album.php?album_id=8
I hope you can see what causes the problem.
Thanks...
If I were you I would either download a working solution from the net and adapt it to your particular situation OR refactor the whole thing taking into consideration some SoC (Separation of Concerns).
Separate your concerns, leave the logic out of the presentation
Separate the the pagination logic from the rest. Pagination should only need to know at most 4 things to show its links
the target URL: the links need a homogeneous url scheme, only a pageNum in the url will change
how many links have to show up (probably an array of pageNums containing the variable part)
[optional] the current ID being viewed, probably to blur out a page
[optional] logic behind first/last, and behind prev/next
Help yourself by cleaning up your code and also encapsulating some functionality. For example:
function html_a($href, $content) {
return ''.$content.'';
}
function pag_href($url, $id) {
return sprintf($url,$id);// url: host/target.php?pageNum=%s&someOtherstuff...
}
// usage inside loop
echo html_a(pag_href($url,$id),$pageNum);
// if you do this right, your pagination code can become reusable
use some mysql features to your advantage. You can limit the number of results with LIMIT, set the first element of the page with OFFSET, and still get the total of number of rows with SQL_CALC_FOUND_ROWS
// This way is clearer for me
$q = 'SELECT SQL_CALC_FOUND_ROWS image_id FROM images WHERE album_id=? ORDER BY image_id DESC LIMIT ? OFFSET ?'
// after executing your query you may now use pdo's foundRows
$totalNumEntries = $pdo->foundRows();
// get number of pages with pageSize
$totalNumPages = (int)ceil($totalNumEntries / $yourPageSize);
All in all, try to practice some encapsulation/separation of concerns, do some planning ahead and you won't have to deal with code that not even you want to debug
I'm sorry this does not help with your existing code
Related
i have a PHP code that creates a team page with team member boxes on my website. The code for every team meber is fetched from the database, and it works fine, but time has passed and we are many now. So many, that I need to display only 5 team members per section. Like 5 team members in one line, the next 5 in the other line, .. - I tried a few things to do that, but I got stuck. Sometimes members were missing, sometimes he created many lines with the same members, and so on. That's why I'm asking here. I already searched a bit but couldn't find anything that helps me.
Currently, my code creates a new <section> for ALL team members. But that's not what I need. It should create a section for 5 members, the next section for the next 5 members, and so on. How can I do that?
(btw I'm a complete beginner in PHP and want to learn something from it so you can improve my code if you want.)
function team_helfer() {
$db = new mysqli("ip", $username, $password, $database);
$result = mysqli_query($db, "SELECT code FROM team_data WHERE rank = 'HELFER'");
$team_list = "";
$count = 0;
while ($row = mysqli_fetch_assoc($result)) {
foreach ($row as $field => $value) {
$team_list .= "$value";
++$count;
}
}
if ($count == 2) {
$code = '<section class="elementor-section elementor-inner-section elementor-element elementor-section-boxed elementor-section-height-default elementor-section-height-default" data-element_type="section">
<div class="elementor-container elementor-column-gap-default" style="left: 17%;">
'.$team_list.'
</div>
</section>';
} elseif ($count == 1) {
$code = '<section class="elementor-section elementor-inner-section elementor-element elementor-section-boxed elementor-section-height-default elementor-section-height-default" data-element_type="section">
<div class="elementor-container elementor-column-gap-default" style="left: 33%;">
'.$team_list.'
</div>
</section>';
} else {
$code = '<section class="elementor-section elementor-inner-section elementor-element elementor-section-boxed elementor-section-height-default elementor-section-height-default" data-element_type="section">
<div class="elementor-container elementor-column-gap-default">
'.$team_list.'
</div>
</section>';
}
mysqli_free_result($result);
$db->close();
return $code;
}
add_shortcode( 'team_helfer', 'team_helfer' );
Code from HTMHell's answer:```php
function team_helfer() {
$db = new mysqli("ip", $username, $password, $database);
$result = mysqli_query($db, "SELECT code FROM team_data WHERE rank = 'HELFER'");
$rows = mysqli_fetch_all($result, MYSQLI_ASSOC);
$sections = array_chunk($rows, 5);
foreach ($sections as $section) {
$teamList = implode(",", array_map(function($row) {
return $row['code'];
}));
$code = '<section class="elementor-section elementor-inner-section elementor-element elementor-section-boxed elementor-section-height-default elementor-section-height-default" data-element_type="section">
<div class="elementor-container elementor-column-gap-default">'.$teamList.'</div></section>';
}
mysqli_free_result($result);
$db->close();
return $code;```
Sadly that doesn't help, all elements are vanished now, i can't see them.
I think you are looking for array_chunk.
$rows = mysqli_fetch_all($result, MYSQLI_ASSOC);
$sections = array_chunk($rows, 5);
foreach ($sections as $section) {
$teamList = implode(",", array_map(function($row) {
return $row['code'];
}, $section));
echo "<section>$teamList</section>";
}
Here's a live example:
https://3v4l.org/mXRtV
This is with Apache 2.2.23, PHP 7 and MySQL 5.6.32. The hosting environment is a Bluehost Shared hosting Web Server.
Here is the database connection function:
function connectBlogDatabase() {
$server = 'localhost';
$dbname = 'database_name';
$username = 'database_user';
$password = '1234';
$dsn = 'mysql:host=' . $server . ';dbname=' . $dbname;
$options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
try {
$link = new PDO($dsn, $username, $password, $options);
} catch (Exception $ex) {
$_SESSION['error'] .= $ex;
}
return $link;
}
Here is the query function:
function getMostRecentArticles(int $quantity){
$connection = connectBlogDatabase();
try{
$sql = "SELECT blog_title, blog_body, blog_description, blog_date FROM blog WHERE published = 1 ORDER BY blog_id DESC LIMIT :quantity;";
$stmt = $connection->prepare($sql);
$stmt->bindParam(':quantity', $quantity, PDO::PARAM_INT);
$stmt->execute();
$blogs = $stmt->fetchAll();
$stmt->closeCursor();
return $blogs;
} catch (Exception $ex) {
echo $ex;
$_SESSION['error'] .= "ERROR: Caught Exception \$ex";
return FALSE;
}
}
The query function is used in the following display function. This is simply designed to display the n most recently published blog article links while alternating the css background-color property:
function blog_article_links($quantity){
//get a list of the $quantity most recently written blog articles
$articles = getMostRecentArticles($quantity);
$i = 1;
do{
if($i % 2 === 0){
//even numbered blog post
echo "<li class='blog-nav-li flex-box-blog-link background-
1'>";
} else{
echo "<li class='blog-nav-li flex-box-blog-link background-
2'>";
}
echo "<a class='nav-article-link'
href='view/blog_template.php'>";
echo "<h4 class='nav-article-header'>" . $articles['blog_title']
. "</h4>";
echo "<p class='nav-article-description'>" .
$articles['blog_description'] . "</p>";
echo "<footer class='article-footer'>";
echo "<div class='article-footer-social'>";
echo "<a class='twitter-share-button tweet'
href='https://twitter.com/intent/tweet'>Tweet</a>";
echo "</div><!-- facebook share button-->";
echo "<div class='fb-share-button fb-share' data-
href='https://developers.facebook.com/docs/plugins/' data-
layout='button' data-size='small' data-mobile-iframe='true'><a
class='fb-xfbml-parse-ignore' target='_blank'
href='https://www.facebook.com/sharer/sharer.php
u=https%3A%2F%2Fdevelopers.facebook
.com%2Fdocs%2Fplugins%2F&src=sdkpr
eparse'>Share</a></div>";
echo "<time datetime='".$articles["blog_date"] . "'
class='article-footer-date'>" . $articles['blog_date'] . "</time>";
echo "</footer></a></li>";
$i += 1;
}while($i <= $quantity);
}
When I run the blog_article_links() function, no error is thrown, and $ex is not displayed. When I use var_dump() inside blog_article_links(), I get the following:
" ["blog_description"]=> string(46) "Lorem Ipsum Delta Theda Alpha Omega Pizza Pie." [2]=> string(46) "Lorem Ipsum Delta Theda Alpha Omega Pizza Pie." ["blog_date"]=> string(10) "2017-08-10" [3]=> string(10) "2017-08-10" } }
I've used various different parameters in the fetchAll() method, including PDO::FETCH_NUM, explicitly using the default PDO::FETCH_BOTH, and once even used PDO::FETCH_ASSOC just for the hell of it, to see what would change.
None of these created the results I need. I think it has something to do with the
FROM blog WHERE published = 1 ORDER BY blog_id DESC LIMIT :quantity;
portion of the query. I think PDO is fucking it up because PDO doesn't know how to deal with the ORDER BY and DESC LIMIT keywords.
Now I know for a fact the connectBlogDatabase() function works. There is another query function using this same connectBlogDatabase() function for its PDO object, and that function returns the expected results.
Also, the database user used by connectBlogDatabase() only has SELECT, INSERT, UPDATE, and DELETE permissions.
When you call $articles = getMostRecentArticles($quantity); in your blog_article_links function, $articles gets the value of the $stmt->fetchAll(); expression in that function.
$articles will be a multidimensional array, where each inner array is a row from the query results. So when you do $articles['blog_title'], $articles['blog_description'], etc., those are undefined indexes.
There are different ways to fix this. One way would be to separate your blog_article_links function into two functions, one that prints out a single article and another that iterates the list returned from the database and calls the single output function for each one.
Get the list and output it with one function
function blog_article_links($quantity) {
//get a list of the $quantity most recently written blog articles
$articles = getMostRecentArticles($quantity);
$i = 1;
foreach ($articles as $article) {
if ($i % 2 === 0){
//even numbered blog post
echo "<li class='blog-nav-li flex-box-blog-link background-1'>";
} else {
echo "<li class='blog-nav-li flex-box-blog-link background-2'>";
}
blog_article_link($article);
echo "</li>";
$i += 1;
}
}
The helper function formats and outputs a single article
function blog_article_link($article) {
echo "<a class='nav-article-link' href='view/blog_template.php'>
<h4 class='nav-article-header'>$article[blog_title]</h4>
<p class='nav-article-description'>$article[blog_description]</p>
<footer class='article-footer'>
<div class='article-footer-social'>
<a class='twitter-share-button tweet' href='https://twitter.com/intent/tweet'>Tweet</a>
</div>
<!-- facebook share button-->
<div class='fb-share-button fb-share' data-href='https://developers.facebook.com/docs/plugins/' data-layout='button' data-size='small' data-mobile-iframe='true'>
<a class='fb-xfbml-parse-ignore' target='_blank' href='https://www.facebook.com/sharer/sharer.php u=https%3A%2F%2Fdevelopers.facebook.com%2Fdocs%2Fplugins%2F&src=sdkpreparse'>
Share
</a>
</div>
<time datetime='$article[blog_date]' class='article-footer-date'>
$article[blog_date]
</time>
</footer>
</a>";
}
I figured out how to get it right, but I still don't know why it went wrong in the first place.
I only had 2 blog articles stored in the database. I inserted a 3rd blog article and refreshed the web page. It worked.
That's all I had to do, but that still doesn't explain the fact that it wasn't previously showing up in the developer view in-browser.
I'm working on a simple vlog site project and I'm trying to show only 20 video thumbnail per page and I've wrote this code in the index to divide the videos to multiple pages and then pagination them ... the problem is that it shows the same first video's thumbnail 20 times per page for infinity pages.
I really need help with this code
<?php
require_once ('db.php') ;
require_once ('VideosApi.php') ;
$count = mysql_query('SELECT COUNT(id) AS numb FROM videos ORDER BY id');
$array = mysql_fetch_assoc($count);
$number = $array['numb'];
mysql_free_result($count);
$PerPage = 20;
$nbPage = ceil(abs($number/$PerPage));
if(isset($_GET['page']) && $_GET['page'] > 0 && $_GET['page'] <= $nbPage && preg_match('#^[0-9]+$#',$_GET['page'])){ $cPage = $_GET['page']; }
else{ $cPage = 1; }
$Query = mysql_query('SELECT * FROM Videos ORDER BY id LIMIT '.(($cPage-1) * $PerPage).','.$PerPage);
$videos = Videos_Get() ;
if ($videos == Null)
die ('problem');
$vcount = #count ($videos) ;
if ($vcount == 0)
die('no videos') ;
For ($i = 0; $i < $vcount; $i++)
{
$video = $videos [$i];
if ($video->time > 3600)
$duration = gmdate("H:i:s",$video->time);
else
$duration = gmdate("i:s",$video->time);
while($Rows = mysql_fetch_assoc($Query)){
echo ( "<div class=\"video\">
<img src=\"$video->img\"><span class=\"class-video-name\">$video->name</span>
<div class=\"class-video-footer\">
<span class=\"class-video-duration\">$duration</span>
</div>
</div>") ; }
} ?>
A few tips before we get to the answer proper:
Don't use mysql_*. That family of functions is now deprecated and support will be dropped in future versions of PHP. For code longevity, consider using MySQLi or PDO.
Use is_numeric() to check if a string has a numeric value. Using preg_match() is very load heavy for such a simple task.
Try to avoid using SELECT * inside of MySQL queries. Very rarely do you need everything from the table so fetching all fields for rows is very inefficient (especially if you're not using indexes optimally).
That having been said, I've taken some time to rewrite your code following the practices I've preached above. Below is the modified code, and underneath that an explanation of what was wrong:
Update db.php as follows:
<?php
$db = new PDO( 'mysql:dbname=DATABASE_NAME;host=127.0.0.1', 'DATABASE_USER', 'DATABASE_PASSWORD' );
?>
Now for your main file:
<?php
require_once 'db.php';
require_once 'VideosApi.php';
$count = $db->query( 'SELECT COUNT(id) AS total FROM videos' )->fetchObject();
$number = $count->total;
$perPage = 20;
$pages = ceil( $number / $perPage );
$page = ( isset($_GET['page']) ) ? $_GET['page'] : 1;
$page = ( $page < 1 || $page > $pages || !is_numeric($page) ) ? 1 : $page;
$offset = ($page - 1) * $perPage;
$query = $db->query( 'SELECT id, img, name, time FROM videos ORDER BY id LIMIT '.$offset.','.$perPage );
if( $query->rowCount() < 1 )
{
die( 'No videos' );
}
$html = '';
while( $video = $query->fetchObject() )
{
$duration = ($video->time > 3600) ? gmdate('H:i:s', $video->time) : gmdate('i:s', $video->time);
$html.= '<div class="video">';
$html.= "\n\t".'<a href=\"video.php?id='.$video->id.'">';
$html.= "\n\t\t".'<img src="'.$video->img.'" />';
$html.= "\n\t".'</a>';
$html.= "\n\t".'<span class="class-video-name">'.$video->name.'</span>';
$html.= "\n\t".'<div class="class-video-footer">';
$html.= "\n\t\t".'<span class="class-video-duration">'.$duration.'</span>';
$html.= "\n\t".'</div>';
$html.= "\n".'</div>';
}
echo $html;
?>
The problem with your original code is a little hard to determine since you do not provide the contents of VideosApi.php, which is where I assume you define Videos_Get(). Anyway, what I suspect is happening, is that Videos_Get() is actually ignoring all of your pagination, but as I say, it's difficult to diagnose because we can't see all of your code!
I want to have a previous|next link on my page to change pictures.
I use a function to get relevent elements. However, I do not know what additional code is require in my function and where to place it. Also what should be in the html section.
I have looked at many pages on next/previous from 'foreach' but I cannot seem to relate to them.
Code:
function image_data($image_album_id) {
$image_album__id = (int)$image_album_id;
$args = func_get_args();
unset($args[0]);
$fields = '`'.implode('`, `', $args).'`';
$query = mysql_query("SELECT $fields FROM `album_images`
WHERE `image_album_id`=$image_album_id AND `member_id`= '1'");
$ query_result = mysql_fetch_assoc($query);
foreach ($args as $field) {
$args[$field] = $query_result[$field];
}
return $args;
}
Html Page:
Last|
Next
</div>
<?php
$image_album_id =$_GET['image_album_id'];
$image_data = image_data($image_album_id, 'album_id', 'albumname', 'ext', 'timestamp');
echo '';
?>
<td class="smallfont albumthumb2" align="center" valign="middle" >
<img alt="" class="album_cover" src="<?php echo 'images/albums/thumbs/'. $image_data['album_id']. '/'. $image_album_id. '.' .$image_data['ext'];?> " height="175px" width="175px">
</td>
Many thanks. I hope I make sense.
Thanks for the speedy response.
Since there is a lot to look at and digest I thought I would just see if it works.
Alas no.
There is a parse error: syntax error, unexpected
'<' on line
$prev_link = Previous;
The only thing I notice within that section was an extra curly bracket after 'title="$prev_name"}'
I see there is the same for the 'title="$next_name"}'
WIth reference to your specific questions.
I get to the album_viewT page when I click on a link in a previous page. This contains tiny thumbnails. The link being localhost/Testing/album_view.php?artist_id=4&image_album_id=4 as an example.
Not sure if I fully understand "order of date is by image_album_data
Yes there are almost 3,000 rows in the database.
I should also mention that album_id has been replaced by artist_id.
Should the href be changed to "album_view.php/id/...
Your question lacks some data i.e. what is the page you are visiting that produces this code (the URL that is) and how is your data sorted. For instance I can see that you are providing potentially more information using the album_viewT.php? script but that is not necessarily the one that displays the HTML that you have produced.
So after all this I will make some assumptions and hopefully that will give you the right guidance to get to the solution.
I am assuming that your visiting page is http://mysite.com/albums/id/25
I am also assuming that the order of the data is by image_album_id.
Finally I am assuming that you have data in your db and I don't need to check whether there are returned data in the template for the current image. You will need to sort that out yourself.
You will need to get the data first from the database:
function image_data($image_album_id)
{
$results = array(
'previous' => FALSE,
'current' => FALSE,
'next' => FALSE,
);
$image_album__id = (int)$image_album_id;
$args = func_get_args();
unset($args[0]);
$fields = '`'.implode('`, `', $args).'`';
// Current record
$results['current'] = get_data(
$fields,
"AND image_album_id = {$image_album_id}"
);
// Previous - no need to run this if we don't have a current
if ($results['current'])
{
// Current record
$results['previous'] = get_data(
$fields,
"AND image_album_id < {$image_album_id} " .
"LIMIT 1"
);
}
// Next - no need to run this if we don't have a current
if ($results['current'])
{
// Current record
$results['next'] = get_data(
$fields,
"AND image_album_id > {$image_album_id} " .
"LIMIT 1"
);
}
// If all went well the $results array will contain the data
// for the 3 records. If we are at the beginning or the end,
// the previous and/or next will be FALSE
return $results;
}
function get_data($fields, $where = '')
{
$return = FALSE;
// Template
$template = "SELECT %s "
. "FROM album_images "
. "WHERE member_id = 1 %s";
// Current record
$sql = sprintf($template, $fields, $where);
$query = mysql_query($sql);
$query_result = mysql_fetch_assoc($query);
// If data has been found
if ($query_result)
{
$return = $query_result;
}
return $return;
}
For your HTML page:
<?php
$image_album_id = $_GET['image_album_id'];
$image_data = image_data(
$image_album_id,
'album_id', 'albumname', 'ext', 'timestamp'
);
$prev_link = '';
$next_link = '';
if ($image_data['previous'])
{
$prev_id = $image_data['previous']['album_id'];
$prev_name = $image_data['previous']['albumname'];
$prev_link = <a href="/albums/id/{$prev_id}" title="$prev_name"}>Previous</a>";
}
if ($image_data['next'])
{
$next_id = $image_data['next']['album_id'];
$next_name = $image_data['next']['albumname'];
$next_link = <a href="/albums/id/{$next_id}" title="$next_name"}>Next</a>";
}
$curr_id = $image_data['current']['album_id'];
$curr_ext = $image_data['current']['ext'];
?>
<?php echo $prev_link; ?>|<?php echo $next_link; ?>
</div>
<td class="smallfont albumthumb2" align="center" valign="middle">
<a href="album_viewT.php?images/albums/thumbs/
<?php echo "{$curr_id}/{$image_album_id}.{$curr_ext}; ?>
<img alt="" class="album_cover"
src="images/albums/thumbs/
<?php echo "{$curr_id}/{$image_album_id}.{$curr_ext}"; ?>
height="175px"
width="175px" />
</a>
</td>
Note: I have split the line for the img and a tags in the HTML file for clarity.
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 ?>