Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Convert this code into a google like pagination
Old 11-15-2009, 07:50 PM Convert this code into a google like pagination
Junior Talker

Posts: 3
Trades: 0
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

Code:
<?php
require_once('simplepie.inc');
 
// Set your own configuration options as you see fit.
$feed = new SimplePie();
$feed->set_feed_url(array(
'http://feeds2.feedburner.com/MishsGlobalEconomicTrendAnalysis',
'http://feeds.feedburner.com/TheBigPicture',
'http://feeds2.feedburner.com/ChartsAndCoffee',
        'http://feeds.feedburner.com/typepad/tradeblogs/the_slope_of_hope_with_ti',
));
$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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" 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 < 0 && (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>
MyMarket is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-16-2009, 01:45 PM Re: Convert this code into a google like pagination
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
You can use my pagination class if you like. It requires that you know how many records exist, and requires that you can control how many records show up per page:
PHP Code:
<?php
class Pagination {
    public 
$menu;
    function 
__construct($count$opt = array()) {
        
$this->saveList($count$opt);
    }

    public function 
returnList($total_number_records$options = array()) {
        
$defaults = array(
            
'prefix' => '',//string which precedes page number in list
            
'activeClass' => 'active',//if we're on this page, anchor carries this class
            
'key' => 'p',//?key=value
            
'assignKey' => '{key}={value}',
            
'limit' => 10,//how many records per page
            
'listcount' => 10,//how many pages to show in list
            
'wrap' => 'li',//html entity wrapper for each page number
            
'seperator' => " ",//seperation deliminator
            
'page' => $_SERVER['PHP_SELF'],//page targeted by pagination
            
'parseGet' => true,
            
'nextLink' => 'Next >',//next link text or HTML
            
'prevLink' => '< Prev'//same as above
        
);
        
$opt array_merge($defaults$options);
        
$pattern str_replace('{key}''%1$s'$opt['assignKey']);
        
$pattern str_replace('{value}''%2$d'$pattern);
        
        
$q_sym '';
        if(
$opt['parseGet']) {
            
$request_page explode("?"$opt["page"]);
            
$q_sym "?";
            if(
sizeof($request_page 1)) {
                
$sym "?";
                
$opt["page"] = $request_page[0];
                foreach(
$_GET as $k => $v) {
                    if (
$k == $opt["key"]) continue;
                    
$opt["page"] .= $sym.$k."=".$v;
                    
$sym "&";
                    
$q_sym "&";
                }
            }
        }

        
//first, some math (where are we?)
        
$id = (!empty($_GET[$opt["key"]])) ? $_GET[$opt["key"]]: 1;
        
$here $id $opt["limit"];
        
$bridgeleft $here - ($opt["limit"] * (floor($opt["listcount"] / 2)));
        
$bridgeleft = ($bridgeleft 0) ? $bridgeleft;
        
$bridgeright $bridgeleft + ($opt["limit"] * (ceil($opt["listcount"])));

        
$oldbridgeright $bridgeright;
        
$bridgeright = ($bridgeright $total_number_records) ? $total_number_records $bridgeright;
        
$bridgeright ceil($bridgeright $opt["limit"]) * $opt["limit"];// round up to nearest $opt["limit"]

        
$diff $oldbridgeright $bridgeright;
        
//
        
if($diff $opt["limit"] * $opt["listcount"]) {
            
$bridgeleft -= $diff;
            
$bridgeleft = ($bridgeleft 0) ? $bridgeleft;
        }

        
$last ceil($total_number_records $opt["limit"]);
        
$idinc $id 1;
        
$iddeinc $id 1;
        
$seperate_beginning '';
        if(
$bridgeleft && $bridgeleft 10) {
            
$seperate_beginning ' ... ';
        }
        elseif(
$bridgeleft >= 10) {
            
$div_num floor($bridgeleft 2);
            
//$opt["key"].'='.$div_num
            
$seperate_beginning ' .. '."<".$opt["wrap"].'> <a href="'.$opt["page"].$q_sym.sprintf($pattern$opt['key'], $div_num).'">'.$div_num.'</a> </'.$opt["wrap"].">".' .. ';
        }
        
        
$nextoption = ($id != $last) ? "<".$opt["wrap"].'> <a href="'.$opt["page"].$q_sym.sprintf($pattern$opt['key'], $idinc).'">'.$opt["nextLink"].'</a> </'.$opt["wrap"].">" "";
        
$prevoption = ($id != 1) ? "<".$opt["wrap"].'> <a href="'.$opt["page"].$q_sym.sprintf($pattern$opt['key'], $iddeinc).'">'.$opt["prevLink"].'</a> </'.$opt["wrap"].">" "";
        
$optional_first = ($id != 1) ? "<".$opt["wrap"].'> <a href="'.$opt["page"].$q_sym.sprintf($pattern$opt['key'], 1).'">First</a>'.$seperate_beginning.'</'.$opt["wrap"].">" '';
        
$list $prevoption.$nextoption.$optional_first;
        
$inc $bridgeleft $opt["limit"];
        
$seperate "";
        for(
$i $bridgeleft$i $bridgeright$i += $opt["limit"]) {
            
$optional = ($id == ++$inc) ? 'class="'.$opt["activeClass"].'" ' "";
            
$list .= "<".$opt["wrap"].'>'.$seperate.'<a '.$optional.'href="'.$opt["page"].$q_sym.sprintf($pattern$opt['key'], $inc).'">'.$opt["prefix"].$inc.'</a></'.$opt["wrap"].">";
            
$seperate $opt["seperator"];
        }
        
$seperate_end '';
        if(
$last $inc 10 && $last $inc 1) {
            
$seperate_end ' ... ';
        }
        elseif(
$last $inc >= 10) {
            
$div_num ceil(($last $inc) / 2);
            
$seperate_end ' .. '."<".$opt["wrap"].'> <a href="'.$opt["page"].$q_sym.$opt["key"].'='.$div_num.'">'.$div_num.'</a> </'.$opt["wrap"].">".' .. ';
        }
        if(
$id != $last) {
            
$list .= "<".$opt["wrap"].'>'.$seperate_end.'<a href="'.$opt["page"].$q_sym.sprintf($pattern$opt['key'], $last).'">Last</a> </'.$opt["wrap"].">";
        }
        return 
$list;
    }

