Woodburn2006 Posted August 10, 2006 Share Posted August 10, 2006 i did this tutorial for paginating: http://www.phpfreaks.com/tutorials/43/0.phpbut i cannot get the next and prev buttons to work, they appear and have 1 as a page number at the bottom but there is 3 pages. i can put ?page=3 onto the address and it moves me to page 3 but it is the actual next and prev buttons that do not work. this is the code[code] 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); } [/code]any ideas on this? Quote Link to comment Share on other sites More sharing options...
bltesar Posted August 10, 2006 Share Posted August 10, 2006 Are you sure the NEXT button doesn't work? I found a typo for the PREV button:[code]echo("<a href=\"$PHP_SELF&page=$pageprev\">PREV".$limit."</a> "); [/code]should be:[code]echo("<a href=\"$PHP_SELF?page=$pageprev\">PREV".$limit."</a> "); [/code]notice the & has changed to a ? Quote Link to comment Share on other sites More sharing options...
Woodburn2006 Posted August 10, 2006 Author Share Posted August 10, 2006 http://www.dancingledgegallery.co.uk/tester.phpthat is where it is, there is about 53 entries in the DB. if you put ?page=2 or 3 at the end of the address you will see how it gets the records fine but the next prev etc dont workthanks Quote Link to comment Share on other sites More sharing options...
SharkBait Posted August 10, 2006 Share Posted August 10, 2006 Out put your variables.I had to do that when I was working with my pagnation scripts.I found that my $prevpage kept equalling my $pageSo I changed $prevpage to be set up like[code]<?php$prevpage = $page - 1;?>[/code]Which is odd cause I thought $page--; would work but it didnt seem to for some reason??? Quote Link to comment Share on other sites More sharing options...
HeyRay2 Posted August 10, 2006 Share Posted August 10, 2006 Okay, so you're not getting your Prev and Next to show up as links, and you're not getting links to your pages printed out.I tested your page by adding "?page=<pagenumber>" to the URL, but I'm not seeing the results change.Can you post the code you are using to define your [b]$page[/b], [b]$totalrows[/b], and [b]$limit[/b] variables? Quote Link to comment Share on other sites More sharing options...
Woodburn2006 Posted August 10, 2006 Author Share Posted August 10, 2006 [code] $limit = 25; $query_count = "SELECT count(*) FROM gallery"; $result_count = mysql_query($query_count); $totalrows = mysql_num_rows($result_count); if(empty($page)){ $page = 1; } [/code]the prev works now that i changed $pageprev to $page - 1;but there are 3 pages in total and it only shows 1 page Quote Link to comment Share on other sites More sharing options...
HeyRay2 Posted August 10, 2006 Share Posted August 10, 2006 Try these changes. I changed some [b]echo()[/b] formatting, added some page validation checking, and removed some redundant code.[code=php:0] $numofpages = ceil($totalrows / $limit); // Check that the page requested is a valid page number if($page < 1){ $page = 1; } if($page > $numofpages){ $page = $numofpages; } if($page > 1){ $pageprev = $page--; echo ("<a href=\"$PHP_SELF&page=".$pageprev."\">PREV".$limit."</a> "); }else{ echo ("PREV".$limit." "); } for($i = 1; $i <= $numofpages; $i++){ if($i == $page){ echo($i." "); }else{ echo("<a href=\"$PHP_SELF?page=".$i."\">$i</a> "); } } if($page < $numofpages){ $pagenext = $page++; echo("<a href=\"$PHP_SELF?page=".$pagenext."\">NEXT".$limit."</a>"); }else{ echo("NEXT".$limit); }[/code]Let us know how this works. Quote Link to comment Share on other sites More sharing options...
king arthur Posted August 10, 2006 Share Posted August 10, 2006 [quote author=SharkBait link=topic=103739.msg413329#msg413329 date=1155229573]Out put your variables.I had to do that when I was working with my pagnation scripts.I found that my $prevpage kept equalling my $pageSo I changed $prevpage to be set up like[code]<?php$prevpage = $page - 1;?>[/code]Which is odd cause I thought $page--; would work but it didnt seem to for some reason???[/quote]The reason that doesn't work is because what it actually does is assigns the value in $page to $prevpage, and then decrements $page. So if you started with $page = 2, you would end up with $prevpage = 2 and $page = 1. Not what you wanted. Quote Link to comment Share on other sites More sharing options...
HeyRay2 Posted August 10, 2006 Share Posted August 10, 2006 Good catch, [b]king arthur[/b].Make these changes to the code I posted:[code=php:0] $pageprev = $page--; $pagenext = $page++;[/code]to this:[code=php:0] $pageprev = $page - 1; $pagenext = $page + 1;[/code]and you should be good to go... ;) Quote Link to comment Share on other sites More sharing options...
Woodburn2006 Posted August 10, 2006 Author Share Posted August 10, 2006 still wont work.when i changed the code you said all it would give me is the first page and there was no other pages when i typed ?page=2 or 3.so i removed this code and i could get to the 2 next pages:[code] if($page < 1){ $page = 1; }[/code]but i still cant get it to workthis is the full code:[code]<?php @mysql_connect(localhost, <USER>, <PASS>) or die("ERROR--CAN'T CONNECT TO SERVER"); @mysql_select_db(<DB>) or die("ERROR--CAN'T CONNECT TO DB"); $limit = 25; $query_count = "SELECT count(*) FROM gallery"; $result_count = mysql_query($query_count); $totalrows = mysql_num_rows($result_count); if(empty($page)){ $page = 1; } $limitvalue = $page * $limit - ($limit); $query = "SELECT * FROM gallery 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."><td>"); echo($row["name"]); echo("</td></tr>"); } echo("</table>"); $numofpages = ceil($totalrows / $limit); // Check that the page requested is a valid page number if($page > $numofpages){ $page = $numofpages; } if($page > 1){ $pageprev = $page - 1; echo ("<a href=\"$PHP_SELF&page=".$pageprev."\">PREV".$limit."</a> "); }else{ echo ("PREV".$limit." "); } for($i = 1; $i <= $numofpages; $i++){ if($i == $page){ echo($i." "); }else{ echo("<a href=\"$PHP_SELF?page=".$i."\">$i</a> "); } } if($page < $numofpages){ $pagenext = $page + 1; echo("<a href=\"$PHP_SELF?page=".$pagenext."\">NEXT".$limit."</a>"); }else{ echo("NEXT".$limit); } mysql_free_result($result); ?> [/code]i removed the user and pass for security ;) Quote Link to comment Share on other sites More sharing options...
bltesar Posted August 10, 2006 Share Posted August 10, 2006 $prevpage=$page--; is equivalent to $prevpage=$page;$page=$page-1;which is why you need to use:$prevpage=$page-1; Quote Link to comment Share on other sites More sharing options...
bltesar Posted August 10, 2006 Share Posted August 10, 2006 here's the trouble[code] $limit = 25; $query_count = "SELECT count(*) FROM gallery"; $result_count = mysql_query($query_count); $totalrows = mysql_num_rows($result_count);[/code]because you are selecting COUNT(*), the result of this query will only have one row.do this-[code]$row=mysql_fetch_array($result_count, MYSQL_ASSOC);$totalrows=$row['COUNT(*)'];[/code] Quote Link to comment Share on other sites More sharing options...
Woodburn2006 Posted August 10, 2006 Author Share Posted August 10, 2006 finally got there, what you just said to do did not seem to work either so i just removed the count(*) and just used * and it works finethanks for all your help everyone Quote Link to comment Share on other sites More sharing options...
HeyRay2 Posted August 10, 2006 Share Posted August 10, 2006 The method posted by [b]bltesar[/b] will work if you make change to your query:[code=php:0] $query_count = "SELECT count(*) AS rowcount FROM gallery"; [/code]and change the [b]$totalrows[/b] to read this:[code=php:0]$totalrows=$row['rowcount'];[/code]so your query code would read like this:[code=php:0] $query_count = "SELECT count(*) AS rowcount FROM gallery"; $result_count = mysql_query($query_count); $row = mysql_fetch_array($result_count, MYSQL_ASSOC); $totalrows = $row['rowcount'];[/code]also, your code for printing a [b]Prev[/b] link will not work, because you have a [b]&[/b] where you need a [b]?[/b]. Change this line:[code=php:0] echo ("<a href=\"$PHP_SELF&page=".$pageprev."\">PREV".$limit."</a> "); [/code]to this:[code=php:0] echo ("<a href=\"$PHP_SELF?page=".$pageprev."\">PREV".$limit."</a> ");[/code] Quote Link to comment Share on other sites More sharing options...
bltesar Posted August 10, 2006 Share Posted August 10, 2006 What I originally wrote should work fine, except that these things are case sensitive. The query should be SELECT COUNT(*)...Using that with totalrows=$row['COUNT(*)'] is preferred over just doing a SELECT *... because the latter approach will take up more resources because it actually gets all the contents of all the rows. Quote Link to comment Share on other sites More sharing options...
Woodburn2006 Posted August 10, 2006 Author Share Posted August 10, 2006 cool thanks1 last thing, i want to order my results by the name field, how do i do this? i have tried this but get errors[code]$query = "SELECT * FROM gallery (LIMIT $limitvalue, $limit) order by 'name'"; [/code] Quote Link to comment Share on other sites More sharing options...
HeyRay2 Posted August 10, 2006 Share Posted August 10, 2006 [code=php:0]$query = "SELECT * FROM gallery ORDER BY name LIMIT $limitvalue, $limit";[/code] Quote Link to comment Share on other sites More sharing options...
SharkBait Posted August 10, 2006 Share Posted August 10, 2006 And then depending on how you want to sort them it can be either ASC (ascending) or DESC (descending) [code=php:0]$query = "SELECT * FROM gallery ORDER BY name DESC LIMIT $limitvlaue, $limit";// OR$query ="SELECT * FROM gallery ORDER BY name ASC LIMIT $limitvalue, $limit";[/code]=-) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.