duansanzi5265 2015-02-26 20:40
浏览 41
已采纳

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

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?

  • 写回答

3条回答 默认 最新

  • dpda53918 2015-02-27 12:36
    关注

    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 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型