I had a php page to display images from a mysql database. It displays all the images uploaded in 1 page one after the other. How can i display 5 or 6 images per page with pagination?
Here is my php page.
<?Php
include("init.php");
include("template/header.php");
?>
<div class="view_albums"><h3> View Albums </h3>;
<?php
$album_id = $_GET['album_id'];
$images = get_images($album_id);
if (empty($images)) {
echo 'There are no images ';
} else {
foreach ($images as $image) {
?> <div class="box"> <?php
echo ' <img class="box1" src="uploads/thumbs/', $image['album'], '/', $image['id'], '.', $image['ext'], '" title="Uploaded on ', date('l F j, Y \a\t g:i A',$image['timestamp']),'"> [delete]';
?> <?php
}
}
?>
</div>
</div>
<?php
include("template/footer.php");
?>
try to use the below pagination function
function genPagination($total,$currentPage,$baseLink,$nextPrev=true,$limit=10)
{
if(!$total OR !$currentPage OR !$baseLink)
{
return false;
}
//Total Number of pages
$totalPages = ceil($total/$limit);
//Text to use after number of pages
$txtPagesAfter = ($totalPages==1)? " page": " pages";
//Start off the list.
$txtPageList = '<br />'.$totalPages.$txtPagesAfter.' : <br />';
//Show only 3 pages before current page(so that we don't have too many pages)
$min = ($page - 3 < $totalPages && $currentPage-3 > 0) ? $currentPage-3 : 1;
//Show only 3 pages after current page(so that we don't have too many pages)
$max = ($page + 3 > $totalPages) ? $totalPages : $currentPage+3;
//Variable for the actual page links
$pageLinks = "";
//Loop to generate the page links
for($i=$min;$i<=$max;$i++)
{
if($currentPage==$i)
{
//Current Page
$pageLinks .= '<b class="selected">'.$i.'</b>';
}
else
{
$pageLinks .= ''.$i.'';
}
}
if($nextPrev)
{
//Next and previous links
$next = ($currentPage + 1 > $totalPages) ? false : 'Next';
$prev = ($currentPage - 1 <= 0 ) ? false : 'Previous';
}
return $txtPageList.$prev.$pageLinks.$next;
}
think it will help you
Related
I want to know how can i add next page, previous page, first page and last page in this pagination. Below code is only showing the page number it doesn't show next page, previous page, first page and last page.
<?php
$pagesToShow = 10;
$pageSize = 20;
$numPages = ceil($numResults / $pageSize);
$pageLefts = min($pagesToShow, $numPages);
$currentPage = $page - floor( $pagesToShow / 2 );
if($currentPage < 1){
$currentPage = 1;
}
if($currentPage + $pageLefts > $numPages + 1) {
$currentPage = $numPages + 1 - $pageLefts;
}
while($pageLefts != 0 && $currentPage <= $numPages) {
if($currentPage == $page){
echo "<div class='pageNumberContainer'>
<span class='pageNumber'>$currentPage</span>
</div>";
}else{
echo "<div class='pageNumberContainer'>
<a href='search.php?term=$term&type=$type&page=$currentPage'>
<span class='pageNumber'>$currentPage</span>
</a>
</div>";
}
$currentPage++;
$pageLefts--;
}
?>
I dont know if this helps .. but maybe you can do something with it :)
<?php $pagesToShow = 10;
$pageSize = 20;
$numPages = ceil($numResults / $pageSize);
$pageLefts = min($pagesToShow, $numPages);
$currentPage = $page - floor( $pagesToShow / 2 );
if($currentPage < 1){
$currentPage = 1;
}
if($currentPage + $pageLefts > $numPages + 1) {
$currentPage = $numPages + 1 - $pageLefts;
}
if ($numPages > 1){echo 'FirstPage = 1';}
while($pageLefts != 0 && $currentPage <= $numPages) {
if($currentPage == $page){
echo "<div class='pageNumberContainer'>
<span class='pageNumber'>Current Page Number Is: ".$currentPage</span>
</div>";
}else{
if ($currentPage != 1){$previousPage = $currentPage - 1; echo
'PREVIOUS PAGE:' . $previousPage; }
echo "<div class='pageNumberContainer'>
<a href='search.php?term=".$term."&type=".$type."&page=".$currentPage."'>
<span class='pageNumber'>CURRENT PAGE: ".$currentPage." </span>
</a>
</div>";
if ($currentPage != $numPages){$nextPage = $currentPage + 1; echo 'NEXT PAGE:' . $nextPage; }
}
$currentPage++;
$pageLefts--;
}
if ($numPages > 1){echo 'LastPage = '.$numPages;}
?>
I'm trying to build a site that has a photo gallery and rather than build a database CMS I'm trying it with the use of PHP and folders. At the moment I have a script to get all of the images in a folder and display them on a page, however as there are going to be probably in excess of 100 photo's I'd like to use pagination to spllit this over several pages but I have no idea how to do this.
Here is the script I'm currently running:
<?php
$folder = 'cms/gallery/photo/';
$filetype = '*.*';
$filename = HOW DO I GET THE NAME WITHOUT FILE TYPE
$files = glob($folder.$filetype);
foreach ($files as $file)
{
echo '
<div class="galleryCellHolder">
<div class="galleryCell">
<a class="fancybox" rel="group" href="'.$file.'"><img class="galleryPhoto" src="'.$file.'" alt="'.$filename.'"></a>
</div>
</div>
';
}
?>
Q1 - How do I extract the file name without the file extension?
Q2 - How do I paginate this for say 24 images per page?
For paging you must calculate the total items to page , capture the parameter of the current page and iterate over the respective range.
<?php
$folder = 'cms/gallery/photo/';
$filetype = '*.*';
$files = glob($folder.$filetype);
$total = count($files);
$per_page = 6;
$last_page = (int)($total / $per_page);
if(isset($_GET["page"]) && ($_GET["page"] <=$last_page) && ($_GET["page"] > 0) ){
$page = $_GET["page"];
$offset = ($per_page + 1)*($page - 1);
}else{
echo "Page out of range showing results for page one";
$page=1;
$offset=0;
}
$max = $offset + $per_page;
if($max>$total){
$max = $total;
}
You can use the function pathinfo to get the file name without extension.
//print_r($files);
echo "Processsing page : $page offset: $offset max: $max total: $total last_page: $last_page";
show_pagination($page, $last_page);
for($i = $offset; $i< $max; $i++){
$file = $files[$i];
$path_parts = pathinfo($file);
$filename = $path_parts['filename'];
echo '
<div class="galleryCellHolder">
<div class="galleryCell">
<a class="fancybox" rel="group" href="'.$file.'"><img class="galleryPhoto" src="'.$file.'" alt="'.$filename.'"></a>
</div>
</div>
';
}
show_pagination($page, $last_page);
Using the following function you can create the navigation links
function show_pagination($current_page, $last_page){
echo '<div>';
if( $current_page > 1 ){
echo ' <<Previous ';
}
if( $current_page < $last_page ){
echo ' Next>> ';
}
echo '</div>';
}
?>
I have the following mysql query and I have added pagination from here:
http://www.tonymarston.net/php-mysql/pagination.html
$DBQuery3 = mysqli_query($dblink, "SELECT * FROM images WHERE project_id = '$FormProjectID'");
if (mysqli_num_rows($DBQuery3) < 1) {
$ProjectContent = '
<p>This project is empty. Upload some files to get started.</p>
';
} else {
//if no page number is set, start at page 1
if (isset($_GET['pageno'])) {
$pageno = $_GET['pageno'];
} else {
$pageno = 1;
}
//This code will count how many rows will satisfy the current query.
$DBQuery3b = mysqli_query($dblink, "SELECT count(*) FROM images WHERE project_id = '$FormProjectID'");
$query_data = mysqli_fetch_row($dblink, $DBQuery3b);
$numrows = $query_data[0];
//This code uses the values in $rows_per_page and $numrows in order to identify the number of the last page.
$rows_per_page = 2;
$lastpage = ceil($numrows/$rows_per_page);
//This code checks that the value of $pageno is an integer between 1 and $lastpage.
$pageno = (int)$pageno;
if ($pageno > $lastpage) {
$pageno = $lastpage;
}
if ($pageno < 1) {
$pageno = 1;
}
//This code will construct the LIMIT clause for the sql SELECT statement.
$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;
$DBQuery3c = "SELECT * FROM images WHERE project_id = $FormProjectID $limit";
$DBQuery3d = mysqli_query($dblink, $DBQuery3c);
//set this variable to empty and so we can latwe loop and keep adding images to it
$ProjectContent ='';
while($row = mysqli_fetch_array($DBQuery3d)) {
$DBImageID = $row['image_id'];
$DBProjectID = $row['project_id'];
$DBImageName = $row['image_name'];
$DBImageDescription = $row['image_description'];
$DBDateCreated = $row['date_created'];
$DBLinkToFile = $row['link_to_file'];
$DBLinkToThumb = $row['link_to_thumbnail'];
$DBGivenName = $row['given_name'];
//if the image was given a name by the user, display it
//otherwise display the generated name
if (strlen($DBGivenName) > 1) {
$FileName = $DBGivenName;
} else {
$FileName = $DBImageName;
}
$ProjectContent .= '
<img src="uploads/'.$DBLinkToThumb.'" width="150px" height="150px" alt="'.$FileName.'" title="'.$FileName.'"/>
';
//Finally we must construct the hyperlinks which will allow the user to select other pages. We will start with the links for any previous pages.
if ($pageno == 1) {
$FirstPrev = " FIRST PREV ";
} else {
$First = " <a href='{$_SERVER['PHP_SELF']}?page=project&id=$FormProjectID&pageno=1'>FIRST</a> ";
$prevpage = $pageno-1;
$Prev = " <a href='{$_SERVER['PHP_SELF']}?page=project&id=$FormProjectID&pageno=$prevpage'>PREV</a> ";
}
//Next we inform the user of his current position in the sequence of available pages.
$PageNumb = " ( Page $pageno of $lastpage ) ";
//This code will provide the links for any following pages.
if ($pageno == $lastpage) {
$NextLast = " NEXT LAST ";
} else {
$nextpage = $pageno+1;
$Next = " <a href='{$_SERVER['PHP_SELF']}?page=project&id=$FormProjectID&pageno=$nextpage'>NEXT</a> ";
$Last = " <a href='{$_SERVER['PHP_SELF']}?page=project&id=$FormProjectID&pageno=$lastpage'>LAST</a> ";
}
}
}
Then in my html I have:
<div id="projectview">
<?php echo $ProjectContent; ?>
<?php echo $FirstPrev; ?>
<?php echo $First; ?>
<?php echo $Prev; ?>
<?php echo $PageNumb; ?>
<?php echo $NextLast; ?>
<?php echo $Next; ?>
<?php echo $Last; ?>
<div class="clear-div"></div>
</div>
This is outputing for example in this case 2 images, but the pagination links look like this:
FIRST PREV ( Page 1 of 0 ) NEXT LAST
Clicking on the links is not cycling through any other images, it still shows the same 2 images.
I can't figure out what I have done wrong here. I don't understand why it says "1 of 0" when clearly there should be more results.
I have two tiny little problems;
1 . I want to add a previous / next button into this code
2 . I want it to only show like max 10 links between previous and next. So if i have 50 numbers/links it will only show 10 of them and not 50 links on the page.
I have searched on the clo
The code works, only need that two options in it.
Can someone help me out? Thank you !
<?php
include 'includes/connection.php';
$per_page = 8;
$pages_query = mysql_query("SELECT COUNT(`id`) FROM `products`");
$pages = ceil(mysql_result($pages_query, 0) / $per_page);
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_page;
$query = mysql_query("SELECT `name` FROM `products` LIMIT $start, $per_page");
while ($query_row = mysql_fetch_assoc($query)) {
echo '<p>', $query_row['name'] ,'</p>';
}
if ($pages >= 1 && $page <= $pages) {
for ($x=1; $x<=$pages; $x++) {
//echo $x, ' ';
echo ($x == $page) ? '<strong>'.$x.'</strong> ' : ''.$x.' ';
}
}else{
header("location:index.php?page=1");
}
?>
First of all, you should, just for good practice, put an "exit;" after the header() call at the end. It doesn't make a difference in this particular script, but keep in mind that any code following a header("Location: ...") call WILL be executed before redirection.
Now to your question, try this (UPDATE: This code has been tested and works.)
<?php
include 'includes/connection.php';
$per_page = 8;
$pages_query = mysql_query("SELECT COUNT(`id`) FROM `products`");
$pages = ceil(mysql_result($pages_query, 0) / $per_page);
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_page;
$query = mysql_query("SELECT `name` FROM `products` LIMIT $start, $per_page");
while ($query_row = mysql_fetch_assoc($query))
{
echo '<p>' . $query_row['name'] . '</p>';
}
// If the requested page is less than 1 or more than the total number of pages
// redirect to the first page
if($pages < 1 || $page > $pages)
{
header('Location: ?page=1');
// end execution of the rest of this script
// it will restart execution after redirection
exit;
}
// If more than one page, show pagination links
if($pages > 1)
{
$html = array();
$html[] = '<strong>';
// if you're on a page greater than 1, show a previous link
$html[] = (($page > 1) ? 'Previous ' : '');
// First page link
$pageFirst = '1';
$html[] = (($page == 1) ? "</strong>{$pageFirst}<strong>" : $pageFirst);
if ($pages > 6)
{
$start_cnt = min(max(1, $page - (6 - 1)), $pages - 6);
$end_cnt = max(min($pages, $page + 4), 8);
$html[] = ($start_cnt > 1) ? '...' : ' ';
for ($i = $start_cnt + 1; $i < $end_cnt; $i++)
{
$html[] = ($i == $page) ? '</strong>' . $i . '<strong>' : '' . $i . '';
if ($i < $end_cnt - 1)
{
$html[] = ' ';
}
}
$html []= ($end_cnt < $pages) ? '...' : ' ';
}
else
{
$html[] = ' ';
for ($i = 2; $i < $pages; $i++)
{
$html[] = ($i == $page) ? '</strong>' . $i . '<strong>' : '' . $i . '';
if ($i < $pages)
{
$html[] = ' ';
}
}
}
// last page link
$pageLast = '' . $pages . '';
$html[] = (($page == $pages) ? "</strong>{$pageLast}<strong>" : $pageLast);
// Show next page link if you're on a page less than the total number of pages
$html[] = ($page < $pages) ? ' Next' : '';
// If you're not on the last page, show a next link
$html[] = '</strong>';
}
else
{
// show page number 1, no link.
$html[] = '<strong>1</strong>';
}
echo implode('', $html);
Also note that the final ?> is not required in PHP files that do not have HTML code following the PHP code, so I left it off.
Buddy refer this URL
http://www.codediesel.com/php/simple-pagination-in-php/
OR
http://www.phpfreaks.com/tutorial/basic-pagination
Surely will help you.
Thanks
Can anyone help in this php page navigation script switch on counting normal serial number? In this script there is a var called "page_id" - I want this var to store the real page link by order like 0, 1, 2, 3, 4, 5 ...
<?
$onpage = 10; // on page
/*
$pagerecord - display records per page
$activepage - current page
$records - total records
$rad - display links near current page (2 left + 2 right + current page = total 5)
*/
function navigation($pagerecord, $activepage){
$records = 55;
$rad = 4;
if($records<=$pagerecord) return;
$imax = (int)($records/$pagerecord);
if ($records%$pagerecord>0)$imax=$imax+1;
if($activepage == ''){
$for_start=$imax;
$activepage = $imax-1;
}
$next = $activepage - 1; if ($next<0){$next=0;}
$end =0;
$prev = $activepage + 1; if ($prev>=$imax){$prev=$imax-1;}
$start= $imax;
if($activepage >= 0){
$for_start = $activepage + $rad + 1;
if($for_start<$rad*2+1)$for_start = $rad*2+1;
if($for_start>=$imax){ $for_start=$imax; }
}
if($activepage < $imax-1){
$str .= ' <<< End <span style="color:#CCCCCC">•</span> < Forward | ';
}
$meter = $rad*2+1; //$rad; ---------------------
for($i=$for_start-1; $i>-1; $i--){
$meter--;
//$line = '|'; if ($meter=='0'){ $line = ''; }
$line = ''; if ($i>0)$line = '|';
if($i<>$activepage){
$str .= " <a href='?page=".$i."&page_id=xxx'>".($i)."</a> ".$line." ";
} else {
$str .= " <strong>[".($i)."]</strong> ".$line." ";
}
if($meter=='0'){ break; }
}
if($activepage > 0){
$str .= " | <a href='?page=".$next."'>Back ></a> <span style='color:#CCCCCC'>•</span> <a href='?page=".($end)."'>Start >>></a> ";
}
return $str;
}
if(is_numeric($_GET["page"])) $page = $_GET["page"];
$navigation = navigation($onpage, $page); // detect navigation
echo $navigation;
?>
Instead xxx here (page_id=xxx) I want to link to real page number by normal order when this script show links but reversed.
Really need help with this stuff! Thanks in advance!
I were helped by one of the programmers with my above script. So here is a worked example of the reversed page navigation on PHP.
<?
$onpage = 10; // on page
/*
$pagerecord - display records per page
$activepage - current page
$records - total records
$rad - display links near current page (2 left + 2 right + current page = total 5)
*/
function navigation($pagerecord, $activepage){
$records = 126;
$rad = 4;
if($records<=$pagerecord) return;
$imax = (int)($records/$pagerecord);
if ($records%$pagerecord>0)$imax=$imax+1;
if($activepage == ''){
$for_start=$imax;
$activepage = $imax-1;
}
$next = $activepage - 1; if ($next<0){$next=0;}
$end =0;
$prev = $activepage + 1; if ($prev>=$imax){$prev=$imax-1;}
$start= $imax;
if($activepage >= 0){
$for_start = $activepage + $rad + 1;
if($for_start<$rad*2+1)$for_start = $rad*2+1;
if($for_start>=$imax){ $for_start=$imax; }
}
$meter = $rad*2+1; //$rad; ---------------------
$new_meter = $for_start-1;
if($activepage < $imax-1){
$str .= ' <<< End <span style="color:#CCCCCC">•</span> < Forward | ';
}
for($i=$for_start-1; $i>-1; $i--){
$meter--;
//$new_meter++;
//$line = '|'; if ($meter=='0'){ $line = ''; }
$line = ''; if ($i>0)$line = '|';
if($i<>$activepage){
$str .= " <a href='?page=".$i."&page_id=".($imax-$i-1)."'>".($i)."</a> ".$line." ";
} else {
$str .= " <strong>[".($i)."]</strong> ".$line." ";
}
if($meter=='0'){ break; }
}
if($activepage > 0){
$str .= " | <a href='?page=".$next."&page_id=".($imax-$next-1)."'>Back ></a> <span style='color:#CCCCCC'>•</span> <a href='?page=".($end)."&page_id=".($start-1)."'>Start >>></a> ";
}
return $str;
}
if(is_numeric($_GET["page"])) $page = $_GET["page"];
$navigation = navigation($onpage, $page); // detect navigation
echo $navigation;
?>
$page = keeps the page number from the reversed order
$page_id = keeps the real page by serial order. so you can make SELECT queries to database and ORDER BY id DESC use.