Zend Paginator与Gdata Youtube

时间:2010-12-24 03:21:49

标签: zend-framework youtube gdata zend-gdata

Zend_Paginator如何根据变量查询的交换工作?

第8行执行单次提取,即使更改查询变量也不会更改。

如何根据gdata feed的start-index进行分页功能?

代码:http://pastebin.com/rmxSP1Us

            $yt = new Zend_Gdata_YouTube();

        $limit = 12;
        $offset = ($page - 1) * $limit + 1;

        $query = "http://gdata.youtube.com/feeds/api/users/aculinario/favorites?start-index=$offset";

        $paginator = Zend_Paginator::factory($yt->getVideoFeed($query));
        $paginator->setCurrentPageNumber($page);
        $paginator->setItemCountPerPage($limit);
        $paginator->setPageRange(6);            
        $this->view->paginator = $paginator;

        echo $query // query changes but paginator no, every time Zend_Paginator factory should check the returned array of getVideoFeed, but not this checking
Sry,我的英语很差,我正在尝试

1 个答案:

答案 0 :(得分:2)

我有类似的工作使用快速&脏的paginator适配器。

值得注意的是,可能有更好,更通用的方法来实现这一目标。但如果你赶时间,这将让你前进。

<?php
class Lib_Paginator_Adapter_YoutubeUser implements Zend_Paginator_Adapter_Interface
{
    protected $_username;
    protected $_results;

    public function __construct($username)
    {
        $this->_username = $username;
    }

    public function getItems($offset, $itemCountPerPage)
    {
        $url = sprintf(
                '%s/%s/%s',
                Zend_Gdata_YouTube::USER_URI,
                $this->_username,
                Zend_Gdata_YouTube::UPLOADS_URI_SUFFIX
        );  
        try 
        {          
            $query = new Zend_Gdata_Query($url);
            $query->setMaxResults($itemCountPerPage)
                ->setStartIndex($offset);
            $youtube  = new Zend_Gdata_YouTube();
            $this->_results = $youtube->getUserUploads(null, $query);
            return $this->_results;
        }
        catch (Exception $ex)
        {
            echo $ex->getMessage();
            exit;
        }
    }

    public function count()
    {
        try 
        { 
            $youtube  = new Zend_Gdata_YouTube();
            return $youtube->getUserUploads($this->_username)->getTotalResults()->getText();
        }
        catch (Exception $ex)
        {
            echo $ex->getMessage();
            exit;
        }
    }

}

然后在您的控制器中

    $page = $this->getRequest()->getParam("page");
    $limit = 10;
    $username = 'aculinario';
    $paginator = new Zend_Paginator(new Lib_Paginator_Adapter_YoutubeUser($username));
    $paginator->setItemCountPerPage($limit);
    $paginator->setPageRange(10);
    $paginator->setCurrentPageNumber($page);
    $this->view->youtubeFeed = $paginator;