SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    May 2001
    Location
    United Kingom
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    creating multiple page links

    Ok, simple problem but possibly a not so simple answer??

    I have a page set up so that it displays a review if an id number is given in the url else it prints the complete list of review titles for the viewer to choose from. Of course this will become a problem as the number of reviews grow as the list will be too big for one page.

    here's the part of my code that is working but I would like to extend:

    PHP Code:
    $review mysql_query("SELECT id, date, tshort FROM news ORDER by id DESC");
        while(list(
    $id$date$tshort) = mysql_fetch_row($review))
        echo 
    "<a href=index.php?id=$id>$date - $tshort</a><br>"
    I'm looking for a way to display 5 reviews per page only, then place a link at the bottom taking you to the next five and so on.

    I think I know what is required but don't know how to code it properly. can sombody help.

    I'm guessing that I first need to use count(*) where it says mysql_query("SELECT etc... from what I understand this will get me the total number of records. But how do I then get this total figure out into a variable to play with? $total_no_of_reviews=[count] <-that can't be right

    I'm also assuming I need to define some other variables like so...

    $reviews_per_page = 5;
    $total_number_of_pages = ceil($total_no_of_reviews/$reviews_per_page );
    $current_page = 0;
    $next_page = $current_page + 1;

    As you can see I'm lost. I know it's possible but I'm not sure where to start. Can somebody help me extend the aboveworking code to include this "5 reviews" per page code.

    Thanks,
    Paul.
    http://www.djsource.co.uk - the complete dj source.

  2. #2
    SitePoint Addict manipura's Avatar
    Join Date
    Apr 2001
    Location
    Calgary,AB
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is the script I used to display 5 results per page
    I hope it helps you out. If there are any problems E-mail me at: mroberts@calgarymarket.com



    $result = mysql_query("SELECT * FROM table,$db);
    $row_count = mysql_num_rows($result);
    $pages = floor(($row_count-1)/5)+1;
    if ( $page)
    {
    $start_entry = ($page * 5) - 5;
    }
    else
    {
    $page = 1;
    $start_entry = 1;
    }
    $result = mysql_query("SELECT * FROM Table LIMIT $start_entry,5",$db);

    echo "<BR><center>";
    if ($row_count <> 0)
    {
    echo "<pre><table width=100% border=1>";
    echo "<tr><td align=center>Total Entries: $row_count<br>";
    echo "Displaying Page $page of $pages</td></tr>";
    echo "<tr><td>";

    echo "<table width=100%>\n";
    echo "<tr><td width=30%><B>Company Name</td><td width=70%><B>Description</tr>\n";
    while ($myrow = mysql_fetch_array($result))
    {
    $variable = $myrow["field"];

    echo ( "<tr><td><font size='-1'><B>title</B></a>&nbsp;</font></td><td><font size='-1'description</font></td></tr>");
    }
    echo "</table></td></tr>\n";

    echo "<tr><td><table width=100%>";
    if ($pages > 1)
    {
    echo "<table width=100%>";
    $next_page = $page + 1;
    $last_page = $page - 1;
    if ($page == "1")
    {
    echo "<tr><td width=50% align=center><A href='javascript:history.go(-1)'><b>Back</b></A></td><td width=50% align=center><a href='category.php3?category=$category&page=$next_page'>NEXT PAGE</a></td></tr>";
    }
    elseif ($page == $pages)
    {
    echo "<tr><td width=50% align=center><a href='./category.php3?category=$category&page=$last_page'>PREVIOUS PAGE</a></td><td width=50%>&nbsp;</td></tr>";
    }
    else
    {
    echo "<tr><td width=50% align=center><a href='category.php3?category=$category&page=$last_page'>PREVIOUS PAGE</a></td><td width=50% align=center><a href='category.php3?category=$category&page=$next_page'>NEXT PAGE</a></td></tr>";
    }
    echo "</table>";
    }
    echo "</td></tr></table></pre>\n";
    }
    else
    {
    echo("<br><center><h2><font color=red>There are currently no entries in this category.</h2></font>");
    }
    echo "</center>\n";

    mysql_free_result ($result);

    ?>

  3. #3
    SitePoint Addict manipura's Avatar
    Join Date
    Apr 2001
    Location
    Calgary,AB
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    fix it

    Now that was an old script that I used.. But there is an error. It will miss the first record the query finds. So to fix that you have to change one thing.

    $start_entry = 1;

    Change this to:

    $start_entry = 0;

  4. #4
    SitePoint Member
    Join Date
    May 2001
    Location
    United Kingom
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Phew!, I finally manged to get some kind of resultfrom your code but things aren't working 100%.

    At the moment most things are working, I can see what page I'm on, it tells me 'Displaying page 1 of 3' but there is an error occuring ....

    Warning: Undefined variable: page on line 60
    I scrolled down to line 60 and it's not there . I can see the following on line 62... if ($page) which looks like it could be the problem .

    Could somebody take a look over the section of code I'm using to see if they can spot the mistake

    PHP Code:
        $review mysql_query("SELECT id FROM news"); 
        
    $review_count mysql_num_rows($review);
        
    $total_num_pages ceil($review_count/5);
        if (
    $page
          { 
            
    $start_entry = ($page 5) - 5
          } 
        else 
          { 
            
    $page 1
            
    $start_entry 0
          } 

        
    $review mysql_query("SELECT id, tshort, img, intro FROM news ORDER by id DESC LIMIT $start_entry,5"); 

        if (
    $review_count <> 0
          { 
            echo 
    "<table width=100% border=1>"
            echo 
    "<tr><td align=center>Total Reviews: $review_count<br>"
            echo 
    "Displaying Page $page of $total_num_pages</td></tr>"
            echo 
    "<tr><td>"
            echo 
    "<table width=100% border=0>\n"

            while(list(
    $id,$tshort,$img,$intro) = mysql_fetch_row($review)) 
              {  
                echo (
    "<tr><td valign=top><a href=index.php?id=$id><img src=$img hspace=5 width=92 border=0></a></td><td><b>$tshort</b><br>$intro<br><a href=index.php?id=$id><img src=../nav/go.gif width=28 height=9 border=0 vspace=5></a><p></td></tr>"); 
              } 

            echo 
    "</table></td></tr>\n"
            echo 
    "<tr><td>"
        
            if (
    $total_num_pages 1
              { 
                echo 
    "<table width=100% border=0>"
                
    $next_page $page 1
                
    $last_page $page 1
             
                if (
    $page == "1"
                  { 
                  echo 
    "<tr><td width=50% align=center><A href='javascript:history.go(-1)'><b>Back</b></a></td><td width=50% align=center><a href='index.php?page=$next_page'>NEXT PAGE</a></td></tr>"
                  } 
                  elseif (
    $page == $total_num_pages
                  { 
                  echo 
    "<tr><td width=50% align=center><a href='index.php?page=$last_page'>PREVIOUS PAGE</a></td><td width=50%></td></tr>"
                  } 
                  else 
                  { 
                  echo 
    "<tr><td width=50% align=center><a href='index.php?page=$last_page'>PREVIOUS PAGE</a></td><td width=50% align=center><a href='index.php?page=$next_page'>NEXT PAGE</a></td></tr>"
                  } 
                  echo 
    "</table>"
              } 
                echo 
    "</td></tr></table>\n"
            } 

        else 
          { 
          echo(
    "<center><h2><font color=red>There are currently no entries in this table.</font></h2></center>"); 
          } 
    Let me know if you need any more info. This is starting to hurt my brain now, <insert cry for help>

    Paul.
    Last edited by djsource; May 30, 2001 at 11:38.
    http://www.djsource.co.uk - the complete dj source.

  5. #5
    SitePoint Member
    Join Date
    May 2001
    Location
    United Kingom
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, a little update...

    I've managed to get the links working so that I can now view all the pages correctly by following the PREVIOUS NEXT links at the bottom of the page.

    In fact all seems to be working apart from one little thing....

    If I view news/index.php?page=1 // this works fine
    If I view news/index.php?page=2 // this works fine
    If I view news/index.php?page=3 // this works fine too

    but if I veiw news/index.php without passing the page number I get the same little error... Warning: Undefined variable: page

    Any ideas how to solve this last little bit?

    Paul.
    http://www.djsource.co.uk - the complete dj source.

  6. #6
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    With the :

    if($page) {

    change that to:

    if(!isset($page)) {

    and see if that fixes your problem.

  7. #7
    SitePoint Member
    Join Date
    May 2001
    Location
    United Kingom
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by petesmc
    With the :

    if($page) {

    change that to:

    if(!isset($page)) {

    and see if that fixes your problem.
    I was actually reading about this just as you replied. I did try your suggestion:

    if(!isset($page)) {

    but that didn't work, a little change however and it did(without the ! )

    if(isset($page)) {

    Whoohoooo! Multi page city here we come!! <big cheesy grin>

    Thanks for the help petesmc and manipura

    Paul.
    http://www.djsource.co.uk - the complete dj source.


Bookmarks

Posting Permissions

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