pagination problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
twinx
Forum Newbie
Posts: 1
Joined: Fri Dec 08, 2006 12:14 pm

pagination problem

Post by twinx »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Can someone please tell me what is wrong with this code, i'm trying to display 5 results per page from my database query... But at the moment i'm only getting 1 entry displaying...

Code: Select all

<?php 

    if (isset($_GET['pageno'])) {
       $pageno = $_GET['pageno'];
    } else {
       $pageno = 1;
    } // if


    $query = "SELECT count(*) FROM tracks";
    $result= mysql_query($query) or die ("couldn't get you an answer");


    $num_rows = mysql_num_rows($result);
    $query_data = mysql_fetch_row($result);



    $rows_per_page = 5;
    $lastpage      = ceil($num_rows/$rows_per_page);

    $pageno = (int)$pageno;
    if ($pageno < 1) {
       $pageno = 1;
    } elseif ($pageno > $lastpage) {
       $pageno = $lastpage;
    } // if

    $limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;

    $query = "SELECT * FROM tracks $limit";
    $result = mysql_query($query, $db) or trigger_error("SQL", E_USER_ERROR);




    if ($pageno == 1) {
       echo " FIRST PREV ";
    } else {
       echo " <a href='{$_SERVER['PHP_SELF']}?pageno=1'>FIRST</a> ";
       $prevpage = $pageno-1;
       echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage'>PREV</a> ";
    } // if

    echo " ( Page $pageno of $lastpage ) ";


    if ($pageno == $lastpage) {
       echo " NEXT LAST ";
    } else {
       $nextpage = $pageno+1;
       echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage'>NEXT</a> ";
       echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$lastpage'>LAST</a> ";
       }
    ?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Post by califdon »

I would try using a while loop.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Code: Select all

$num_rows = mysql_num_rows($result);
$num_rows will always equal 1 (the query is returning count(*)). Try

Code: Select all

mysql_result($result,0)
Post Reply