Tutorials
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsOtherTutorials

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old September 5th, 2006, 05:04 PM
cybernine cybernine is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 73 cybernine User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 3
Next and Previous Links using Functions

I have been working on this for a while, trying to simplify previous and next links using functions. So here it is, Please let me know how it works for you.

Basic Rundown

This script can pull information from MySQL Database and let you use Previous and Next Links to navigate. All you have to do is set the configuration info at the top of the script and setup the SELECT statement and the rows. This also works well using include from another php file.

UPDATED 09/11/2006 6:12

Removed gpageNum function and added it in with recordPaging().
Fixed problem with results with only 2 pages.
Added Comments
Updated Record Paging
Added Not displaying links when its not needed.

php Code:
Original - php Code
  1.  
  2. <?php
  3. // MySQL Host Name or IP Address, Default 'localhost'
  4. $sql_host = 'localhost';
  5. // MySQL Username, Default 'root'
  6. $sql_usr = 'root';
  7. // MySQL Password.
  8. $sql_pass = '';
  9. // MySQL Database to use
  10. $sql_db = 'employees';
  11. // How many rows to return in a page.
  12. $paging_maxrows = 15;
  13. // Default page number to start from, Default left '0'.
  14. $paging_pagenum = 0;
  15. // Alternate's row color in a result 1st row.
  16. $rowColor1 = "FFFFFF";
  17. // Alternate's row color in a result 2st row.
  18. $rowColor2 = "#00CCCC";
  19. /*Page Title Name*/
  20. $strPageTitle = 'Next & Previous Links';
  21. /*Domain Name: Example http://www.google.com*/
  22. $homeURL = "http://www.google.com";
  23.  
  24.  
  25. /*NO MORE CONFIGURATION REQUIRED AFTER THIS POINT*/
  26.  
  27.  
  28.  
  29.  
  30. // Gets the current full url page location.
  31. // This function forces a user go to the full URL.
  32. function currentPage() {
  33. global $homeURL;
  34.  
  35. $currentpage = sprintf("%s%s", $homeURL, $_SERVER["PHP_SELF"]);
  36. return $currentpage;
  37.  
  38. }
  39.  
  40. // Queries a MySQL Database
  41. function dbquery($strSQL) {
  42. global $sql_host, $sql_usr, $sql_pass, $sql_db;
  43.  
  44.     $db = mysql_connect($sql_host, $sql_usr,$sql_pass);
  45.     mysql_select_db($sql_db,$db);
  46.    
  47.     if (!$queryValue = @mysql_query($strSQL, $db)) {
  48.     die("<p><font color='red'>Error: ".mysql_error())."</p>";
  49.     } Else {   
  50.     return $queryValue;
  51.     }   }
  52.  
  53. // Queries a MySQL Database and allows paging results.
  54. // Insert the UPDATE, SELECT, OR DELETE statement where $strSQL is using the function.
  55. function dbqueryl($strSQL) {
  56. global $sql_host, $sql_usr, $sql_pass, $sql_db, $paging_maxrows, $paging_pagenum;
  57.  
  58. $db = mysql_connect($sql_host, $sql_usr, $sql_pass);
  59. mysql_select_db($sql_db, $db);
  60.  
  61. // Returns the current pageNumber or defaults to page '0'.
  62. $pagenum = 0;
  63. if (isset($_GET['pageNumber'])) {
  64.   $pagenum = $_GET['pageNumber'];
  65. }
  66. $startRow = $pagenum * $paging_maxrows;
  67.  
  68.  
  69. // Inserts the LIMIT statement in the $strSQL statement.
  70. $query_limit = sprintf("%s LIMIT %d, %d", $strSQL, $startRow, $paging_maxrows);
  71.     if (!$queryValue = mysql_query($query_limit, $db)) {
  72.     die("<p><font color='red'>Error: ".mysql_error())."</p>";
  73.     } Else {   
  74.     return $queryValue;
  75. }   
  76. }
  77.  
  78.  
  79. // Alternates the row color in a result.
  80. function rowColor() {
  81. global $rowColor1, $rowColor2, $ity0;
  82.  
  83. if($ity0 % 2) {
  84. print $rowColor1;
  85. } else {
  86. print $rowColor2;
  87. }
  88. $ity0++;
  89. }
  90.  
  91.  
  92. // Recordset Paging that is used with function "dbqueryl".
  93. function recordPaging($sql) {
  94. global $paging_maxrows, $paging_pagenum, $currentPage;
  95.  
  96.  
  97. $sel_pagenum_sql = dbquery($sql);
  98. $sel_pagenum = mysql_num_rows($sel_pagenum_sql);
  99. $page_number = ceil($sel_pagenum/$paging_maxrows)-1;
  100.  
  101.  
  102. // Function to replace the pageNumber and TotalPages in the Query String
  103. if(isset($_GET['pageNumber'])) {
  104. $current_pagenum = $_GET['pageNumber'];
  105. } else {
  106. $current_pagenum = 0;
  107. }
  108.  
  109. if(isset($_GET['TotalPages'])) {
  110. $current_totalpages = $_GET['TotalPages'];
  111. } else {
  112. $current_totalpages = 0;
  113. }
  114.  
  115.  
  116. $url_querystr = $_SERVER['QUERY_STRING']; // Gets the current query string
  117. $find_page_info = sprintf("pageNumber=%d&TotalPages=%d&", $current_pagenum, $current_totalpages);
  118. $rem_page_info = str_replace($find_page_info, '', $url_querystr);
  119. $alt = $rem_page_info;
  120. // End of Function to replace the pageNumber and TotalPages in the Query String
  121.  
  122.  
  123.  
  124. echo "<table width="360" border="0" align="center">n";
  125. echo "<tr class="tblrow">n";
  126. // First Page Link.
  127. echo "<td width="90" align="center">n";
  128. if($current_pagenum > 0) { // If Page 0, Do not display First Page Link.
  129. printf ("<a href="%s?pageNumber=%d&TotalPages=%d&%s">First Page </a>n", currentPage(), 0, $page_number, $alt);
  130. }
  131. echo "</td>n";
  132. // Previous Page Link.
  133. echo "<td width="90" align="center">n";
  134. if($current_pagenum > 0) { // If Page 0, Do not display Previous Link.
  135. $previous = max(0, $current_pagenum - 1);
  136. printf ("<a href="%s?pageNumber=%d&TotalPages=%d&%s">Previous Page</a>n", currentPage(), $previous, $page_number, $alt);
  137. }
  138. echo "</td>n";
  139. // Next Page Link.
  140. echo "<td width="90" align="center">n";
  141. if($current_pagenum != $page_number) { // If Page is Last, Do not display Next Page Link.
  142. $next = min($page_number, $current_pagenum + 1);
  143. printf ("<a href="%s?pageNumber=%d&TotalPages=%d&%s">Next Page</a>n", currentPage(), $next, $page_number, $alt);
  144. }
  145. echo "</td>n";
  146. // Last Page Link.
  147. echo "<td width="90" align="center">n";
  148. if($current_pagenum != $page_number) { // If Page is Last, Do not display Last Page Link.
  149. printf ("<a href="%s?pageNumber=%d&TotalPages=%d&%s">Last Page</a>n", currentPage(), $page_number, $page_number, $alt);
  150. echo "</td>n";
  151. }
  152. echo "</tr>n";
  153. echo "</table>n";
  154.  
  155.  
  156. }
  157.  
  158. ?>
  159. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  160. <html xmlns="http://www.w3.org/1999/xhtml">
  161. <head>
  162. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  163. <title><?php echo $strPageTitle; ?></title>
  164. </head>
  165.  
  166. <body>
  167. <p>
  168.   <?php
  169. $Search_Search = "0";
  170. if (isset($_GET['Search'])) {
  171.   $Search_Search = $_GET['Search'];
  172. }
  173.  
  174. $Search_sql = "SELECT * FROM employees";
  175. $Search = dbqueryl($Search_sql);
  176.  
  177.  
  178. while ($row = mysql_fetch_assoc($Search)) {
  179. print "".$row['Name']."<br >";
  180.  
  181. }
  182. ?>
  183. </p>
  184. <?php recordPaging($Search_sql); // Reference the SQL statement inside the () to caculate the correct pages to result. ?>
  185. </body>
  186. </html>
  187.  
  188.  

