0

The concept idea here is that The database would store the youtube channel users name in a cell called YOUTUBE.

I needed the code to look up the USERDB, look up the YOUTUBE cell and retrieve all the usernames that were stored in a list (any blank cells would not show).

From here I needed the code to place users youtube username from the YOUTUBE cell into the FEEDURL

I needed this looped so it would do each individual user from the result. The problem I am getting is that the FEEDURL is showing all the users usernames in the URL instead of one.

EG. http://gdata.youtube.com/feeds/api/users/PewDiePie,MissFushi/uploads?max-results=13

but I need it to be like

EG. http://gdata.youtube.com/feeds/api/users/MissFushi/uploads?max-results=13

here is my code

$communityvideos = mysql_query("SELECT youtube FROM userdb WHERE rights='user' && youtube IS NOT NULL");
while($youtube = mysql_fetch_array($communityvideos)) {

$v[] = $youtube["youtube"];
}

$youtube2 = implode(',', array_filter($v));

$usernames = array($youtube2);

error_reporting(E_ALL);
foreach ($usernames as $user) {
$feedURL = 'http://gdata.youtube.com/feeds/api/users/' . $user .'/uploads?max-results=13';
$sxml = simplexml_load_file($feedURL);
}
$i=0;
foreach ($sxml->entry as $entry) {
      $media = $entry->children('media', true);
      $watch = (string)$media->group->player->attributes()->url;
      $thumbnail = (string)$media->group->thumbnail[0]->attributes()->url;

parse_str( parse_url( $watch, PHP_URL_QUERY ), $my_array_of_vars);

Finally I only want 13 videos showing in total. Not 13 videos from each user, just 13 from all the users joined together. Any ideas on that?

Lonely Ranger
  • 89
  • 1
  • 10

3 Answers3

0

You dont need to do a implode to take the users name. Just do directly for each with array_filter($v) results:

$users = array_filter($v);
foreach ($users as $user) {
$feedURL = 'http://gdata.youtube.com/feeds/api/users/' . $user .'/uploads?max-results=13';
$sxml = simplexml_load_file($feedURL);
}
Juan de Parras
  • 768
  • 4
  • 18
  • nope started working with the answer you had before which was $users = array_filter($v); but only showed the videos for the last person in the array list. – Lonely Ranger Feb 27 '15 at 13:01
  • Please, remove array_filter. Its not necesary. If you want work with it anyway, please use `$users[] = array_filter($v);` users need to be an array. – Juan de Parras Feb 27 '15 at 13:05
  • taking away the filter caused errors as it kept my blank entries still intact, adding the filter and doing what you said had corrected the error showing only users who had entered there usernames in. Though it worked as $users = array_filter($v) and not $users[] = array_filter($v) which was causing errors :/ – Lonely Ranger Feb 27 '15 at 13:28
0

Try using this code. I've made some edits to the way the array was being manipulated. Make sure to start using the mysqli_* extension instead of mysql_* since the former is much more secure and less prone to SQL injection.

$communityvideos = mysql_query("SELECT youtube FROM userdb WHERE rights='user' && youtube IS NOT NULL");
$usernames = array();
while($youtube = mysql_fetch_assoc($communityvideos)) {
    $usernames[] = $youtube['youtube'];
}

//Redacted this does not do what you want it to do. This glues all usernames together and wrecks the $usernames array.
//$youtube2 = implode(',', array_filter($v));
//$usernames = array($youtube2);

error_reporting(E_ALL);

foreach ($usernames as $user){
$feedURL = 'http://gdata.youtube.com/feeds/api/users/' . $user .'/uploads?max-results=13';
$sxml = simplexml_load_file($feedURL);


// Place this inside the $usernames loop
$i=0;
foreach ($sxml->entry as $entry) {
      $media = $entry->children('media', true);
      $watch = (string)$media->group->player->attributes()->url;
      $thumbnail = (string)$media->group->thumbnail[0]->attributes()->url;

        parse_str( parse_url( $watch, PHP_URL_QUERY ), $my_array_of_vars);
        // And whatever came after this it doesn't show in the question.
    }
}

// You will have to figure the part below as well. Since you the last iteration of your foreach loop above will end with the last result in $sxml.
// The loop below will only loop the xml for the last result 
Community
  • 1
  • 1
Rimble
  • 873
  • 8
  • 22
  • that didn't work. it worked for the last user in the list like you predicted when I deleted all the stuff you put in //. Kept $v[] =..... and added in $users = array_filter($v); and the foreach became users as user. that worked where it would show the videos for the very last user in the list only not the others which would fall into the second part you were on about yea? – Lonely Ranger Feb 27 '15 at 13:04
  • Your problem is that you keep rewriting `$sxml` I'll make an edit try it out. – Rimble Feb 27 '15 at 13:09
  • I placed the $sxml loop inside the usernames loop. Make sure you add that up with the code that came after that since it's incomplete in the data you provided for your question. – Rimble Feb 27 '15 at 13:12
  • yes you are correct I have amended that now, the loop is working now I just need to figure how to change it so it only shows 13 results in total as right now with 2 user names operating it is showing 26 results – Lonely Ranger Feb 27 '15 at 13:28
  • oh you said you couldn't see what was below, below that parse was the html part for borders links etc. and then once that echo was complete the ending was `$i++; if($i==5) { $i=0; } }` – Lonely Ranger Feb 27 '15 at 13:34
  • Make a counter variable, then whenever you show a video or embed one. Add to that counter. When it hits 13. Stop the loop. I'm not sure how the exact code for the youtube API is but if you elaborate a bit more I could help with the counter part. Please fill in the rest of your code so I can make a next edit on it. – Rimble Feb 27 '15 at 13:36
  • ive seen this counter business you may be describing before, how-to-get-latest-videos-from-multiple-youtube-channels-in-single-api-call is this what you mean? – Lonely Ranger Feb 27 '15 at 13:48
  • What is your goal? Do you want one video per user. Or an arbitrary amount of videos per user and a maximum of 13? – Rimble Feb 27 '15 at 13:52
  • goal is maximum 13 recent videos. I could have up to 100 users but all I want is the 13 most recent videos not per user just the 13 most recent videos (eg. 100 users, each account has 2 videos, I retrieve 200 videos in total, from those 200 videos I want just the 13 most recent videos). So yes Maximum Of 13 – Lonely Ranger Feb 27 '15 at 13:55
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/71879/discussion-between-tom-kriek-and-lonely-ranger). – Rimble Feb 27 '15 at 13:57
  • ok sure let me just put this as a green tick answer as it did the bulk of my main first question – Lonely Ranger Feb 27 '15 at 14:03
0
ommunityvideos = mysql_query("SELECT * FROM userdb WHERE rights='user' && youtube IS NOT NULL");
while($youtube = mysql_fetch_assoc($communityvideos)) {
    $v[] = $youtube['youtube'];

error_reporting(E_ALL);
$usernames = array_filter($v);
foreach ($usernames as $user){
$feedURL = 'http://gdata.youtube.com/feeds/api/users/' . $user .'/uploads?max-results=13';
$sxml = simplexml_load_file($feedURL);
$i=0;

foreach ($sxml->entry as $entry) {
      $media = $entry->children('media', true);
      $watch = (string)$media->group->player->attributes()->url;
      $thumbnail = (string)$media->group->thumbnail[0]->attributes()->url;

        parse_str( parse_url( $watch, PHP_URL_QUERY ), $my_array_of_vars);

for anyone reading and wondering how it was fixed to work in the end? Well here it is

Lonely Ranger
  • 89
  • 1
  • 10