Description: An easy page browser class that produces this type of utput: ( prev 6 7 8 9 10 next )
Comment: (none)
Language: PHP Highlight Mode: PHP Last Modified: March 01st, 2009
<?PHP/*
-----------
Description
-----------
The function returns an array of 3 values as described below:
VALUE 1 [0]: Returns a string that can be appended to your SQL query so that the
correct block of rows can be returned. Eg. " LIMIT 10, 20"
VALUE 2 [1]: Returns an html string that gives the user an indication of which rows
he's currently browsing. Eg. 11-20
VALUE 3 [2]: Returns an html navigation bar needed for navigating through the total
result returned Eg. Page: << 6 7 8 9 10 >>
---------
Arguments
---------
The first 4 arguments are declared/assigned by you while the last 3 are done when
you start using the navigation bar generated by the function (See 'Usage' below).
totalrows - The TOTAL amount of rows returned by your executed SQL statement
numLimit - The amount of page numbers you'd like displayd in your navigation bit
Eg. '4' would display "Page: 1 2 3 4 Next 4 >>"
amm - The amount of rows returned per page (for the SQL string to append)
queryStr - If you'd like extra vars to be passed to that page Eg. "&name=value"
There are also 3 variables inside the function you can change if you'd like the
navigation bar to look a little different (larrow,rarrow,wholePiece).
-----
Usage
-----
See 'Usage' at the end of this document.
*/FUNCTION pageBrowser($totalrows,$numLimit,$amm,$queryStr,$numBegin,$begin,$num){$larrow=" << Prev ".$numLimit." ";//You can either have an image or text, eg. Previous $rarrow=" Next ".$numLimit." >> ";//You can either have an image or text, eg. Next $wholePiece="Page: ";//This appears in front of your page numbers IF($totalrows>0){$numSoFar=1;$cycle=CEIL($totalrows/$amm);IF(!ISSET($numBegin)||$numBegin<1){$numBegin=1;$num=1;}$minus=$numBegin-1;$start=$minus*$amm;IF(!ISSET($begin)){$begin=$start;}$preBegin=$numBegin-$numLimit;$preStart=$amm*$numLimit;$preStart=$start-$preStart;$preVBegin=$start-$amm;$preRedBegin=$numBegin-1;IF($start>0||$numBegin>1){$wholePiece.="<a href='?num=".$preRedBegin."&numBegin=".$preBegin."&begin=".$preVBegin.$queryStr."'>".$larrow."</a>\n";}FOR($i=$numBegin;$i<=$cycle;$i++){IF($numSoFar==$numLimit+1){$piece="<a href='?numBegin=".$i."&num=".$i."&begin=".$start.$queryStr."'>".$rarrow."</a>\n";$wholePiece.=$piece;BREAK;}$piece="<a href='?begin=".$start."&num=".$i."&numBegin=".$numBegin.$queryStr."'>";IF($num==$i){$piece.="</a><b>$i</b><a>";}ELSE{$piece.="$i";}$piece.="</a>\n";$start=$start+$amm;$numSoFar++;$wholePiece.=$piece;}$wholePiece.="\n";$wheBeg=$begin+1;$wheEnd=$begin+$amm;$wheToWhe="<b>".$wheBeg."</b> - <b>";IF($totalrows<=$wheEnd){$wheToWhe.=$totalrows."</b>";}ELSE{$wheToWhe.=$wheEnd."</b>";}$sqlprod=" LIMIT ".$begin.", ".$amm;}ELSE{$wholePiece="Sorry, no records to display.";$wheToWhe="<b>0</b> - <b>0</b>";}RETURNARRAY($sqlprod,$wheToWhe,$wholePiece);}/* /////////////////////////////////////////////////////
| Usage |
NOTE!: This is just an example and WON'T WORK if you include
this in your code, comment it out or delete it :)
*/$criterea=" WHERE column=value AND column_2=value_2";$sql="SELECT COUNT(*) AS totalrows FROM table".$criterea;//get the total amount of rows returned $arr=MYSQL_FETCH_ARRAY(MYSQL_QUERY($sql));/////////////////////////////////////////////////////////*
Call the function:
I've used the global $_GET array as an example for people
running php with register_globals turned 'off' :)
*/$navigate= pageBrowser($arr[totalrows],10,10,"&name=value",$_GET[numBegin],$_GET[begin],$_GET[num]);//execute the new query with the appended SQL bit returned by the function $sql="SELECT * FROM table".$criterea.$navigate[0];$rs=MYSQL_QUERY($sql);//the indication of which rows are being browsed. Eg. listing 1-10 of 100 results. ECHO"<p>Listing ".$navigate[1]." of ".$totalrows." results.</p>";//the navigation bar returned by the function ECHO"<p>".$navigate[2]."</p>";//loop and display the limited records being browsed WHILE($arr=MYSQL_FETCH_ARRAY($rs)){ECHO$arr[column_1]." ".$arr[column_2]."<br>";}//the navigation bar at the bottom again ECHO"<p>".$navigate[2]."</p>";?>
SQL University.net courses meet the most demanding needs of the business world for advanced education
in a cost-effective manner. SQL University.net courses are available immediately for IT professionals
and can be taken without disruption of your workplace schedule or processes.
Compared to traditional travel-based training, SQL University.net saves time and valuable corporate
resources, allowing companies to do more with less. That's our mission, and that's what we deliver.