bsnmcahi Posted October 25, 2012 Share Posted October 25, 2012 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> ================================================================================== Quote Link to comment https://forums.phpfreaks.com/topic/269914-add-pagination-to-this-php-script/ Share on other sites More sharing options...
PFMaBiSmAd Posted October 25, 2012 Share Posted October 25, 2012 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> "; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/269914-add-pagination-to-this-php-script/#findComment-1387748 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.