Reply With Quote
  #2  
Old September 6th, 2006, 07:36 AM
notepad notepad is offline
Codewalkers Loyal (3000 - 3499 posts)
 
Join Date: Apr 2007
Location: Central, IL USA
Posts: 3,214 notepad User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Send a message via AIM to notepad
RE: Next and Previous Links using Functions

not bad.. i think there is room for improvement. specifically:
- you might want to make sure that your query string for what page you're on doesn't interfere with any other query string variables..
- you might consider displaying page numbers, allowing people to jump 2 or 3 pages ahead, rather than forcing them to go one page at a time. or allow the developer to choose between several different displays. either way don't force the developer into using a table, try style sheets.
- don't link back to the page you're currently viewing
- i think you should allow the functions to be portable with both databases as well as flat-files.

some commenting in your code would be nice too.

Reply With Quote
  #3  
Old September 6th, 2006, 01:23 PM
cybernine cybernine is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 73 cybernine User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 3
RE: Next and Previous Links using Functions

I thought about several of those improvement options that you mentioned right after I posted this. I will be adding to this code soon. I just wanted people to have a better option of using next and previous links than what is posted in the tutorial section.

I am also open for any additional code that anyone might think will be usefull.

Reply With Quote
  #4  
Old September 6th, 2006, 05:53 PM
notepad notepad is offline
Codewalkers Loyal (3000 - 3499 posts)
 
