|
-
Oct 4th, 2005, 10:42 PM
#1
Thread Starter
PowerPoster
Pagination
I've found the below code and modified it to my needs and everything is displayed the way I want it. (well nearly!) The pagination looks OK if there are no more than 4/5 pages (eg Page 3 of 5 < 1 2 3 4 5 >) however if there are a large number of pages I don't want it to look like (Page 7 of 10 < 1 2 3 4 5 6 7 8 9 10 > ) but rather like (Page 7 of 10 < 6 7 8 9 10 >)
Hopefully I've explained myself clear enough
PHP Code:
<?php
@mysql_connect($localhost, $user, $password) or die("ERROR--CAN'T CONNECT TO SERVER");
@mysql_select_db($database) or die("ERROR--CAN'T CONNECT TO DB");
$limit = 25;
$query_count = "SELECT count(*) FROM table";
$result_count = mysql_query($query_count);
$totalrows = mysql_num_rows($result_count);
if(empty($page)){
$page = 1;
}
$limitvalue = $page * $limit - ($limit);
$query = "SELECT * FROM table LIMIT $limitvalue, $limit";
$result = mysql_query($query) or die("Error: " . mysql_error());
if(mysql_num_rows($result) == 0){
echo("Nothing to Display!");
}
$bgcolor = "#E0E0E0"; // light gray
echo("<table>");
while($row = mysql_fetch_array($result)){
if ($bgcolor == "#E0E0E0"){
$bgcolor = "#FFFFFF";
}else{
$bgcolor = "#E0E0E0";
}
echo("<tr bgcolor=".$bgcolor.">n<td>");
echo($row["users"]);
echo("</td>n<td>");
echo($row["usersID"]);
echo("</td>n</tr>");
}
echo("</table>");
if($page != 1){
$pageprev = $page--;
echo("<a href=\"$PHP_SELF&page=$pageprev\">PREV".$limit."</a> ");
}else{
echo("PREV".$limit." ");
}
$numofpages = $totalrows / $limit;
for($i = 1; $i <= $numofpages; $i++){
if($i == $page){
echo($i." ");
}else{
echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");
}
}
if(($totalrows % $limit) != 0){
if($i == $page){
echo($i." ");
}else{
echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");
}
}
if(($totalrows - ($limit * $page)) > 0){
$pagenext = $page++;
echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT".$limit."</a>");
}else{
echo("NEXT".$limit);
}
mysql_free_result($result);
?>
Last edited by lintz; Oct 5th, 2005 at 10:02 PM.
-
Oct 5th, 2005, 12:34 AM
#2
Re: Pagination
Have you seen the pagination example here? The Hobo wrote a function which generates the links and it works very well.
http://www.vbforums.com/showthread.p...51#post1988751
-
Oct 5th, 2005, 12:48 AM
#3
Thread Starter
PowerPoster
Re: Pagination
I tried that code but got an error when the code got to $db = new SiteDb; and I don't follow this part either
PHP Code:
if (! $result = $db->query($query)) {
echo($db->error());
die('Query error.');
}
which is why I went searching for another example.
-
Oct 5th, 2005, 12:52 AM
#4
Re: Pagination
You just need to replace those functions with the equivilent mysql functions. So,
$db = new SiteDb -----> mysql_connect()
$db->query() ----------> mysql_query()
$db->fetch_assoc() ----> mysql_fetch_assoc()
-
Oct 5th, 2005, 12:56 AM
#5
Thread Starter
PowerPoster
Re: Pagination
I'll give it a shot 
Does the example The Hobo wrote do what my original post ask?
I see you've changed the example
Last edited by lintz; Oct 5th, 2005 at 01:35 AM.
-
Oct 5th, 2005, 01:00 AM
#6
Re: Pagination
Did you have a look at the link in the post. You can see exactly what it does:
http://adam.codedv.com/examples/pagination.php
-
Oct 5th, 2005, 01:13 AM
#7
Thread Starter
PowerPoster
Re: Pagination
my mistake
-
Oct 5th, 2005, 02:12 AM
#8
Re: Pagination
 Originally Posted by lintz
I'll give it a shot
Does the example The Hobo wrote do what my original post ask?
I see you've changed the example 
I should have edited it before I posted it. 
The DB lcass is just a wrapper for the MySql functions. But as it wraps the functions you can also make one for Oracle, ODBC and other packages and just switch the object when required
-
Oct 5th, 2005, 07:21 PM
#9
Thread Starter
PowerPoster
Re: Pagination
I can't get Hobo's code to work.
Here is my situation.
TotalRecords = 32
ItemsPerPage = 10
PagesToShow per pagination = 3
Overall there are 4 pages to display.
Page 1 works OK. (Page 1 of 4 << < 1 2 3 > >> )
Page 2 doesn't as it shows page 4 when it shouldn't (Page 2 of 4 << < 1 2 3 4 > >> )
Page 3 works OK. (Page 3 of 4 << < 2 3 4 > >> )
Page 4 doesn't as it deosn't show page 2 (Page 4 of 4 << < 3 4 > >>)
Any suggestions?
-
Oct 5th, 2005, 10:03 PM
#10
Thread Starter
PowerPoster
Re: Pagination
I modified Hobos code and it now works the way it should
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|