Jump to content

Add Pagination To This Php Script


bsnmcahi

Recommended Posts

Hi all! I have this script here I want to add paging to it so that the table only shows 15 rows at a time then offers page numbers below like 1-15, 16-31 etc..

 

As this is not an SQL database how would I do this??

 

 

Any help would be great..

 

Thanks

 

Martin

 

 

=======================================================

 

 

<HTML>

<HEAD>

<TITLE>Wall Art Stickers UK Design Inventory Online System</TITLE>

</HEAD>

<BODY>

<H1>Design Inventory Online Viewing System</H1>

 

<p>Use this page to view a design numbers product images.<br></p>

<table border="1">

<?php

 

$files = glob("img/products/database small image/*.*");

 

usort($files, "strnatcmp");

 

// Read just the filename of the SVG directory "img/products/database small image/ save in $svgfilename variable

$svgfilename = glob("img/products/database small image/*.*");

 

usort($files, "strnatcmp");

 

$files1 = glob("ebayimages/productimage1/*.*");

 

usort($files1, "strnatcmp");

 

$files2 = glob("ebayimages/productimage2/*.*");

 

usort($files2, "strnatcmp");

 

for ($i=0; $i<count($files); $i++)

{

$num = $files[$i];

$num2 = $files1[$i];

$num3 = $files2[$i];

$rownumber = $i+1;

echo '<tr>';

echo '<td width = "200px"><b><p align="center"><font size="4">Design Number:</b></font><br></p><p align="center"><font size="6">'.$rownumber.'</font></p></td>';

echo '<td width = "250px"><img src="'.$num.'" width="200px"></td>';

echo '<td width = "275px"><img src="'.$num2.'" width="275px"></td>';

echo '<td width = "275px"><img src="'.$num3.'" width="275px"></td>';

echo '</tr>';

}

 

?>

</table>

 

</BODY>

 

</HTML>

 

==================================================================================

Link to comment
Share on other sites

You can paginate an array of data by simply replacing the sql logic with equivalent array functions -

 

<?php
// pagination from db -> array

$arr = range(1,32); // simulate data

$rows_per_page = 15;

// get total number of rows
/*
$query = "SELECT count(*) FROM table WHERE ...";
$result = mysql_query($query, $db) or trigger_error("SQL", E_USER_ERROR);
list($numrows) = mysql_fetch_row($result);
*/
$numrows = count($arr);

// Calculate number of $lastpage
$lastpage = ceil($numrows/$rows_per_page);

// condition inputs/set default
if (isset($_GET['pageno'])) {
  $pageno = $_GET['pageno'];
} else {
  $pageno = 1;
}

// validate/limit requested $pageno
$pageno = (int)$pageno;
if ($pageno > $lastpage) {
  $pageno = $lastpage;
}
if ($pageno < 1) {
  $pageno = 1;
}

// Find starting array index that corresponds to the requested page no
$start = ($pageno - 1) * $rows_per_page;

// database query
/*
$query = "SELECT * FROM table $limit";
$result = mysql_query($query, $db) or trigger_error("SQL", E_USER_ERROR);
//... process contents of $result ...
*/

$page = array_slice($arr, $start, $rows_per_page, true);
foreach ($page as $key=>$value){
   echo "Key: $key, Value: $value<br />";
}

echo "<br />";

// first/prev pagination hyperlinks
if ($pageno == 1) {
  echo " FIRST PREV ";
} else {
  echo " <a href='?pageno=1'>FIRST</a> ";
  $prevpage = $pageno-1;
  echo " <a href='?pageno=$prevpage'>PREV</a> ";
}

// Display current page or pages
echo " ( Page $pageno of $lastpage ) ";

// next/last pagination hyperlinks
if ($pageno == $lastpage) {
  echo " NEXT LAST ";
} else {
  $nextpage = $pageno+1;
  echo " <a href='?pageno=$nextpage'>NEXT</a> ";
  echo " <a href='?pageno=$lastpage'>LAST</a> ";
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.