...

View Full Version : Page script



ChadWick
11-30-2006, 01:41 AM
Im using the standard page script in php:




<?php

require("db.php");

db_connect();


$max_results = 2;

if(!isset($_GET['page'])){
$page = 1;
} else {
$page = $_GET['page'];
}

$from = (($page * $max_results) - $max_results);


$sql = mysql_query("SELECT id, uid, title, category_main, category_sub, description FROM `tutorials` LIMIT $from, $max_results") or die("MySQL Error");

while($row = mysql_fetch_array($sql))
{
echo( "<b>$row[title]</b><br>" );
echo( " submitted by $row[uid] in $row[category_main] / $row[category_sub]<br>" );
echo( "<i>$row[description]</i><br><br>" );

}

$total_results = mysql_result(mysql_query("select count(*) as Num FROM tutorials"),0);

$total_pages = ceil($total_results / $max_results);

if(($total_pages) < 2){ echo "..."; }
else{

echo "Page <b>$page</b> out of <b>$total_pages</b><br />";

if($page > 1){
$prev = ($page - 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\"><<Previous</a> ";
}

for($i = 1; $i <= $total_pages; $i++){
if(($page) == $i){
echo "$i ";
} else {
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ";
}
}

if($page < $total_pages){
$next = ($page + 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next>></a>";
}

}


db_disconnect();


?>


but what im trying to do the effect where it will display 5 page numbers at a time only the dot dot dot to the last and first two pages...

Example:

http://bin1.myphotoupload.com/pages.gif

it basicly displays the page your on then 2 pages ahead... 2 pages behind... the first 2 pages and last two pages....

Thanks

Tyree
11-30-2006, 03:33 AM
Well, pages 1 and 2 are easy...they'll always be there as long as you have at least 2 pages.

For the others, just do some simple math to determine which buttons to display. $total_pages gives you the last number, $total_pages - 1 gives you the next to last.

$current_page gives you the current page...-1, -2....+1, +2.
If ($current_page -1) > 3 then put in the first set of dots.
If ($current_page -1) < ($total_pages -2) then put in second set of dots.

Does that cover what you were trying to figure out, or do you want actual code? Looks like you know how to write the code though. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum