Version: 1.4
Type: Sample Code (HOWTO)
Category: HTML
License: GNU General Public License
Description: This script will output a link list in a table with PREV 123 NEXT navigation links.
The main logic is taken from Rod Kreisler’s article in PHPbuilder.com.
I’m a beginner at PHP programming and would like to thank Rod for the built in bugs.
There’s no better way to learn programming than trying to fix the mistakes of other’s.
No sarcasm intended.
<? ////////////////////////////////////////////////////////////////////////////// //**Complete re-write here... //**Enjoy: Chris Whalin - [email protected] ////////////////////////////////////////////////////////////////////////////// //**Fixed bug that would display wrong results on rare reloads //**Fixed bad result issue when records were access indivudually // or through a different query. For example if your displaying // results in sets of 5, you'ld get results as 1-5 6-10 and so on // The issue came about when you wanted to link directly to one of // record numbers inbetween the sets of 5. So if you did // http://yourpage.php?thisOffset=8&lineIndex=8 // the results would show record 9 instead of 8. // To fix this i just added anohter variable to adjust the offset and limit // So on any search query or direct link to a record that is not handled // by the Prev/Next navigation just append action=search onto your url path. // Like: http://yourpage.php?thisOffset=8&lineIndex=8&action=search // This should work under any circustances, if not let me know and ill // find a solution for the problem. //**Re-commented the entire script to try to make it more understandable //**Added variables for easy font control of the Next/Prev navigation ////////////////////////////////////////////////////////////////////////////// /*=========================================*/ /*==Variables used through out the script==*/ /*=========================================*/ $server = ""; // Server Name $userid = ""; // Username $pass = ""; // Password $database = ""; // Database Name $dbtable = ""; // Table Name $recordLimit= 5; // Limit per page /*==============================================*/ /*==Font control for the Next/Prev Naviagation==*/ /*==============================================*/ $prevStatus = "Show Previous $recordLimit Results"; $nextStatus = "Show Next $recordLimit Results"; $prevLink = "< Previous|"; $nextLink = "|Next >"; $fontfamily = "tahoma, helvetica"; $fontsize = "13px"; $fontcolor = "#000000"; $inactivecolor = "#666666"; // Color for when there is no link $fontweight = "700"; $textdecoration = "none"; /*==========================================*/ /*==Window Status of individual page links==*/ /*==========================================*/ function pageStatus($i){ echo " Click to View Page $i"; } /*========================================*/ /*==Make server and database connections==*/ /*========================================*/ $con = mysql_connect("$server","$userid","$pass") or die ("Connection Error to Server"); $db = mysql_select_db("$database",$con) or die("Connection Error to Database"); /*==================================================*/ /*==If thisOffset not set, set to zero==============*/ /*==================================================*/ if (!isset($thisOffset) || $thisOffset < 0) $thisOffset=0; if ($action==search) $thisOffset = $thisOffset - 1; /*==================================================*/ /*==This is to count the line index of the results==*/ /*=============if not set, set to zero==============*/ /*==================================================*/ if (!isset($lineIndex) || $lineIndex < 0) $lineIndex=0; if ($action==search) $lineIndex = $lineIndex - 1; /*========================================*/ /*==Set Base SQL Statement for the query==*/ /*========================================*/ $sql="SELECT * FROM $dbtable"; //$sql.=" WHERE category = ''"; /*======================================*/ /*==Query for the total number of rows==*/ /*======================================*/ $getTotalRows = mysql_query($sql, $con); $totalRowsNum = mysql_num_rows($getTotalRows); /*==========================================*/ /*==Append the limit and offset to the sql==*/ /*==========================================*/ $sql.=" Limit $thisOffset,$recordLimit"; /*===================================================*/ /*==Query and fetch array with the limit and offset==*/ /*===================================================*/ $query = mysql_query($sql, $con); while ($result=mysql_fetch_array($query)){ /*=================================*/ /*==Increment the line index by 1==*/ /*=================================*/ $lineIndex++; /*============================================*/ /*====== Your results layout goes here =======*/ /*============================================*/ //**Example: //$category=$result['CATEGORY']; //$title=$result['TITLE']; //echo "$lineIndex. - $category - $title<br>"; } /*===================================================================*/ /*==If the number of returned is less than the limit, show nothing===*/ /*===================================================================*/ if ($totalRowsNum <= $recordLimit) { // less than recordLimit returned. } else { /*==============================================*/ /*==If the current offset is not equal to zero==*/ /*==Show the link. Else (if>0) show just text===*/ /*==============================================*/ if ($thisOffset!=0) { $prevOffset = intval($thisOffset-$recordLimit); echo "<a style='font-family:$fontfamily; font-size:$fontsize; color:$fontcolor; font-weight:$fontweight;" ." text-decoration:$textdecoration;' onmouseover="window.status='$prevStatus'; return true"" ." onmouseout="window.status=''" href="$PHP_SELF?thisOffset=$prevOffset&lineIndex=$prevOffset">" ."$prevLink</a> "; } else { echo "<span style='font-family:$fontfamily; font-size:$fontsize; color:$inactivecolor;" ." font-weight:$fontweight; text-decoration:$textdecoration;'>$prevLink</span> "; } /*================================================================*/ /*==Divide total rows by record limit to get the number of pages==*/ /*================================================================*/ $totalPages = intval($totalRowsNum/$recordLimit); /*===========================================================*/ /*==If remainder is left from division increment totalPages==*/ /*===========================================================*/ if ($totalRowsNum%$recordLimit) $totalPages++; /*===========================================================*/ /*==Using a for() loop, Display links for each page. 1 2 3==*/ /*===========================================================*/ for ($i=1;$i<=$totalPages;$i++) { /*==================================================================*/ /*==If the page being viewed is the current page, disable the link==*/ /*==================================================================*/ if ((intval($thisOffset/$recordLimit)) == (intval($i-1))) { echo " <span style='font-family:$fontfamily; font-size:$fontsize; color:$inactivecolor;" ." font-weight:$fontweight; text-decoration:$textdecoration;'>$i</span> "; } else { /*=======================================================*/ /*==If not current page, set nextOffset and display link==*/ /*=======================================================*/ $nextOffset= intval($recordLimit*($i-1)); echo " <a style='font-family:$fontfamily; font-size:$fontsize; color:$fontcolor;" ." font-weight:$fontweight; text-decoration:$textdecoration;' onmouseover="window.status='"; pageStatus($i); echo "'; return true" onmouseout="window.status=''"" ." href="$PHP_SELF?thisOffset=$nextOffset&lineIndex=$nextOffset">$i</a> "; } } /*==========================================================================*/ /*==If the page is not the last page, set the nextOffset and show the link==*/ /*==============If the page is the last page disable the link===============*/ /*==========================================================================*/ if (!(intval(((intval($thisOffset/$recordLimit))+1))==$totalPages) && $totalPages!=1) { $nextOffset = intval($thisOffset+$recordLimit); echo " <a style='font-family:$fontfamily; font-size:$fontsize; color:$fontcolor; font-weight:$fontweight;" ." text-decoration:$textdecoration;' onmouseover="window.status='$nextStatus'; return true"" ." onmouseout="window.status=''" href="$PHP_SELF?thisOffset=$nextOffset&lineIndex=$nextOffset">$nextLink</a><p>n"; } else echo " " ."<span style='font-family:$fontfamily; font-size:$fontsize; color:$inactivecolor;" ." font-weight:$fontweight; text-decoration:$textdecoration;'>$nextLink</span><p>"; } mysql_close($con); //End ?>