Mega Code Archive

 
Categories / Php / MySQL Database
 

Html split bar used to split in multiple pages a database result

This module prints a common split href menu, like those used to display into several pages the results of a search engine. For example, you have a page results.php3 that prints a collection of results, and for more convenience you want to write several pages from this result, each grouping MAXHIT results. So you paste this code into a file split.php3, then you write in results.php3: include("[...]/split.php3"); at the exact location where you want the split bar to display. Don't forget to modify your results page results.php3 to display only the results i to i+MAXHIT-1 ! Code of split.php3: arguments: _maxelmt: number of elements in the collection MAXHIT : number of max hits for a page p : current page to be seen cat (optional): optional parameters to send at return <? /*if MAXHIT=0 print a message error*/ if(!$MAXHIT) { print "MAXHIT=0 : no split available!\n"; } else { if($res && $_maxelmt) { $end=$p*$MAXHIT; if($_maxelmt < $p*$MAXHIT) $end=$_maxelmt; print "\t\t?Results ".(max(1,($p-1)*$MAXHIT+1)). '-'.$end. '??<br>'. "\n"; } /*bypass if there's no more than one page*/ if ($_maxelmt> $MAXHIT) { /* pattern: Results i'-i'+MAXHIT [<<Prev] | 1 --page+1 [2 --page+2 ...] | [Next>>] Page i of n */ /*<<Prev*/ /*if p > 1, Prev is active, otherwise it's not*/ print "\t\t<font face='Arial' size='1'"; if (!($p-1)) { print " color='#646464'><<Prev?|?\n"; } /*light gray*/ else { /*prev*/ $prevpage=$p-1; $anurl = "results.php3?_maxelmt=$_maxelmt&MAXHIT=$MAXHIT&p=$prevpage&$cat"; print " color='#008000' ><a href=$anurl><<Prev</a>?|?\n"; } print "\t\t</font>". "\n"; /*print a page number every MAXHIT*/ for($i=0;$i<$_maxelmt;$i+=$MAXHIT) { /*page number i+1*/ $pi=($i/$MAXHIT)+1; /*don't activate current page's href*/ if($pi==$p) { print "<font face ='Arial' size='1' color=#646464>$pi?"; } else { $anurl = "results.php3?_maxelmt=$_maxelmt&MAXHIT=$MAXHIT&p=$pi&$cat"; print "<font face ='Arial' size='1' color=#008000 >\n"; print "\t\t<a href=$anurl>".$pi. '?</a>'. "\n"; } print "\t\t</font>". "\n"; } /*Next>>*/ /*if p*MAXHIT doesn't meet or exceed MAXHIT, Next is active, otherwise it's not*/ print "\t\t<font face ='Arial' size='1'"; if (($_maxelmt-$p*$MAXHIT) <= 0) { print " color='#646464'>| Next>>\n"; } /*light gray*/ else { /*next*/ $nextpage=$p+1; $anurl = "results.php3?_maxelmt=$_maxelmt&MAXHIT=$MAXHIT&p=$nextpage&$cat"; print ">|?<a href=$anurl>Next>></a>\n";} print '</font>'; } if($_maxelmt) { print "\t\t<font face ='Arial' size='1' color='#008000' >?Page ".$p. ' of '.ceil($_maxelmt/$MAXHIT). '</font>'. "\n"; } } /*if ($_maxelmt> $MAXHIT) } /*end lse if(!$MAXHIT)*/ ?> /*Example of use in results.php3: 9 hits maximum, in a 3*3 table Note: the colspans of <td> may not be correct as I adapted this example from another presentation **/ <? /*place here your code to query MySQL database...*/ /*get results*/ if(strcmp($result, '')) $number=mysql_numrows($result); else $number=0; /*maximum number of hits*/ $MAXHIT= 9; /*if more than MAXHIT results are found, split the results into several pages*/ $i=$MAXHIT*$p; /* --> $p is initialized to 0 if results.php3 is 1st called, but then it gets its value from split.php3!*/ print " <table border='0'>\n"; print " <tr>\n"; print " <td>\n"; /*remind query or show a sorry message if no result was found:*/ print "Your query: $qry.\n"; print " </td>\n"; print " </tr>\n"; if($number) { print "\n\t <tr>\n"; print "\t <td'>\n"; if($number>1) { /*split header*/ $plural= 's'; $pluralbe= 'were'; } else { $plural= ''; $pluralbe= 'was'; } /*displays details about results*/ print "\t\t$number result$plural $pluralbe found for this query\n"; print "\t\t<hr size='1'>\n"; print "\t </td>\n"; print "\t </tr>\n"; print "\t <tr>\n"; print "\t <td>\n"; /*category of result page to show when returning from split module*/ $cat= "[optional parameters]"; $urlinfo= "p=$p&_maxelmt=$number&MAXHIT=$MAXHIT&cat=".$cat; /*calling split module*/ "http://[your path]/split.php3?$urlinfo&res=1"; print "\t <tr>\n"; print "\t <td>\n"; print "\t\t<table border='0' cellspacing='1' cellpadding='1'>\n"; /*default recordset scanning is 0 to MAXHIT-1 (p=1)*/ $i=$MAXHIT*($p-1); while ($i<$MAXHIT*$p && $i < $number) { $mVar1=mysql_result($result,$i, "Var1"); $mVar2=mysql_result($result,$i, "Var2"); /*etc...*/ $index=$i+1; /*format table*/ if (!($i % 3)) { print "\t\t <tr>\n"; } $mName= "$mVar1/$mVar2.html"; /*for instance...*/ print "\t\t <td>\n\t\t\t<a href='$mName'>\n"; print "</a>\n\t\t </td>\n"; if (($i % 3) == 2) { print "\t\t </tr>\n"; } /*next record*/ $i++; } /*end while*/ /*if table didn't end in a full row, append end of row*/ if ($i % 3) { print "\t\t </tr>\n"; } print "\t\t</table>\n"; print "\t </td>\n"; print "\t </tr>\n"; } /*end if $number*/ else { print "\t <tr>\n"; print "\t <td>\n"; /*sorry message*/ print "\t\tSorry, no result was found in the database\n"; print "\t </td>\n"; print "\t </tr>\n"; } print " </table>\n"; ?>