View Full Version : Pagination Display
Titan85
01-15-2007, 04:00 AM
I recently created a pagination script and its working nicely, but I am having issues displaying it where I want it to be. Here is the thing, I had to put the code above the where my data is being echoed because I have a while statement that needs the query to run before it. Because of this, the page links are displayed above the rest of my code. I am hoping there is a way that I can display the pagination at the end of my code instead. Here is the pagination code I am using in case it is needed:
if($_GET['page']) { // Is the page defined?
$page = $_GET['page']; // Set page defined
} else {
$page = 1; // Default page is 1
}
$max = 1; // Max results on a page
$cur = (($page * $max) - $max); // Works out what results to show
$sql = "SELECT * FROM `users` ORDER BY `id` DESC LIMIT $cur, $max";
$result = mysql_query($sql) or die ('Error selecting users from the database!' .mysql_error());
$count_total = mysql_query("SELECT * FROM `users` "); // Get data from database
$count_total = mysql_num_rows($count_total); // Count lines in database
$total_pages = ceil($count_total / $max); // Total results divided by max to display
if($page > 1) { // Is the page # more than one?
$prev = ($page - 1); // If yes, take one away from current page
echo '<a href="?page='.$prev.'">Previous</a>'; // Echo link to previous page
}
for($i == 1; $i <= $total_pages; $i++) // For each page number
if($page == $i) { // If page = current page
echo '<b>'.$i.'</b>'; // Echo page in bold
} else {
echo ' <a href="?page='.$i.'">'.$i.'</a> '; // Echo link to the page
}
if($page < $total_pages) { // Is there another page?
$next = ($page + 1); // If so, add 1 to current page
echo '<a href="?page='.$next.'">Next>></a>'; // Echo next page link
}
I appreciate any help on this
thetestingsite
01-15-2007, 05:08 AM
Instead of echoing the links, try assigning them to variables, then echo the variables at the bottom of the script.
Hope this helps.
Titan85
01-15-2007, 11:03 PM
Instead of echoing the links, try assigning them to variables, then echo the variables at the bottom of the script.
Hope this helps.I tried that, but am not really sure how to do it. Could I get a point in the right direction? Thanks
thetestingsite
01-15-2007, 11:11 PM
if($_GET['page']) { // Is the page defined?
$page = $_GET['page']; // Set page defined
} else {
$page = 1; // Default page is 1
}
$max = 1; // Max results on a page
$cur = (($page * $max) - $max); // Works out what results to show
$sql = "SELECT * FROM `users` ORDER BY `id` DESC LIMIT $cur, $max";
$result = mysql_query($sql) or die ('Error selecting users from the database!' .mysql_error());
$count_total = mysql_query("SELECT * FROM `users` "); // Get data from database
$count_total = mysql_num_rows($count_total); // Count lines in database
$total_pages = ceil($count_total / $max); // Total results divided by max to display
if($page > 1) { // Is the page # more than one?
$prev = ($page - 1); // If yes, take one away from current page
$prevTxt = '<a href="?page='.$prev.'">Previous</a>';
}
for($i == 1; $i <= $total_pages; $i++) // For each page number
if($page == $i) { // If page = current page
$curPage = '<b>'.$i.'</b>'; // Echo page in bold
} else {
$thePages = '<a href="?page='.$i.'">'.$i.'</a> '; // Echo link to the page
}
if($page < $total_pages) { // Is there another page?
$next = ($page + 1); // If so, add 1 to current page
$nextTxt = '<a href="?page='.$next.'">Next>></a>'; // Echo next page link
}
Then where you want the text at, add the following:
echo $prevTxt.$curPage.$thePages.$nextTxt;
Hope this helps.
mburt
01-15-2007, 11:16 PM
You must "echo" after defining the variables though, they must be down lower on the page, then the original code defining the variables.
Titan85
01-15-2007, 11:25 PM
That kinda worked, but the display is messed up. For example, on page 3 the page numbers look like this: Previous 3 2. And on page 1 it is: 1 3. Any idea what to do?
thetestingsite
01-15-2007, 11:31 PM
Try changing $curPage to $thePages in your for loop. and where you echoed the variables, remove $curPage. See if that fixes it.
Titan85
01-16-2007, 12:56 AM
Try changing $curPage to $thePages in your for loop. and where you echoed the variables, remove $curPage. See if that fixes it.Still no good :( . I am not sure what else to do
thetestingsite
01-16-2007, 01:19 AM
Ok, having looked at it a little further and modifying the code a bit, try the following:
if($_GET['page']) { // Is the page defined?
$page = $_GET['page']; // Set page defined
} else {
$page = 1; // Default page is 1
}
$max = 1; // Max results on a page
$cur = (($page * $max) - $max); // Works out what results to show
$sql = "SELECT * FROM `users` ORDER BY `id` DESC LIMIT $cur, $max";
$result = mysql_query($sql) or die ('Error selecting users from the database!' .mysql_error());
$count_total = mysql_query("SELECT * FROM `users` "); // Get data from database
$count_total = mysql_num_rows($count_total); // Count lines in database
$total_pages = ceil($count_total / $max); // Total results divided by max to display
if($page > 1) { // Is the page # more than one?
$prev = ($page - 1); // If yes, take one away from current page
$prevTxt = '<a href="?page='.$prev.'">Previous</a>';
}
$nav = ' ';
for($i == 1; $i <= $total_pages; $i++) // For each page number
if($page == $i) { // If page = current page
$nav .= '<b>'.$i.'</b>'; // Echo page in bold
} else {
$nav .= '<a href="?page='.$i.'">'.$i.'</a> '; // Echo link to the page
}
if($page < $total_pages) { // Is there another page?
$next = ($page + 1); // If so, add 1 to current page
$nextTxt = '<a href="?page='.$next.'">Next>></a>'; // Echo next page link
}
echo $prevTxt . $nav . $nextTxt;
That should work. Hope it helps.
Titan85
01-16-2007, 01:28 AM
Thanks, it works great that way. Only one more question. In the part where the previous page link is made, if I make the text for the link "<<Previous", it only shows up as "<". Is there any reason for this, like maybe the <<?
thetestingsite
01-16-2007, 01:30 AM
Well, you could try using the HTML codes for the brackets > is I believe > and < is I believe <
That would probably fix that.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.