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

  1. Split Bar 
  2.  
  3. This code writes a hotbar that splits a long MySQL result into a multiple pages recordset. See the demonstration on http://tropicalm.free.fr/pub/world/gallery/gallery.php3 
  4.  
  5.  
  6. This module prints a common split href menu, like those used to  
  7. display into several pages the results of a search engine.  
  8.  
  9. For example, you have a page results.php3 that prints a collection  
  10. of results, and for more convenience you want to write several pages  
  11. from this result, each grouping MAXHIT results. So you paste this code  
  12. into a file split.php3, then you write in results.php3: include("[...]/split.php3");  
  13. at the exact location where you want the split bar to display.  
  14.  
  15. Don't forget to modify your results page results.php3 to display  
  16. only the results i to i+MAXHIT-1 !  
  17.  
  18. ***  
  19. A live example can be seen at http://tropicalm.free.fr/pub/world/gallery/gallery.php3  
  20. ***  
  21.  
  22. *******************************************************************************  
  23. Code of split.php3:  
  24. *******************************************************************************  
  25.  
  26. arguments: _maxelmt: number of elements in the collection  
  27.   MAXHIT :  number of max hits for a page  
  28.   p :  current page to be seen  
  29.   cat (optional): optional parameters to send at return  
  30.  
  31. <?  
  32. /*if MAXHIT=0 print a message error*/   
  33.  
  34. if(!$MAXHIT) {  
  35.     print  "MAXHIT=0 : no split available!\n";  
  36. }  
  37.  
  38. else {  
  39.     if($res && $_maxelmt) {  
  40.         $end=$p*$MAXHIT;  
  41.     if($_maxelmt < $p*$MAXHIT)  
  42.         $end=$_maxelmt;  
  43.    
  44.      print  "\t\t Results ".(max(1,($p-1)*$MAXHIT+1)). '-'.$end. '  <br>'. "\n";  
  45.      }  
  46.  
  47.      /*bypass if there's no more than one page*/   
  48.     if ($_maxelmt> $MAXHIT) {  
  49.              /* pattern: Results i'-i'+MAXHIT [<<Prev] | 1 --page+1 [2 --page+2 ...] | [Next>>] Page i of n */   
  50.  
  51.              /*<<Prev*/   
  52.              /*if p > 1, Prev is active, otherwise it's not*/   
  53.             print  "\t\t<font face='Arial' size='1'";  
  54.  
  55.             if (!($p-1)) {  
  56.                 print  " color='#646464'><<Prev | \n"; }  /*light gray*/   
  57.             else {  
  58.                  /*prev*/   
  59.                 $prevpage=$p-1;  
  60.                 $anurl =  "results.php3?_maxelmt=$_maxelmt&MAXHIT=$MAXHIT&p=$prevpage&$cat";  
  61.                 print  " color='#008000' ><a href=$anurl><<Prev</a> | \n"; }  
  62.                 print  "\t\t</font>". "\n";  
  63.    
  64.                      /*print a page number every MAXHIT*/   
  65.                 for($i=0;$i<$_maxelmt;$i+=$MAXHIT) {  
  66.                  /*page number i+1*/   
  67.                      $pi=($i/$MAXHIT)+1;  
  68.    
  69.                    /*don't activate current page's href*/   
  70.                   if($pi==$p) {  
  71.                       print  "<font face ='Arial' size='1' color=#646464>$pi ";  
  72.                   }  
  73.                 else {  
  74.                         $anurl =  "results.php3?_maxelmt=$_maxelmt&MAXHIT=$MAXHIT&p=$pi&$cat";  
  75.                     print  "<font face ='Arial' size='1' color=#008000 >\n";  
  76.                     print  "\t\t<a href=$anurl>".$pi. ' </a>'. "\n";  
  77.                 }  
  78.                 print  "\t\t</font>". "\n";  
  79.             }  
  80.           /*Next>>*/   
  81.              /*if p*MAXHIT doesn't meet or exceed MAXHIT, Next is active, otherwise it's not*/   
  82.             print  "\t\t<font face ='Arial' size='1'";  
  83.  
  84.             if (($_maxelmt-$p*$MAXHIT) <= 0) {  
  85.                 print  " color='#646464'>| Next>>\n"; }  /*light gray*/   
  86.             else {  
  87.                   /*next*/   
  88.                  $nextpage=$p+1;  
  89.                  $anurl =  "results.php3?_maxelmt=$_maxelmt&MAXHIT=$MAXHIT&p=$nextpage&$cat";  
  90.                  print  ">| <a href=$anurl>Next>></a>\n";}  
  91.                 print  '</font>';  
  92.         }  
  93.  
  94.         if($_maxelmt) {  
  95.             print  "\t\t<font face ='Arial' size='1' color='#008000' > Page ".$p. ' of '.ceil($_maxelmt/$MAXHIT). '</font>'. "\n";  
  96.         } 
  97.     }  /*if ($_maxelmt> $MAXHIT) 
  98. } /*end lse if(!$MAXHIT)*/ 
  99. ?>  
  100.    
  101.  
  102. /********************************************************************************  
  103. Example of use in results.php3: 9 hits maximum, in a 3*3 table  
  104.  
  105. Note: the colspans of <td> may not be correct as I adapted this example  
  106. from another presentation  
  107. ********************************************************************************/  
  108. <?  
  109.  
  110. /*place here your code to query MySQL database...*/   
  111.  
  112. /*get results*/   
  113. if(strcmp($result, ''))  
  114.     $number=mysql_numrows($result);  
  115. else  
  116.      $number=0;  
  117.  
  118. /*maximum number of hits*/   
  119. $MAXHIT= 9;  
  120.  
  121. /*if more than MAXHIT results are found, split the results into several pages*/   
  122. $i=$MAXHIT*$p;  
  123.  
  124. /* --> $p is initialized to 0 if results.php3 is 1st called, but then it gets its value  
  125. from split.php3!*/   
  126.  
  127. print  " <table border='0'>\n";  
  128. print  "   <tr>\n";  
  129. print  "     <td>\n";  
  130.    /*remind query or show a sorry message if no result was found:*/   
  131.   print  "Your query: $qry.\n";  
  132. print  "     </td>\n";  
  133. print  "  </tr>\n";  
  134.  
  135. if($number) {  
  136.     print  "\n\t  <tr>\n";  
  137.     print  "\t    <td'>\n";  
  138.     if($number>1) {  
  139.            /*split header*/   
  140.            $plural= 's';  
  141.            $pluralbe= 'were';  
  142.       }  
  143.     else {  
  144.          $plural= '';  
  145.         $pluralbe= 'was';  
  146.     }  
  147.    
  148.      /*displays details about results*/   
  149.    
  150.     print  "\t\t$number result$plural $pluralbe found for this query\n";  
  151.     print  "\t\t<hr size='1'>\n";  print  "\t    </td>\n";  print  "\t  </tr>\n";  
  152.     print  "\t  <tr>\n";  
  153.     print  "\t    <td>\n";  
  154.  
  155.      /*category of result page to show when returning from split module*/   
  156.     $cat= "[optional parameters]";  
  157.     $urlinfo= "p=$p&_maxelmt=$number&MAXHIT=$MAXHIT&cat=".$cat;  
  158.    
  159.      /*calling split module*/   
  160.       "http://[your path]/split.php3?$urlinfo&res=1";  
  161.  
  162.     print  "\t  <tr>\n";  
  163.     print  "\t    <td>\n";  
  164.  
  165.     print  "\t\t<table border='0' cellspacing='1' cellpadding='1'>\n";  
  166.  
  167.      /*default recordset scanning is 0 to MAXHIT-1 (p=1)*/   
  168.     $i=$MAXHIT*($p-1);  
  169.  
  170.     while ($i<$MAXHIT*$p && $i < $number) {  
  171.         $mVar1=mysql_result($result,$i, "Var1");  
  172.         $mVar2=mysql_result($result,$i, "Var2");  
  173.             /*etc...*/   
  174.    
  175.         $index=$i+1;  
  176.    
  177.          /*format table*/   
  178.         if (!($i % 3)) {  
  179.             print  "\t\t  <tr>\n";  
  180.         }  
  181.         $mName= "$mVar1/$mVar2.html";  /*for instance...*/   
  182.  
  183.         print  "\t\t    <td>\n\t\t\t<a href='$mName'>\n";  
  184.            print  "</a>\n\t\t    </td>\n";  
  185.  
  186.             if (($i % 3) == 2) {  
  187.             print  "\t\t  </tr>\n";  
  188.         }  
  189.          /*next record*/   
  190.         $i++;  
  191.     }  /*end while*/   
  192.    
  193.      /*if table didn't end in a full row, append end of row*/   
  194.     if ($i % 3) {  
  195.                  print  "\t\t  </tr>\n";  
  196.     }  
  197.     print  "\t\t</table>\n";  
  198.    
  199.     print  "\t    </td>\n";  
  200.     print  "\t  </tr>\n";  
  201. }  /*end if $number*/   
  202.    
  203. else {  
  204.     print  "\t  <tr>\n";  
  205.     print  "\t    <td>\n";  
  206.          /*sorry message*/   
  207.         print  "\t\tSorry, no result was found in the database\n";  
  208.     print  "\t    </td>\n";  
  209.     print  "\t  </tr>\n";  
  210. }  
  211.  
  212. print  " </table>\n";  
  213. ?> 
  214.