简体   繁体   English

在嵌套的foreach循环中使用嵌套查询

[英]using nested queries in nested foreach loops

Okay, after pounding my head against the wall for about 2 hours, I need help. 好吧,将我的头撞在墙上约2个小时后,我需要帮助。 I have a query that pulls "comments" based on a users "followers". 我有一个查询,它基于用户“关注者”拉出“评论”。 The followers have a unique id, and lets say just for ease of use, their id's are 4 and 5. The query returns all followers, so there could be way more than just two rows. 追随者有一个唯一的ID,为了便于使用,可以说他们的ID为4和5。查询返回所有追随者,因此可能不仅仅是两行。 That query creates a foreach that I want to say for each "follower" execute this query. 该查询创建一个foreach,我想针对每个“跟随者”执行此查询。 Then, that query needs to create a fetch_assoc() so that the data can be recalled later on down the page. 然后,该查询需要创建一个fetch_assoc(),以便稍后可以在页面下方重新调用数据。 The way I have the code right now, it only shows postings from follower 4, and not 5. I have tried using counters to append variable names so that they wont get overwritten each time, but it doesnt help. 我现在拥有代码的方式,它仅显示来自跟随者4的帖子,而不显示5。我试图使用计数器附加变量名,以使它们每次不会被覆盖,但这无济于事。 Thanks a lot! 非常感谢!

$sqlb ="SELECT myfriend_ID FROM friends WHERE my_id = '$myuid'";
$resultb = mysql_query($sqlb,$db);
$numa = mysql_num_rows($resultb);
if($numa)
{
    while(($follower = mysql_fetch_assoc($resultb))) {
        $followers[] = $follower;
    }
}
$i=0;
foreach($followers as $follower)
{
    $i++;
    $fnub = $follower['myfollower_ID'];//this will print 4 and 5
    $fnum = $fnub;

    $sql = "SELECT * FROM usr, feed, course WHERE usr.usr_id = '$fnum' AND 
             feed.usr_id = '$fnum' AND course.usr_id = '$fnum' ORDER BY Feed_ID desc";
    $result = mysql_query($sql,$db);
    $num.$fnum = mysql_num_rows($result);
    if($num.$fnum)
    {
        while(($feed = mysql_fetch_assoc($result))) {
            $feeds[] = $feed;
        }
        foreach($feeds as $feed)
        {
            echo $feed['comment'];
        }
    }
}
$sqlb ="SELECT myfriend_ID FROM friends WHERE my_id = '$myuid'";
$resultb = mysql_query($sqlb,$db);
$follower = array();

if(mysql_num_rows($resultb))
{
   while(($follower = mysql_fetch_assoc($resultb))) {
      $fnum = $follower['myfriend_ID'];//what is the column name
      $sql = "SELECT * FROM usr, feed, course WHERE usr.usr_id = '$fnum' AND feed.usr_id = '$fnum' AND course.usr_id = '$fnum' ORDER BY Feed_ID desc";
      $result = mysql_query($sql,$db);
      if( mysql_num_rows($result))
      {
         while(($feed = mysql_fetch_assoc($result))) {
           echo $feed['comment'];
       }
    }
}

And you must combine these two queries into single. 并且您必须将这两个查询合并为一个查询。 But I am not too good in SQL. 但是我对SQL不太好。 Probably some one other can help. 也许其他人可以帮忙。

You assign $i, then you increment $i within the foreach, but you never use $i for anything else, I'm not sure what that counter is supposed to do. 您分配$ i,然后在foreach中递增$ i,但是您从不将$ i用于其他任何事情,我不确定该计数器应该做什么。 I'm unsure of why you assign a second variable to the value of $fnub, why not just use $fnub each time? 我不确定为什么要为$ fnub的值分配第二个变量,为什么不每次都使用$ fnub? Later you use (what we can only assume is) an empty $num variable and prepend $fnum with it - I'm not sure why - or even why you have to assign anything to the num rows, just use the function within the if as that looks to be the only instance you are using $num.$fnum. 稍后您使用(我们只能假设是)一个空的$ num变量并在其前面加上$ fnum-我不确定为什么-甚至为什么您必须将任何内容分配给num行,只需在if中使用该函数因为这似乎是您使用$ num。$ fnum的唯一实例。

You can take your last foreach out completely and move the content of that loop into the while before it as they both are iterating over the same data, just assign the values, then echo $feed['comment'] out right there, then it will move the pointer and have the next value ready for you by itself. 您可以完全取出最后一个foreach并将该循环的内容移到它之前的那一刻,因为它们都在同一数据上进行迭代,只需分配值,然后在此处回显$ feed ['comment'],然后将移动指针,并为您准备下一个值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM