while / foreach循环返回同一行中的四个 [英] While/foreach Loops Returning Four of the Same Row

查看:167
本文介绍了while / foreach循环返回同一行中的四个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法弄清楚为什么这个脚本是这样做的,所以我需要一个有更多经验/更好的眼睛的人来告诉我这个问题是什么。我似乎无法在网上找到其他任何东西,但我可能会在我的搜索条件错误的措辞。这个While / Foreach循环的工作方式很好,除了它四次吐出同一行:
$ b $ pre $ $ $ $ $ $ $ $用户名,名称从name_records;
$ sresult = mysql_query($ squery);
while($ srow = mysql_fetch_array($ sresult)){
foreach($ srow as $ scell){
$ username = $ srow ['username'];
$ name = $ srow ['name'];
$ queryi =SELECT SUM(totalseconds)FROM gradpoint WHERE username ='$ username';
$ resulti = mysql_query($ queryi)或die(mysql_error());
while($ rowi = mysql_fetch_array($ resulti)){
$ total = $ rowi ['SUM(totalseconds)'];
$分钟= $ total / 60;
echo $ name。已经为。分钟。分钟今天!好jorb!< p />;





如果前两个条目在name_records表是Bob Ross,br01234和Alfred Hitchcock,ah43210,那么上面的代码就会吐出来:

  Bob Ross今天工作了342分钟!好jorb! 
鲍勃罗斯今天工作了342分钟!好jorb!
鲍勃罗斯今天工作了342分钟!好jorb!
Alfred Hitchcock今天工作了187分钟!好jorb!
Alfred Hitchcock今天工作了187分钟!好jorb!
Alfred Hitchcock今天工作了187分钟!好jorb!
Alfred Hitchcock今天工作了187分钟!好jorb!

为什么是四个?为什么?!我尝试了几种不同的方法来解决这个问题(对不起,我不记得我现在尝试了什么),但是没有用。

解决方案

我认为这个问题来自foreach:

$ $ $ $ $ s $ $ $ $ $ b $ username = $ srow ['username'];
$ name = $ srow ['name'];
$ queryi =SELECT SUM(totalseconds)FROM gradpoint WHERE username ='$ username';
$ resulti = mysql_query($ queryi)或die(mysql_error());
while($ rowi = mysql_fetch_array($ resulti)){
$ total = $ rowi ['SUM(totalseconds)'];
$分钟= $ total / 60;
echo $ name。已经为。分钟。分钟今天!好jorb!< p />;

//


I can't seem to figure out why this script is doing this, so I need someone with more experience/better eyes to tell me what the issue is. I can't seem to find anything elsewhere online, but I may be wording my search terms wrong. This set of While/Foreach loops works just fine except that it spits out the same row four times:

   $squery = "SELECT username, name FROM name_records";
   $sresult = mysql_query($squery);
   while($srow = mysql_fetch_array($sresult)) {
        foreach ($srow as $scell) {
        $username = $srow['username'];
             $name = $srow['name'];
             $queryi = "SELECT SUM(totalseconds) FROM gradpoint WHERE username = '$username'";
             $resulti = mysql_query($queryi) or die(mysql_error());
             while($rowi = mysql_fetch_array($resulti)){
                  $total = $rowi['SUM(totalseconds)'];
                  $minutes = $total / 60;
                  echo $name ." has worked for " .$minutes ." minutes today! Good jorb!<p/>";
             }
         }
 }

If the first two entries in the name_records table were "Bob Ross, br01234" and "Alfred Hitchcock, ah43210", then the above code would spit out this:

 Bob Ross has worked for 342 minutes today! Good jorb!
 Bob Ross has worked for 342 minutes today! Good jorb!
 Bob Ross has worked for 342 minutes today! Good jorb!
 Bob Ross has worked for 342 minutes today! Good jorb!
 Alfred Hitchcock has worked for 187 minutes today! Good jorb!
 Alfred Hitchcock has worked for 187 minutes today! Good jorb!
 Alfred Hitchcock has worked for 187 minutes today! Good jorb!
 Alfred Hitchcock has worked for 187 minutes today! Good jorb!

Why four? Why?! I tried a few different ways to fix it (sorry, I don't remember what I tried now), but to no avail.

解决方案

I think the issue came from the foreach:

    //foreach ($srow as $scell) {
    $username = $srow['username'];
         $name = $srow['name'];
         $queryi = "SELECT SUM(totalseconds) FROM gradpoint WHERE username = '$username'";
         $resulti = mysql_query($queryi) or die(mysql_error());
         while($rowi = mysql_fetch_array($resulti)){
              $total = $rowi['SUM(totalseconds)'];
              $minutes = $total / 60;
              echo $name ." has worked for " .$minutes ." minutes today! Good jorb!<p/>";
         }
     //}

这篇关于while / foreach循环返回同一行中的四个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