Jump to content

Recommended Posts

I've made a page which lists information from a database like:

 

1 Andy

2 James

3 Bob

 

But if James over takes Andy it goes

 

1 James

2 Andy

3 Bob.

 

I have that working.

 

However, I've added multi pages into my website [1, 2, 3, 4, 5] etc, however, when you go onto a new page, it starts back at number 1. It doesn't continue adding 1 on.

 

Does anyone know how to make this work?

 

Thanks in advance,

Andy.

Link to comment
https://forums.phpfreaks.com/topic/256505-muti-pages-1-2-3-with-counter/
Share on other sites

Right, i have a variable called

$position = 1;

 

Then I also have a while look like

while($fetch = mysql_fetch_assoc($query))
{
echo $position++.' '.$fetch['username'];
}

 

So if I had 4 usernames in the database with other values like cash it would be like:

 

1 Andy £100

2 Dan £98

3 Bob £23

4 James £10

 

etc, so it shows who has the highest amount of money.

 

However, I've added that multi-page so that theres only 20 names per page.

 

When I go onto a new page, insted of going

 

21 Name £

22 Name £

23 Name £

 

it goes

 

1 Name £

2 Name £

3 Name £

 

etc. I want the numbers to increase throughout the other pages.

you've got to pass that end position to the new page as it's start

so on the link to move to the next age, add

 

?position=$position

so that it will put whatever the current position is there, with your link code i can be more specific

 

 

and then on the subsequent pages, you want to first make sure position isn't being set to 1, but also make sure it is being set to whatever the end position was

 

so in the beginning

<?php
$position = $_GET['position'];
?>

 

 

if all you need is the position, that's great. but if you need more info you may consider saving it to a session or a db. or passing a serialized array, which is my preferred quick and dirty method.

i'm sorry what exactly do you mean upping the number? can i see your next page link code?the foreach doesn't actually track the postition

 

 

so instead of:

while($fetch = mysql_fetch_assoc($query)){echo $position++.' '.$fetch['username'];}

do:

<?php
$position = 1 ;
while($fetch = mysql_fetch_assoc($query)){echo $position.' '.$fetch['username'];
$position ++;} 
?>

 

that way position will actually keep track of the current position, rather than just echoing it.

Hello, heres my next page links

 

// if not on page 1, don't show back links
if ($currentpage > 1) {
   // show << link to go back to page 1
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'>First</a> ";
   // get previous page num
   $prevpage = $currentpage - 1;
   // show < link to go back to 1 page
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
} // end if 

// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
   // if it's a valid page number...
   if (($x > 0) && ($x <= $totalpages)) {
      // if we're on current page...
      if ($x == $currentpage) {
         // 'highlight' it but don't make a link
         echo " [<b>$x</b>] ";
      // if not current page...
      } else {
         // make it a link
         echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x&position=".$position."'>$x</a> ";
      } // end else
   } // end if 
} // end for
                 
// if not on last page, show forward and last page links        
if ($currentpage != $totalpages) {
   // get next page
   $nextpage = $currentpage + 1;
    // echo forward link for next page 
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";
   // echo forward link for lastpage
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>Last</a> ";
} // end if

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.