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: