Zend Framework 101: Zend_Gdata_YouTubeSplitting Results Into Multiple PagesIn our code so far we accessed all videos uploaded by a single user. If the user has a large number of videos this could result in a lot of data. Typically though, you would want to restrict the number of videos returned for each call. To demonstrate this, we'll split up returned results into pages of 2 videos (you might want a higher limit).In order to split results into multiple pages, there are several extra steps we must perform:
p
which specifies the current page of results to view. Based on the limit
we specify and the page number specified, we must determine the
starting point.
Note: With many PHP functions the starting offset begins with
Listing 6 shows how to calculate the offset based on the page number and limit.
0. When using Zend_Gdata the starting point begins at 1.
Listing 6 Determining offset based on page and limit (listing-6.php)
if (isset($_GET['p'])) { $page = max(1, (int) $_GET['p']); } else { $page = 1; } // maximum number of results $limit = 2; // determine offset based on page and limit. // add 1 at the end to account for offset starting at 1 $offset = ($page - 1) * $limit + 1; getUserUploads(). Previously we passed the username as the first argument, but we must now build a more advanced query using Zend_Gdata_Query. We then replace the username with null and pass the query as the second argument.
First we must load the Zend_Gdata_Query class, then instantiate it with the URL from which the fetch the results from.
Note: Unfortunately we must manually build the URI to request data from.
Next we set the offset and limit using the Zend_Gdata_YouTube doens't include a wrapper method to simplify this. I determined the correct URL by looking inside the Zend/Gdata/YouTube.php file.
setStartIndex() and setMaxResults() methods. Listing 7 continues on from the previous listing and shows how to achieve this.
Listing 7 Building a query for retrieving a video feed (listing-7.php)
// we assume limit and offset are already set require_once('Zend/Gdata/YouTube.php'); require_once('Zend/Gdata/Query.php'); $username = 'ReciteCMS'; $url = sprintf( '%s/%s/%s', Zend_Gdata_YouTube::USER_URI, $username, Zend_Gdata_YouTube::UPLOADS_URI_SUFFIX ); $query = new Zend_Gdata_Query($url); $query->setMaxResults($limit) ->setStartIndex($offset); getUserUploads(). The result is identical to previously and you can loop over the returned feed to access each result.
The final part of splitting results up into pages is to determine the number of pages. To determine the total number of results, call $feed->getTotalResults()->getText();. You then divide this value by the limit to determine the number of pages.
Listing 8 demonstrates how to build and output the pager. Although the output here is a little crude, it demonstrates how to list the pages, and also highlights the active page number.
Listing 8 Splitting video results up into multiple pages (listing-8.php)
if (isset($_GET['p'])) { $page = max(1, (int) $_GET['p']); } else { $page = 1; } // maximum number of results $limit = 2; // determine offset based on page and limit. // add 1 at the end to account for offset starting at 1 $offset = ($page - 1) * $limit + 1; require_once('Zend/Gdata/YouTube.php'); require_once('Zend/Gdata/Query.php'); $username = 'ReciteCMS'; $url = sprintf( '%s/%s/%s', Zend_Gdata_YouTube::USER_URI, $username, Zend_Gdata_YouTube::UPLOADS_URI_SUFFIX ); $query = new Zend_Gdata_Query($url); $query->setMaxResults($limit) ->setStartIndex($offset); $youtube = new Zend_Gdata_YouTube(); try { $feed = $youtube->getUserUploads(null, $query); } catch (Exception $ex) { echo $ex->getMessage(); exit; } // determine the total and number of pages $total = $feed->getTotalResults()->getText(); $numPages = ceil($total / $limit); foreach ($feed as $video) { // output video as usual echo $video->getVideoTitle() . "<br />\n"; } <hr /> Page: <ul> for ($i = 1; $i <= $numPages; $i++) { <li> <a href="?p= echo $i "> if ($i == $page) { <strong> echo $i </strong> } else { echo $i </li> } </a> </li> } </ul> |
chitika Ad
Saturday, 1 March 2014
Zend Framework 101: Zend_Gdata_YouTube
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment