Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jan 2010
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Pagination...or rather not!



    Hi Guys,

    Thought I had finally nailed this but no..sniff

    Coded below to paginate ( left at 2 items per page to start with just to test) and i thought I had cracked it...alas!

    [CODE]
    <?php

    include("rhino.inc");
    // Connects to your Database
    $connection = mysql_connect($host, $user, $password)
    or die ("couldn't connect to server");
    $db = mysql_select_db($database, $connection)
    or die ("couldn't connect to database");

    //This checks to see if there is a page number. If not, it will set it to page 1
    if (!(isset($pagenum)))
    {
    $pagenum = 1;
    }

    //Here we count the number of results
    //Edit $data to be your query
    $data = mysql_query("SELECT * FROM `news` ORDER BY `date` DESC") or die(mysql_error());
    $rows = mysql_num_rows($data);

    //This is the number of results displayed per page
    $page_rows = 2;

    //This tells us the page number of our last page
    $last = ceil($rows/$page_rows);

    //this makes sure the page number isnt below one, or more than our maximum pages
    if ($pagenum < 1)
    {
    $pagenum = 1;
    }
    elseif ($pagenum > $last)
    {
    $pagenum = $last;
    }

    //This sets the range to display in our query
    $max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;

    //This is your query again, the same one... the only difference is we add $max into it
    $data_p = mysql_query("SELECT * FROM `news` ORDER BY `date` DESC $max") or die(mysql_error());

    //This is where you display your query result

    while ($news = mysql_fetch_array( $data_p )) {
    $news_id = $news[urn];
    $news_date = stripslashes($news[date]);
    $news_author = stripslashes($news[author]);
    $news_title = stripslashes($news[title]);
    $news_snippet = stripslashes($news[snippet]);
    {
    Print $info['Name'];
    echo "<table border='0' width='550'>";


    echo "<tr>
    <td>$news_title</td>
    </tr>
    <tr>
    <td> date added $news_date by $news_author </td>
    </tr>

    <tr>
    <td>$snippet</td>
    </tr>
    <tr>
    <td><a href=\"shownews.php?news_id=$news_id\"><img src='/images/clickhereformore.png' width='185' height='25' border='0' /></a></td>
    </tr>
    <tr><td><hr></td></tr>";
    }
    echo "</table>";
    }
    // This shows the user what page they are on, and the total number of pages
    echo " --Page $pagenum of $last-- <p>";

    // First we check if we are on page one. If we are then we dont need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
    if ($pagenum == 1)
    {
    }
    else
    {
    echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
    echo " ";
    $previous = $pagenum-1;
    echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
    }

    //just a spacer
    echo " ---- ";

    //This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
    if ($pagenum == $last)
    {
    }
    else {
    $next = $pagenum+1;
    echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
    echo " ";
    echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
    }
    ?>
    [CODE]


    Whilst it does only display 2 items and has the right number of pages ( 4) when i go to page 2 etc the records displayed do not change =(

    May be a simple thing but i cant get it!

    in case anyone wants to know what i mean a current url is

    www.hainaultbulldogs.co.uk/document2.php

    Thanks

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,853
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Code:
    if (!(isset($pagenum)))
    {
    $pagenum = 1;
    }
    Are you relying on register_globals? Try
    PHP Code:
    if (!isset($_GET['pagenum']))
    {
    $pagenum 1;
    }
    else 
     
    $pagenum =(int) $_GET['pagenum'] ; 
    instead.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    New Coder
    Join Date
    Jan 2010
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts
    abduraooft I loves ya! lol

    cheers for that! was going mad!



  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •