PHP:循环保持从SQL检索所有用户而不是一次一个

时间:2015-02-26 20:40:47

标签: php youtube youtube-api

这里的概念是数据库会将youtube频道用户名存储在名为YOUTUBE的单元格中。

我需要代码来查找USERDB,查找YOUTUBE单元格并检索存储在列表中的所有用户名(任何空白单元格都不会显示)。

从这里我需要代码将用户youtube用户名从YOUTUBE单元格放入FEEDURL

我需要循环播放,以便从结果中为每个用户执行操作。我得到的问题是FEEDURL显示URL中的所有用户用户名而不是一个。

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

但我需要它像

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

这是我的代码

$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);

最后我只想要显示13个视频。每个用户不是13个视频,只有13个来自所有用户的视频。有什么想法?

3 个答案:

答案 0 :(得分:0)

您无需执行implode来获取用户名。 只需使用for each结果直接array_filter($v)

$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);
}

答案 1 :(得分:0)

尝试使用此代码。我对数组的操作方式进行了一些编辑。请务必使用mysqli_*扩展名而不是mysql_*,因为前者更安全,更不容易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 

答案 2 :(得分: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);

对于任何阅读并想知道最终是如何修复工作的人?好吧,这是