php - While/foreach Loops Returning Four of the Same Row -
i can't seem figure out why script doing this, need more experience/better eyes tell me issue is. can't seem find elsewhere online, may wording search terms wrong. set of while/foreach loops works fine except spits out same row 4 times:
$squery = "select username, name name_records"; $sresult = mysql_query($squery); while($srow = mysql_fetch_array($sresult)) { foreach ($srow $scell) { $username = $srow['username']; $name = $srow['name']; $queryi = "select sum(totalseconds) gradpoint 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 " .$minutes ." minutes today! jorb!<p/>"; } } }
if first 2 entries in name_records table "bob ross, br01234" , "alfred hitchcock, ah43210", above code spit out this:
bob ross has worked 342 minutes today! jorb! bob ross has worked 342 minutes today! jorb! bob ross has worked 342 minutes today! jorb! bob ross has worked 342 minutes today! jorb! alfred hitchcock has worked 187 minutes today! jorb! alfred hitchcock has worked 187 minutes today! jorb! alfred hitchcock has worked 187 minutes today! jorb! alfred hitchcock has worked 187 minutes today! jorb!
why four? why?! tried few different ways prepare (sorry, don't remember tried now), no avail.
i think issue came foreach:
//foreach ($srow $scell) { $username = $srow['username']; $name = $srow['name']; $queryi = "select sum(totalseconds) gradpoint 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 " .$minutes ." minutes today! jorb!<p/>"; } //}
php loops foreach while-loop repeating
No comments:
Post a Comment