    public function 
saveList($count$opt = array()) {
        
$this->menu $this->returnList($count$opt);
    }
}
?>
To use this class you're going to probably have to do something like this:
PHP Code:
//calculate how many records
$count 100;//just an example
$per_page 20;//20 records per page
$paginate = new Pagination($count, array(
    
'limit' => $per_page,
    
'key' => 'page'//this will be the $_GET var that we calculate with
)) 
You'll need to take the $_GET['page'], along with the $per_page to calculate $start and $length instead of what you're doing currently.

Don't have time for a more detailed explanation, though I'm sure you can get some good help here from those who can read PHP well. All of the options for my class can be read from the $defaults array().

Once you've intialized the Paginate class, it can be displayed anywhere on the page like this:

PHP Code:
<ul>
<?php echo $paginate->menu;?>
</ul>
By default, it is an unordered list, which means it will have to be styled to be inline. You can choose a different HTML entity to wrap each item with the 'wrap' option if you wish.
__________________
Join me on
Please login or register to view this content. Registration is FREE
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 11-16-2009, 02:01 PM Re: Convert this code into a google like pagination
orionoreo's Avatar
Ultra Talker

Posts: 335
Name: Jerry
Trades: 0
I made a script a while back as well that is similar to what wayfare has there... samething you need to feed the page result and total through... there's a guide and run through of the script here

http://www.crankberryblog.com/2009/s...ing-pagination
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
orionoreo is offline
Reply With Quote
View Public Profile
 
Old 11-16-2009, 02:55 PM Re: Convert this code into a google like pagination
Junior Talker

Posts: 3
Trades: 0
Everyime I try to maodify my code I get a blank page

I tried this:

PHP Code:
<?php
require_once('simplepie.inc');
 
// Set your own configuration options as you see fit.
$feed = new SimplePie();
$feed->set_feed_url(array(
    
'http://feeds2.feedburner.com/MishsGlobalEconomicTrendAnalysis',
    
'http://feeds.feedburner.com/TheBigPicture',
    
'http://feeds2.feedburner.com/ChartsAndCoffee',
        
'http://feeds.feedburner.com/typepad/tradeblogs/the_slope_of_hope_with_ti',
));
$success $feed->init();



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

$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?
$paginate = new Pagination($max, array(
    
'limit' => $per_page,
    
'key' => 'page'//this will be the $_GET var that we calculate with
Is the page total somewhere in here:

[PHP]$max = $feed->get_item_quantity(); // Where do we end?
[/[PHP]

Last edited by MyMarket; 11-16-2009 at 02:57 PM..
MyMarket is offline
Reply With Quote
View Public Profile
 
Old 11-16-2009, 03:44 PM Re: Convert this code into a google like pagination
orionoreo's Avatar
Ultra Talker

Posts: 335
Name: Jerry
Trades: 0
$per_page is the amount of entries to show per page...

i don't see the rest of the code so i'm not sure where $start and $max is used
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
orionoreo is offline
Reply With Quote
View Public Profile
 
Old 11-16-2009, 04:41 PM Re: Convert this code into a google like pagination
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Quote:
Originally Posted by wayfarer07 View Post
You'll need to take the $_GET['page'], along with the $per_page to calculate $start and $length instead of what you're doing currently.
Also, I'm not seeing where you're including my class in any way where it is then usable.

It might be useful for us to know your skill level. Do you know any PHP at all, or are you just copying and pasting stuff to try to make things work?
__________________
Join me on
Please login or register to view this content. Registration is FREE
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Reply     « Reply to Convert this code into a google like pagination
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.51846 seconds with 12 queries