This is a discussion on Pagination Tutorial within the PHP Language forums, part of the PHP Programming Forums category; Newbie question. I have recently tried a php tutorial on pagination, but I just cannot get the links to work. ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Newbie question.
I have recently tried a php tutorial on pagination, but I just cannot get the links to work. See www.freeweekends.co.uk/pagtest2.php. The search finds over 60 results, but only the first page of ten are displayed, with the Prev and Next links not working. Can anyone help, or has anyone another pagination tutorial? Thanks Alec <?php $connection = @mysql_connect('localhost', '*****', '*****'); if (!$connection) { echo '<p>Unable to make database connection.</p>'; exit(); } if (!@mysql_select_db('uks49179')) { exit('<p>Unable to locate database.</p>'); } $limit = 10; $query_count = "SELECT count(*) FROM companyid_uks49179 WHERE town='Bury St. Edmunds' AND category='sleep' AND priority='0'"; $result_count = mysql_query($query_count); $totalrows = mysql_num_rows($result_count); if(empty($page)){ $page = 1; } $limitvalue = $page * $limit - ($limit); $query = "SELECT company FROM companyid_uks49179 WHERE town='Bury St. Edmunds' AND category='sleep' AND priority='0' 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)){ echo $row['company']."<br />"; } 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); ?> |
|
|||
|
On Tue, 16 Oct 2007 13:40:32 +0200, Alec <ajtdds@aol.com> wrote:
> I have recently tried a php tutorial on pagination, but I just cannot > get the links to work. See www.freeweekends.co.uk/pagtest2.php. The > search finds over 60 results, but only the first page of ten are > displayed, with the Prev and Next links not working. > > Can anyone help, or has anyone another pagination tutorial? > > Thanks > > Alec > > <?php > $connection = @mysql_connect('localhost', '*****', '*****'); > if (!$connection) { > echo '<p>Unable to make database connection.</p>'; > exit(); > } > if (!@mysql_select_db('uks49179')) { > exit('<p>Unable to locate database.</p>'); > } > > $limit = 10; > $query_count = "SELECT count(*) FROM companyid_uks49179 WHERE > town='Bury St. Edmunds' AND category='sleep' AND priority='0'"; > $result_count = mysql_query($query_count); > $totalrows = mysql_num_rows($result_count); $totalrows here should become FALSE or 1 according to the code, never, ever, anything else. $query_count = "SELECT count(*) FROM companyid_uks49179 WHERE town='Bury St. Edmunds' AND category='sleep' AND priority='0'"; $result_count = mysql_query($query_count); $totalrows = mysql_result($result_count,0,0); > if(empty($page)){ register_globals should be disabled, and is probably the problem. Or have you filled $page somewhere else? > $page = 1; > } > > $limitvalue = $page * $limit - ($limit); I certainly hope you check $page > 0? And make sure it's an integer, floats in limit clauses can't be the goal:) -- Rik Wasmus |