Creating page numbers for paging

Hello,
I was wanting help after many trial and errors I only got this far.

How to display it so it looks like this.

(first page) 1 2 3 4 5 6 7 Next Last
(second page) First Previous 1 2* 3 4 5 6 7 Next Last

right now it looks like this … First Previous 0 1 2 Next Last

Let me know if you need more information.

Thanks in advance


<div id="paging">
	<div class="page">
		<?php if ($pageNum_getDisplay > 0) { // Show if not first page ?>
                  	<a href="<?php printf("%s?pageNum_getDisplay=%d%s", $currentPage, 0, $queryString_getDisplay); ?>">First</a>
                  	<?php } // Show if not first page ?></div>
	<div class="page">
		<?php if ($pageNum_getDisplay > 0) { // Show if not first page ?>
                  	<a href="<?php printf("%s?pageNum_getDisplay=%d%s", $currentPage, max(0, $pageNum_getDisplay - 1), $queryString_getDisplay); ?>">Previous</a>
                  	<?php } // Show if not first page ?></div>
	<div class="page">
		<?php for ($i=0;$i<=$pageNum_getDisplay;$i++){
		if ($pageNum_getDisplay==$i)
		echo ($i);
		else
		echo "<a title='Page $i' href='?pageNum_getDisplay=$i'><strong>$i</strong></a>";
		} ?> </div>
	<div class="page">
		<?php if ($pageNum_getDisplay < $totalPages_getDisplay) { // Show if not last page ?>
                  	<a href="<?php printf("%s?pageNum_getDisplay=%d%s", $currentPage, min($totalPages_getDisplay, $pageNum_getDisplay + 1), $queryString_getDisplay); ?>">Next</a></div>
	<div class="page">
		<?php } // Show if not last page ?>
              	<?php if ($pageNum_getDisplay < $totalPages_getDisplay) { // Show if not last page ?>
                  	<a href="<?php printf("%s?pageNum_getDisplay=%d%s", $currentPage, $totalPages_getDisplay, $queryString_getDisplay); ?>">Last</a>
                  	<?php } // Show if not last page ?>
            </div>
</div>
           
        if ($pageNum_getDisplay==$i)         
        echo ($i);      

So thats where it displays the current page. If you want to put a * there, change the echo line to include a *.

Hi starlion,
I tried using the asterik but it doesn’t seem to work Iget error.
I tried creating another php set telling it to display page numbers larger than the current page and Igot an infinite numbers causing it to slow down and freeze browser. Is there a way to limit it to only the amount of pages I have?

Where is $pageNum_getDisplay being set, and what code are you trying to use to show the asterisk?

<div class=“page”>
<?php for ($i=0;$i<=$pageNum_getDisplay;$i++){
if ($pageNum_getDisplay==$i)
echo ($i);
else
echo “<a title=‘Page $i’ href=‘?pageNum_getDisplay=$i’><strong>$i</strong></a>”;
} ?>
<?php for ($i=0;$i>=$pageNum_getDisplay; $i++){
if (pageNum_getDisplay < $i)
echo “<a title=‘Page $i’ href=‘?pageNum_getDisplay=$i’>$i</a>”;
} ?> </div>

this is the code i have currently that displays endless amount of pages beyond current page. i need to create a stop to this to the exact amount of pages and only show 5-10 pages at a time.

for the * i just replaced the $i with what the portion of code you picked out from my thing.

so you put echo(“*”); ? Should work.
That piece of code you show doesnt say where $pageNum_getDisplay comes from. It must be set further up in the script.

okay, the * works to show current page, but the problem I have now is showing the next 3 pages ahead of the current page.

right now I have it displayed like this.

on first page it looks like this … 0* Next Last
when i click next it looks like this…First Previous 0 1* Next Last

I would like to add another set of code to show 5 page links so it looks like this…First Previous 0 1* 2 3 4 5 Next Last

thanks for the help by the way. and the pageNum_getDisplay comes from my php code on top. it’s a replacement for $_GET.

the key to that is your for loop.


        <?php for ($i=0;$i<=$pageNum_getDisplay;$i++){

This loop reads: “Start at 0. Go until you equal the current page. Step by 1”.
From your post, you want to do exactly 5 pages, ideally centered around the current page.

I’ll give you the logic, and lets see if you can implement it.

So we need to do some maths. We need to know a start (which i’m going to call $start . Feel free to call it whatever you want.), and an end.
Now from your code we know a couple of things:
$totalPages_getDisplay is the total number of pages.
$pageNum_getDisplay is the current page.
5 is the number of links you want to display.

I’m going to give you a 3-piece piecemeal function for $start:
$start =:
0, if current page <= 2
totalpages - 5, if current page >= totalpages-2
currentpage - 2, otherwise.

And one for end…
end =:
$start+5, if totalpages > 5
totalpages, otherwise.

okay, I’m not sure if i’m writing this out correctly as I’m new to this. here is what i’ve written.

<?php
$start = 0;
if($pageNum_getDisplay <= $totalPages_getDisplay - 5);
elseif ($pageNum_getDisplay >= $totalPages_getDisplay - $pageNum_getDisplay - 2)
echo $start;
?>
<?php
$end = $start + 5;
if (totalPages_getDisplay > $totalPages_getDisplay);
?>

Okay I got it to work with this code, but is there anyway to make it display the current page as 1 instead of 0 and have it still put up pageNum_getDisplay=0.

<?php
$start = 0;
if($pageNum_getDisplay <= $totalPages_getDisplay - 5);
elseif ($pageNum_getDisplay >= $totalPages_getDisplay - $pageNum_getDisplay - 2);
?>
<?php
$end = $start + 5;
if (totalPages_getDisplay > $totalPages_getDisplay);
?>
<div class=“page”>
<?php for ($i=$start;$i<=$end;$i++){
if ($pageNum_getDisplay==$i)
echo “<a title=‘Page $i’ href=‘?pageNum_getDisplay=$i’>($i)</a>”;
else
echo “<a title=‘Page $i’ href=‘?pageNum_getDisplay=$i’><strong>$i</strong></a>”;
} ?>
</div>

Thanks for all the help StarLion

well your ifs arnt actually doing anything at the moment.

As far as making it display one value higher, easiest way is to add 1 to maxpages, subtract 1 from the currentpages value used to pull page content, and then your minimum start should be 1.

Hi StarLion,
I have another question. Is there a way to only display the pages I have instead of what it’s doing now and showing 5 automatically. Thanks.

If you implement the logic for $end correctly, it would.