Results 1 to 4 of 4

Thread: Showing nearest 5 results of for() loop?

  1. #1
    Join Date
    Nov 2008
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Showing nearest 5 results of for() loop?

    Would that be possible?

    I have this:
    PHP Code:
    $viewpage $_GET['viewpage'];
    $records_per_page 14;
    $offset = ($viewpage-1) * $records_per_page;
    $id is_numeric($_GET['id']);

    $count_result mysql_query("SELECT COUNT(*) FROM entries WHERE tid='$id'"); // $id is the post ID displayed in the URL
    $count_row mysql_fetch_array($count_result);
    $count $count_row["COUNT(*)"];
    for(
    $i=1$i<=$count/$records_per_page$i++){
        
    $i2 $i+1//wait for it...
        
    echo '<a class="page" href="?id='.$_GET["id"].'&amp;viewpage='.$i.'">'.$i.'</a>';
    }    echo 
    '<a class="page" href="?id='.$_GET["id"].'&amp;viewpage='.$i2.'">'.$i2.'</a>'//one page is always missing 
    What that does is create page links, and splits MySQL results across multiple pages. It works fine, but having 100 links at the bottom of the page can be pretty cluttered. Is there a way so it displays the closest 5 numbers?

    For example, if you were on page 10:
    [10]|[11]|[12]|[13]|[14]|[15]
    ...And so on. I've tried tinkering with a few if() statements and messing with the math parts, but to no success. Any ideas?

    Woohoo. Thanks.
    Last edited by MrEvil; 11-13-2008 at 04:42 PM.

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    $margin = 2;
    
    for ($i = $viewpage - $margin;
         $i <= $count / $records_per_page && $i <= $viewpage + $margin;
         ++$i)
      ...
    Last edited by Twey; 11-14-2008 at 03:53 AM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. The Following User Says Thank You to Twey For This Useful Post:

    MrEvil (11-14-2008)

  4. #3
    Join Date
    Nov 2008
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    You.Are.Awesome. That works perfectly. The only thing I did was change || to &&, so it displays the page numbers before and after the current one (which is even better than what I originally wanted). Thank you soooo much.

  5. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Oh yes, of course. Silly me. Sorry.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •