while循环只检索一个结果

发布于 2024-11-18 11:41:03 字数 1441 浏览 1 评论 0原文

更新:似乎仍然无法弄清楚。如果有人可以帮忙,我将不胜感激^^。

我遇到了问题,我不确定我的代码在哪里崩溃了。我有一个“关注”功能,您可以在其中关注不同的注册用户。您关注的用户 ID 存储在数组 (follower_array) 中。它从数组中检索每个成员,但对于后面的每个成员,它只显示每个成员的 1 个最新内容,而不是显示所有内容。

$broadcastList= "";

if ( $follower_array != "" ) {

  $followArray = explode(",", $follower_array);
  $followCount = count($followArray);   
  $i = 0;
  $broadcastList .= "<table>";

  foreach( $followArray as $key => $value ) {

    $i++;
    $sqlName = mysql_query("
        SELECT username, fullname 
          FROM members 
         WHERE id='$value' 
         LIMIT 1
    ") or die ("error!");

    while ( $row = mysql_fetch_array($sqlName) ) {
      $friendUserName = substr($row["username"],0,12);
    }

    $sqlBroadcast = mysql_query("
        SELECT mem_id, bc, bc_date, device 
          FROM broadcast 
         WHERE mem_id='$value' 
      ORDER BY bc_date ASC 
         LIMIT 50
    ") or die ("error!");

    while ( $bc_row = mysql_fetch_array($sqlBroadcast) ) {
      $mem_id  = $bc_row['mem_id'];
      $bc      = $bc_row['bc'];
      $dev     = $bc_row['device'];
      $bc_date = $bc_row['bc_date'];
      $broadcastList .= "<td><a href='member.php?id=' .$value. ''>
      $friendUserName</a> &bull; $when_bc &bull; Via: $dev <b>$bc</b></td></tr>";
    }
    broadcastList .= "</table>";
  }
}

因此,它会完全检索成员,但不超过他们的单个最新“广播”。任何见解将不胜感激..谢谢!

UPDATE: Still can't seem to figure it out. If anyone can lend a hand, it would be appreciated ^^.

I am having a problem and I'm not sure where my code is breaking down. I have a 'follow' function where you can follow different registered users. Their userID's of who you followed are stored in an array (follower_array). It's retrieving each member from the array, but of each member that's followed instead of displaying all the content, it's only displaying the 1 latest one from each member.

$broadcastList= "";

if ( $follower_array != "" ) {

  $followArray = explode(",", $follower_array);
  $followCount = count($followArray);   
  $i = 0;
  $broadcastList .= "<table>";

  foreach( $followArray as $key => $value ) {

    $i++;
    $sqlName = mysql_query("
        SELECT username, fullname 
          FROM members 
         WHERE id='$value' 
         LIMIT 1
    ") or die ("error!");

    while ( $row = mysql_fetch_array($sqlName) ) {
      $friendUserName = substr($row["username"],0,12);
    }

    $sqlBroadcast = mysql_query("
        SELECT mem_id, bc, bc_date, device 
          FROM broadcast 
         WHERE mem_id='$value' 
      ORDER BY bc_date ASC 
         LIMIT 50
    ") or die ("error!");

    while ( $bc_row = mysql_fetch_array($sqlBroadcast) ) {
      $mem_id  = $bc_row['mem_id'];
      $bc      = $bc_row['bc'];
      $dev     = $bc_row['device'];
      $bc_date = $bc_row['bc_date'];
      $broadcastList .= "<td><a href='member.php?id=' .$value. ''>
      $friendUserName</a> • $when_bc • Via: $dev <b>$bc</b></td></tr>";
    }
    broadcastList .= "</table>";
  }
}

So, it's retrieving the members entirely, but not more than their single latest "broadcast." Any insight would be appreciated.. thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

呢古 2024-11-25 11:41:03

发生的情况如下:

while( $row = some_next_result_function() ){
    $result = $row["thing"];
}

每次都会覆盖 $result,因此您最终只会得到最后的结果。

您需要创建一个列表并将其附加到其中。

Here's what's happening:

while( $row = some_next_result_function() ){
    $result = $row["thing"];
}

Is going to overwrite $result every time, so you'll only end up with the last result.

You need to make a list and append to it instead.

鸵鸟症 2024-11-25 11:41:03

我要在黑暗中尝试一下:

$broadcastList .= "<td><a href='member.php?id=' .$value. ''>
  $friendUserName</a> • $when_bc • Via: $dev <b>$bc</b></td></tr>";

该行几乎错过了起始的“tr”元素。您确定内容不会仅仅因为 html 标记错误而显示吗?

另外:

broadcastList .= "</table>";

你错过了那里的 $ ;)。

我不知道这是否可以解决问题或者对您有帮助;但我确实希望如此:)。或者;您还可以检查 HTML 源代码以查看条目是否确实不存在(请参阅第一条评论)。

I'm gonna take a shot in the dark:

$broadcastList .= "<td><a href='member.php?id=' .$value. ''>
  $friendUserName</a> • $when_bc • Via: $dev <b>$bc</b></td></tr>";

That line pretty much misses a starting "tr" element. Are you sure the content isn't shown simply because of the faulty html markup?

Also:

broadcastList .= "</table>";

You're missing the $ there ;).

I don't know if this fixes it or even helps you; but I sure hope so :). Alternatively; you can also check the HTML source to see if the entries really aren't there (see first remark).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文