Jump to content

[SOLVED] MySQL Paged Results (PHP Coded Links)


Salis

Recommended Posts

All right well it's been a while since I've done Pagination. And so far so good, but I'm in need of a little help with the links. Right now all links are show on the page but I really don't want to do that. What I would like are links based on pages. I want to show some thing like below:

<< 4 5 [6] >>

 

I want to group the links according to the pages so if I'm on page 1, 2 or 3 those links are show however if I click the >> link a new group of 4, 5 and 6 are show and so on. Any idea on how to do this?

 

Thanks all phpfreaks

Sure. This is a "simulated Query" by the way. If that makes since. I find it point less to query when I'm trying to create the links... Any way, This is what I have so far. I think I may need to use 2 for statements to get this working but I'm not sure how to generate the next group.

 

 

<?php

$xPage = (!is_numeric($_GET['page'])) ? 1 : $_GET['page'];

$Total_Posts = 54; // Simulated Query Row count
$Posts_Per_Page = 5;

$Total_Pages = ceil($Total_Posts / $Posts_Per_Page);

$Start_Pull = ($xPage -1) * $Posts_Per_Page; // - $Posts_Per_Page;

$SQL_LIMIT = "LIMIT $Start_Pull, $Posts_Per_Page";


echo "You're on page $xPage. There are a total of $Total_Posts posts on $Total_Pages pages. The SQL LIMIT is "$SQL_LIMIT".<br><br>";

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


?>

<?php
$page = $_GET['page'];

$show = 3;

echo "<< ";

if ($page > $show/2) {
$start = $page-round($show/3);;
} else {
$start = $page;
}

for ($i=$start;$i<$start+$show;$i++) {
    if ($i == $page) {
         print " [$i] ";
    } else {
         print " $i ";
    }
}

echo " >>";
?>

 

Took me a while to get it working, a bit tricky :P

 

Try this on a separate page and see if this is what you're looking for.

 

edit: changed it a bit to make it dynamic.

 

demo: http://www.infamousx.com/test.php?page=4

Hah, this took me a few minutes as well...so I might as well post it.

 

<?php

$xPage = 3;
$prev_page = $xPage-1;
$next_page = $xPage+1;	
$prev_group = $xPage-3;
$next_group = $xPage+3;

//Generate Links
echo "<a href='{$_SERVER['PHP_SELF']}?page=$prev_group'><< </a>";
echo "<a href='{$_SERVER['PHP_SELF']}?page=$prev_page'>$prev_page </a>";
echo "[$xPage] ";
echo "<a href='{$_SERVER['PHP_SELF']}?page=$next_page'>$next_page </a>";
echo "<a href='{$_SERVER['PHP_SELF']}?page=$next_group'>>></a> ";

?>

 

It might take a little bit more coding...but it works =]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.