Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use an array in a function? 1

Status
Not open for further replies.

jon24422531

Technical User
Jan 27, 2004
295
GB
I am a PHP newbie so please forgive what may be a simple request. I have used PHP and JSON to retrieve a view count for a single YouTube video, but I want to list the view count for many more (around 65) probably using an array.

This (predictably) does not work, where am I going wrong?

PHP:
<?php
$vidarray = array("IRQ9bdiq4A4", "ew8TxXHmqzY", "CWQOhEw1vZk", "r4aR49ECD4s");
$arrlength = count($vidarray);
function get_youtube_views($video_ID) {
    $JSON = file_get_contents("[URL unfurl="true"]https://gdata.youtube.com/feeds/api/videos?q={$video_ID}&alt=json");[/URL]
    $JSON_Data = json_decode($JSON);
	$descr = $JSON_Data->{'feed'}->{'entry'}[0]->{'media$group'}->{'media$description'}->{'$t'};
	$views = $JSON_Data->{'feed'}->{'entry'}[0]->{'yt$statistics'}->{'viewCount'};
	$titles = $JSON_Data->{'feed'}->{'entry'}[0]->{'title'}->{'$t'};
	echo "Number of views:" .$views. "<br>";
	echo "Description:" .$descr. "<br>";
	echo "Title:" .$titles. "<br>";
    }
	for($x=0;$x<$arrlength;$x++) {
get_youtube_views($vidarray);
}
?>

On a completely unrelated note, I am doing this as a single web page for a visually impaired friend who has recorded these videos, is there any way that the resulting text can be read out when the page loads?
Many thanks

Jonathan


Jonathan
 
You are passing the entire array each time the for loop runs, you aren't specifying an item within the array to get the ID from which is what the youtube feed is expecting.

Try this:

Code:
for($x=0;$x<$arrlength;$x++) {
get_youtube_views($vidarray[COLOR=#A40000][b][$x][/b][/color]);
}






----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top