jeboy Posted June 7, 2007 Share Posted June 7, 2007 Hi! Is there anyone who could help me create an advanced pagination code? The basic: 1. Data to paginate will be coming from msyql database. 2. 10 or 20 items per page. etc, etc... The problem: Limit the number of page numbers to 5 when pages reach more than 5 pages. Example: Total pages: 10 sample page listing output: start of pagination 1.) << 1 2 3 4 5 >> displayed when last page number is clicked (5) on start of pagination 2.) << 3 4 5 6 7 >> Quote Link to comment https://forums.phpfreaks.com/topic/54544-advanced-php-pagination/ Share on other sites More sharing options...
rcorlew Posted June 7, 2007 Share Posted June 7, 2007 Here is the script I use. It should be much of what you want. <?php session_start(); $HOST = 'localhost'; $USER = 'username'; $PASS = 'password'; $NAME = 'database_name'; $searchterm = $_GET["searchterm"]; $searchword = $_POST["searchword"]; $page = $_GET["page"]; $row_max = 10; if(isset($searchword)) { $searchterm = $searchword; } if(!isset($page)) { $page = 1; } if($page == 1) { $pagein = 0; } if($page > 1) { $pagein = $page * $rowmax - $rowmax; } if(isset($searchterm)) { // Connect to MySQL database $con = mysql_connect($HOST, $USER, $PASS); if (!$con) { // Since the entire script depends on connection, die if connection fails die("Error connecting to MySQL database!"); } mysql_select_db($NAME, $con); $cleansearch = mysql_real_escape_string($searchterm); $query = "SELECT * FROM table_name WHERE text LIKE '%$cleansearch%' LIMIT $pagein, $rowmax"; $result = mysql_query($query); while($row = mysql_fetch_array($result)){ //Results here } if(!isset($_SESSION['number'])) { $query2 = mysql_query("SELECT * FROM table_name WHERE text LIKE '%$cleansearch%'"); $num_rows = mysql_num_rows($query2); $_SESSION['number'] = $num_rows; } $num_rows = $_SESSION['number']; $pagecount = ceil($num_rows / $rowmax); $resultinput = $pagein + 1; $resultsoutput = $pagein + $rowmax; if($resultsoutput > $num_rows) {$resultsoutput = $num_rows;} if ($pagecount > 1) { echo"<div align='center'><div style='width: 350px;'>"; echo "Page "; $x = 1; $y = $pagecount; for( $i = $x; $i <= $y; $i++ ) { print " <a href='search.php?searchterm=$osearch&page=$i'><b>[$i]</b></a> "; } echo "</div></div>"; } echo "<p align='center'>There were $num_rows results.<br />"; if($pagecount > 1) { echo "You are on page <b>$page</b><br />"; } echo "Viewing results $resultinput through $resultsoutput.</p>"; } if(!isset($searchterm)) { if(isset($_SESSION['number'])) { unset($_SESSION['number']); } echo "<form method='POST' action='search.php'> <input type='text' name='searchword' size='20' maxlength='20'> <input type='submit' name='submit' value='Search'> </form>"; } I use sessions variables to save server load from doing the same query multiple times. The only thing that my script does not do is to limit the pages to display, but that should be the easy part. Quote Link to comment https://forums.phpfreaks.com/topic/54544-advanced-php-pagination/#findComment-269796 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.