Join Date: Apr 2007
Location: Central, IL USA
Posts: 3,214 notepad User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Send a message via AIM to notepad
RE: Next and Previous Links using Functions

funny, i sent a pm to bluephoenix a week or two ago talking about revising/updating the prev-next link tutorials

still haven't heard back.

Reply With Quote
  #5  
Old November 4th, 2006, 03:21 AM
bluephoenix's Avatar
bluephoenix bluephoenix is offline
Levelheaded Curmudgeon
Codewalkers Novice (500 - 999 posts)
 
Join Date: Apr 2007
Location: Syracuse, NY
Posts: 508 bluephoenix User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 24 m 56 sec
Reputation Power: 3
Send a message via AIM to bluephoenix
RE: Next and Previous Links using Functions

... and you won't, notepad. I'm not talking to you after that incident with the swiss cheese. Just kidding. I'll see what I can do.

-Tim

Reply With Quote
Reply

Viewing: Codewalkers ForumsOtherTutorials > Next and Previous Links using Functions


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
Create the Optimal Architecture for your Critical Applications
Warburton's the largest independently owned bakery in the UK faced a number of difficult challenges in providing the most robust yet efficient IT infrastructure for their organization's success. IBM's services combined with their xSeries servers created the perfect platform for their SAP environment with sufficient flexibility, and did so in very time effective fashion.

Request Your Free Technology Downloads!
 
Five Best Practices for Deploying a Successful Service-Oriented Architecture
This white paper describes the benefits you can expect with SOA, and how IBM can help take your business there.

Request Your Free Technology Downloads!
 
Gartner Magic Quadrant for Application Delivery Controllers
Gartner summarizes its view on Application Delivery Controllers, evaluates strengths and weaknesses of solutions, and provides Magic Quadrant reporting for a quick comparison across all vendors. Learn from Gartner how you can benefit from an all-in-one device like Citrix NetScaler that delivers the highest levels of availability, performance and security.

Request Your Free Technology Downloads!
 
Knowledge is Power
What you don't know can hurt you, and is likely costing you money and increasing your security risks during an era of scarce resources. This white paper proposes six key strategies that enterprise security managers can use to improve their network defense posture.

Request Your Free Technology Downloads!
 
Rationalizing the Multi-Tool Environment
The rationalized multi-tool approach is flexible, scalable and cost effective. It provides the necessary input to the IT service management business processes. It preserves prior investments in monitoring tools, empowers technologists to select the best tools with which to do their jobs, and enhances effective response to incidents.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2010 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek