Jump to content

Pagination With External Data


Guest FirefoxRocks
 Share

Recommended Posts

Guest FirefoxRocks

It isn't ideal for a search results page to return 950 entries. Currently they are paginated through JavaScript/jQuery but I was hoping that I could use PHP to do this instead (and maybe move onto AJAX later on).Right now the code (that displays non-paginated data) is this:

$yt = new Zend_Gdata_YouTube();					$yt->setMajorProtocolVersion(2);					$q = $yt->newVideoQuery();					$q->setVideoQuery($a);					$videoFeed = $yt->getVideoFeed($q->getQueryUrl(2));										function printEntireFeed($videoFeed) {						foreach($videoFeed as $videoEntry) {							echo "<li>\n" .$videoEntry->getVideoTitle() . " <span class='hl'>" . $videoEntry->getVideoId() . "</span>\n<p>" . $videoEntry->getVideoDuration() . " seconds - " . $videoEntry->getVideoViewCount()." views";						}						// See whether we have another set of results						try {							$videoFeed = $videoFeed->getNextFeed();						}						catch (Zend_Gdata_App_Exception $e) {							return;						}						if ($videoFeed) {							printEntireFeed($videoFeed);						}					}					printEntireFeed($videoFeed);

As you can see, the results are returned through getNextFeed if there are more, and there is no way to specify a start/end range in the function as a variable or hardcoded.What is the proper way to do this? Write everything into an array and display only those array elements through a GET variable? The problem with that is the unnecessary bandwidth of retrieving all the results plus the memory (and speed) of the PHP code execution.More info: http://code.google.com/apis/youtube/2.0/de...html#Pagination

Link to comment
Share on other sites

You'll need the "next" link to include the number to start at, so if you display the first 10 then the next link should include 10 as the position to start at. You can just add a global counter in your code to keep track of how many you've printed.

Link to comment
Share on other sites

Guest FirefoxRocks

Yes I understand that, it would look like "search.php?q=christmas&start=25" but how do I retrieve the next 23 videos from YouTube using the function in the API?

Link to comment
Share on other sites

a global variable is one that is declared within a script but outside of any functions. That way multiple functions can read and right to the variable. It falls under the topic of scoping.

Edited by thescientist
Link to comment
Share on other sites

Guest FirefoxRocks

I still don't understand how I can use that (or a counter) to specify which number of videos to start retrieving though. Can you provide some code example please?

Link to comment
Share on other sites

Guest FirefoxRocks

I don't think you understand what I am not understanding here. I know what a global variable is. I just don't know how you can use it to reduce the amount of data you are getting from the YouTube server.The problem here is that either the server is very slow or the process of retrieving a few hundred videos are slow. With or without an array, I don't think it's the echoing part that is slow.

Link to comment
Share on other sites

Guest FirefoxRocks
If you have a counter then you can figure out when to stop asking for new videos, which means you need to modify the printEntireFeed function to update and check the global counter.
Yes but how do you specify when to start asking for videos?
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...