home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / PHP / mysqlnext.php3.txt < prev    next >
Encoding:
Text File  |  2002-05-06  |  7.6 KB  |  224 lines

  1. MySQL Next/Prev Reco 
  2.  
  3. A template script to provide the ability to get the next or previous n records from a MySQL database. 
  4.  
  5.  
  6.  
  7. <?php 
  8. require( "mysqlcheck.inc");         /* Check the mysql daemon */ 
  9. /* 
  10. This is a template for a script that will return the next n records from a MySQL  
  11. database query. It uses the LIMIT option in the MySQL SELECT syntax 
  12.  
  13. Written by David Robley July 1998 
  14. Comments, improvements etc to webmaster@www.nisu.flinders.edu.au 
  15.  
  16. Data Dictionary 
  17.     *    $fgcolour        Foreground colour of document 
  18.     *    $bgcolour        Background colour of document 
  19.     *    $leftarr        Image to use for Previous image 
  20.     *    $rightarr        Image to use for Next image 
  21.     *    $database        Database to query 
  22.     *    $table            Table to query 
  23.     *    $lower            Lowest number of the records currently on display 
  24.   *    $upper            Highest number of the records currently on display 
  25.   *    $NUM_RECS        Number of records to be diplayed per page 
  26.   *    $offset            Use for OFFSET value in 'SELECT' statement 
  27.   *    $searchon        Field to be searched on 
  28.   *    $searchfor    Value to be searched for 
  29.  */ 
  30.  
  31. /* formDropDown - create an HTML <SELECT> - By John Bryant 
  32.  * vars: $name - the form variable NAME 
  33.  *       $value - the SELECTED option 
  34.  *       $labels - assoc. array, list of values=>labels 
  35.  * returns: string, HTML (i.e. for use in echo or print statement) 
  36.  */ 
  37. function formDropDown($name,$value,$labels) { 
  38.   $html =  "<SELECT NAME=\"$name\">\n"; 
  39.   $key = key($labels); 
  40.   while($key !=  "") { 
  41.     if ($key == $value) { 
  42.       $selected =  "SELECTED"; 
  43.     } else { 
  44.       $selected =  ""; 
  45.     } 
  46.     $html .=  "<OPTION VALUE=\"$key\" $selected>$labels[$key]\n"; 
  47.     next($labels); 
  48.     $key = key($labels); 
  49.   } 
  50.   $html .=  "</SELECT>\n"; 
  51.   return $html; 
  52. /* Some attributes that can be changed */ 
  53. /* Set fore and background colour values */ 
  54. $fgcolour =  "#009999"; 
  55. $bgcolour =  "#000033"; 
  56. /* And the images to use for arrows. */ 
  57. $leftarr =  "/gifs/cyan_left.gif"; 
  58. $rightarr =  "/gifs/cyan_right.gif"; 
  59.  
  60. /* Name of the database and table to use for the query */ 
  61. $database =  "database"; 
  62. $table =  "table"; 
  63. ?> 
  64. <!DOCTYPE HTML PUBLIC "-//AdvaSoft//DTD HTML 3.2 experimental 961018//EN"> 
  65. <HTML> 
  66. <HEAD><TITLE>Title</TITLE></HEAD> 
  67.  
  68. <BODY BGCOLOR=" <?php echo $bgcolour?>" TEXT=" <?php echo $fgcolour?>"> 
  69. <H1>Search</H1> 
  70. Search for references. 
  71. <P> 
  72. <HR> 
  73. <FORM ACTION=" <?php echo $PHP_SELF?>" METHOD="POST"> 
  74. <CENTER> 
  75. <TABLE ALIGN=center CELLSPACING="15" BORDER=0> 
  76.  
  77. <!-- Build your input form here --> 
  78.  
  79. <TR><TD ALIGN="CENTER">View the results 
  80. <?php 
  81. /* This builds a dropdown list to provide the choices for number of records to display per page */ 
  82.  
  83. $labels = array( "5"=> "5", "10"=> "10", "15"=> "15", "20"=> "20"); 
  84. $match = (empty($NUM_RECS) ?  "5" : $NUM_RECS); 
  85. $box = formDropDown( "NUM_RECS",$match,$labels); 
  86. echo $box; 
  87. ?> 
  88. per page 
  89. <TD ALIGN=center><INPUT TYPE=submit NAME="search" VALUE="Search"> 
  90. <TD ALIGN=center><INPUT TYPE=reset></TD> 
  91. </TABLE></CENTER> 
  92. </FORM> 
  93.  
  94. <HR> 
  95. <?php 
  96. /* First time through on a new search $offset should be zero, $lower should be one and $upper 
  97. should be $NUM_RECS */ 
  98. if(empty($lower)): 
  99.     $lower = 1; 
  100. endif; 
  101. if(empty($upper)): 
  102.     $upper = $NUM_RECS; 
  103. endif; 
  104. if(empty($offset)): 
  105.     $offset = 0; 
  106. endif; 
  107.  
  108. if(!empty($next_x)):             /* The Next button has been selected */ 
  109.     $lower += $NUM_RECS; 
  110.     $upper += $NUM_RECS; 
  111.     $offset += $NUM_RECS; 
  112. endif; 
  113.  
  114. if(!empty($prev_x)):             /* The Previous button has been selected */ 
  115.     $lower -= $NUM_RECS; 
  116.     $upper -= $NUM_RECS; 
  117.     $offset -= $NUM_RECS; 
  118. endif; 
  119.  
  120. /* echo "Offset: " . $offset . ": Lower: " . $lower . ": Upper: " . $upper . "<BR>"; */ 
  121.  
  122. switch($search): 
  123.     case  "Search": 
  124.      /* Build the query string from the data passed in. */ 
  125.  
  126.     $NUM_RECS = intval($NUM_RECS); 
  127.     $query =  "SELECT * FROM " . $table .  " WHERE " . $searchon; 
  128.     $query .=  " LIKE '%" . $searchfor .  "%' LIMIT " . ($offset != 0 ?  "$offset, $NUM_RECS" :  "$NUM_RECS"); 
  129.      /* Countquery gets the number of records that are be retuned by the 'real' query */ 
  130.      
  131.     $countquery =  "SELECT count(*) AS numrows FROM " . $table.  " WHERE " . $searchon; 
  132.     $countquery .=  " LIKE '%" . $searchfor .  "%'"; 
  133.  
  134.     $countresult = mysql_db_query($database, $countquery); 
  135.     $countrow = mysql_fetch_array($countresult); 
  136.     $result = mysql_db_query($database, $query); 
  137.     $numrows = intval($countrow[0]); 
  138. ?> 
  139.     <IMG SRC="/gifs/searchresults.gif" ALT="Search Results" WIDTH="480" HEIGHT="15"><P> 
  140.  
  141. <?php 
  142.     switch($numrows): 
  143.         case 0: 
  144.         echo  "There were no matches to your search for <FONT COLOR=\"#FF0000\">" . $searchfor; 
  145.         echo  "</FONT> in " . $searchon .  ". You may wish to try again with a different search phrase."; 
  146.         break; 
  147.          
  148.         default: 
  149.          /* (!empty($offset) ? $lower = ($offset - $NUM_RECS) + 1 : $lower = 1); 
  150.         $upper = $offset + $NUM_RECS; */ 
  151.         echo  "<FONT FACE=\"LucidaTypewriter, Arial, Helvetica\">"; 
  152.  
  153.         if($numrows <= $NUM_RECS): 
  154.             echo  "Displaying all ".  $numrows; 
  155.         else: 
  156.             echo  "Displaying matches " . $lower .  " to " . ($upper > $numrows ? $numrows : $upper) .  " of " . $numrows; 
  157.         endif; 
  158.         echo  " matches for '<FONT COLOR=\"#FFFFFF\">" . $searchfor .  "</FONT>'</FONT>"; 
  159.         while($row = mysql_fetch_array($result)): 
  160. ?> 
  161. <P> 
  162.  
  163. <! Lay out your returned values here however you like --> 
  164.  
  165. <HR> 
  166.  
  167. <?php 
  168.         endwhile; 
  169.         break; 
  170.     endswitch;         /* Numrows */ 
  171.      
  172.     break; 
  173.     default:             /* No search - throw up the search entry form */ 
  174. endswitch;                 /* Search */ 
  175.  
  176. if($numrows > $NUM_RECS): 
  177. ?> 
  178. <CENTER> 
  179. <FORM ACTION=" <?php echo $PHP_SELF?>" METHOD="POST"> 
  180. <TABLE ALIGN="CENTER"><TR VALIGN=MIDDLE CELLSPACING="10"> 
  181. <TD WIDTH="31"> 
  182.  
  183. <?php if($lower != 1):  /* Show the left arrow and text */  ?> 
  184. <INPUT NAME="prev" TYPE=image SRC=" <?php echo $leftarr?>" BORDER="0" WIDTH="31" HEIGHT="31" ALT="<<="> 
  185. <TD><FONT COLOR=" <?php echo $fgcolour?>"> 
  186. <?php else:?> 
  187. <TD><FONT COLOR=" <?php echo $bgcolour?>"> 
  188. <?php endif;?> 
  189.  
  190. Previous  <?php echo $NUM_RECS?> matches</FONT> 
  191.              
  192. <TD>             
  193.  
  194. <?php if($upper < $numrows):  /* Show the right arrow and text */  ?> 
  195.  
  196. <FONT COLOR=" <?php echo $fgcolour?>">Next  <?php echo $NUM_RECS?> matches</FONT><TD WIDTH="31"> 
  197. <INPUT NAME="next" TYPE=image SRC=" <?php echo $rightarr?>" BORDER="0" WIDTH="31" HEIGHT="31" ALT="=>>"> 
  198. <?php else:?> 
  199. <FONT COLOR=" <?php echo $bgcolour?>">Next  <?php echo $NUM_RECS?> matches</FONT><TD WIDTH="31"> 
  200. <?php endif;?> 
  201.  
  202. </TABLE> 
  203. <!-- Use the hidden fields to pass the search criteria, current location, number  
  204. of records etc to the next form --> 
  205.  
  206. <INPUT TYPE="HIDDEN" NAME="searchon" VALUE=" <?php echo $searchon?>"> 
  207. <INPUT TYPE="HIDDEN" NAME="searchfor" VALUE=" <?php echo $searchfor?>"> 
  208. <INPUT TYPE="HIDDEN" NAME="NUM_RECS" VALUE=" <?php echo $NUM_RECS?>"> 
  209. <INPUT TYPE="HIDDEN" NAME="search" VALUE=" <?php echo $search?>"> 
  210. <INPUT TYPE="HIDDEN" NAME="offset" VALUE=" <?php echo $offset?>"> 
  211. <INPUT TYPE="HIDDEN" NAME="upper" VALUE=" <?php echo $upper?>"> 
  212. <INPUT TYPE="HIDDEN" NAME="lower" VALUE=" <?php echo $lower?>"> 
  213. </FORM> 
  214. </CENTER> 
  215.  
  216. <?php 
  217. endif; 
  218. ?> 
  219.  
  220. </BODY> 
  221. </HTML> 
  222.  
  223.