这里的概念是数据库将YouTube channel 用户名存储在名为YOUTUBE的单元中。
我需要代码来查找USERDB,查找YOUTUBE单元并检索存储在列表中的所有用户名(任何空白单元都不会显示)。
从这里,我需要代码将YouTube单元中的用户youtube用户名放入FEEDURL中
我需要将此循环,以便它将每个用户从结果中分离出来。我遇到的问题是FEEDURL在URL中显示所有用户名,而不是一个。
例如。
http://gdata.youtube.com/feeds/api/users/PewDiePie,MissFushi/uploads?max-results=13
但我需要像
例如。
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个视频在一起。有什么想法吗?
最佳答案
尝试使用此代码。我对数组的操作方式进行了一些编辑。确保开始使用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
关于php - PHP:Loop不断从SQL中检索所有用户,而不是一次执行一个,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28752338/