PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Zend Framework 101: Zend_Gdata_YouTube

Splitting Results Into Multiple Pages

In 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:

  1. Specify the maximum number of results to return in the query
  2. Specify the starting point for the query
  3. Determine the total number of results so we know how many pages there will be

In the examples in this section, we'll pass a URL parameter called 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 0. When using Zend_Gdata the starting point begins at 1.

Listing 6 shows how to calculate the offset based on the page number and limit.

Listing 6 Determining offset based on page and limit (listing-6.php)
<?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;
?>

Now we must update the call to the 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. 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.

Next we set the offset and limit using the 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)
<?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);
?>

Now you can combine these two listings and call the 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)
<?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>
    <?php for ($i = 1; $i <= $numPages; $i++) { ?>
        <li>
            <a href="?p=<?php echo $i ?>">
                <?php if ($i == $page) { ?>
                    <strong><?php echo $i ?></strong>
                <?php } else { ?>
                    <?php echo $i ?></li>
                <?php } ?>
            </a>
        </li>
    <?php } ?>
</ul>

In This Article