SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Nov 2009
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Convert this code into a google like pagination

    I have this RSS parsing code and it paginates , but only uses 'next' and 'previous' links instead of how google does it. I want it to read like this: first 1 2 3.... last

    PHP Code:
    <?php
    require_once('simplepie.inc');

    // Set your own configuration options as you see fit.
    $feed = new SimplePie();
    $feed->set_feed_url(array(
    link1
    link2
    .....
    ));
    $success $feed->init();



    // Make sure the page is being served with the right headers.
    $feed->handle_content_type();

    // Set our paging values
    $start = (isset($_GET['start']) && !empty($_GET['start'])) ? $_GET['start'] : 0// Where do we start?
    $length = (isset($_GET['length']) && !empty($_GET['length'])) ? $_GET['length'] : 5// How many per page?
    $max $feed->get_item_quantity(); // Where do we end?

    // When we end our PHP block, we want to make sure our DOCTYPE is on the top line to make
    // sure that the browser snaps into Standards Mode.
    ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "\">

    <html xmlns="" xml:lang="en-US" lang="en-US">
    <head>
    <title>SimplePie: Demo</title>

    <link rel="stylesheet" href="styles.css" type="text/css" media="screen, projector" />

    </head>

    <body>
    <div id="site">
    <?php
    // If we have an error, display it.
    if ($feed->error())
    {
    echo 
    '<div class="sp_errors">' "\r\n";
    echo 
    '<p>' htmlspecialchars($feed->error()) . "</p>\r\n";
    echo 
    '</div>' "\r\n";
    }
    ?>

    <?php if ($success): ?>
    <?php
    // get_items() will accept values from above.
    foreach($feed->get_items($start$length) as $item):
    $feed $item->get_feed();
    ?>

    <div class="chunk">

    <h4><?php if ($item->get_permalink()) echo '<a href="' $item->get_permalink() . '">'; echo $item->get_title(true); if ($item->get_permalink()) echo '</a>'?></h4>

    <p class="footnote">Source: <a href="<?php echo $feed->get_permalink(); ?>"><?php echo $feed->get_title(); ?></a> | <?putenv("TZ=US/Pacific"); echo $item->get_date('j M Y | g:i a'); ?></p>

    </div>

    <?php endforeach; ?>
    <?php 
    endif; ?>

    <?php
    // Let's do our paging controls
    $next = (int) $start + (int) $length;
    $prev = (int) $start - (int) $length;

    // Create the NEXT link
    $nextlink '<a href="?start=' $next '&length=' $length '">Next &raquo;</a>';
    if (
    $next $max)
    {
    $nextlink 'Next &raquo;';
    }

    // Create the PREVIOUS link
    $prevlink '<a href="?start=' $prev '&length=' $length '">&laquo; Previous</a>';
    if (
    $prev && (int) $start 0)
    {
    $prevlink '<a href="?start=0&length=' $length '">&laquo; Previous</a>';
    }
    else if (
    $prev 0)
    {
    $prevlink '&laquo; Previous';
    }

    // Normalize the numbering for humans
    $begin = (int) $start 1;
    $end = ($next $max) ? $max $next;
    ?>

    <p>Showing <?php echo $begin?>&ndash;<?php echo $end?> out of <?php echo $max?> | <?php echo $prevlink?> | <?php echo $nextlink?> | <a href="<?php echo '?start=' $start '&length=5'?>">5</a>, <a href="<?php echo '?start=' $start '&length=10'?>">10</a>, or <a href="<?php echo '?start=' $start '&length=20'?>">20</a> at a time.</p>
    </div>

    </body>
    </html>

  2. #2
    SitePoint Guru silver trophy JamesColin's Avatar
    Join Date
    May 2009
    Location
    Jomtien, Pattaya, Thailand
    Posts
    910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should use the $length and $max variables to know how many pages you'll have:
    $max / $length should give you the amount, ie.
    $max = 27 and $length = 5 :
    27 / 5 = 5.4 so you know you'll need 6 pages and the last page will have only 4 entries.
    Do you really need traffic? Where to? What for?
    If you really do need traffic then stop messing around!
    Advertise on my sites today: She Told Me & Best Reviewer :
    200,000+ UV / Month

  3. #3
    SitePoint Member
    Join Date
    Nov 2009
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    but how do I make it look like like: 1 2 3 .... max

  4. #4
    SitePoint Guru silver trophy JamesColin's Avatar
    Join Date
    May 2009
    Location
    Jomtien, Pattaya, Thailand
    Posts
    910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well it's not very simple and would involve some reflexion, but you get the idea, to use $max and $length variables to know how many pages you'll have to display.
    It's best for you in the long term if you can spend time thinking about it and trying to do it yourself and then post your code with what you've done so far.
    You can search and find snippets for pagination in php and study it and try to adapt it to your current code.
    Do you really need traffic? Where to? What for?
    If you really do need traffic then stop messing around!
    Advertise on my sites today: She Told Me & Best Reviewer :
    200,000+ UV / Month

  5. #5
    SitePoint Member
    Join Date
    Nov 2009
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found this snipet which seems similar to what i'm doing:

    wallpaperama(dot com)/forums/pagination-script-how-to-create-page-numbers-links-function-php-t5775.html

    I'm most interested in the pagination function since I'm sure I'm gonna need it

    So when calling the RSS what would be my 'total number of items'? Would it be $max or $feed? or something else?

    I'm almost there...( I think)

    $start = (isset($_GET['start']) && !empty($_GET['start'])) ? $_GET['start'] : 0; // Where do we start?
    $per_page = (isset($_GET['length']) && !empty($_GET['length'])) ? $_GET['length'] : 5; // How many per page?
    $max = $feed->get_item_quantity(); // Where do we end?


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
  •