dotkpay Posted April 14, 2011 Share Posted April 14, 2011 Hello, I 've been coding in php for some time now but am not sure where the end bracket on a while loop is supposed to be situated. For example: $get = mysql_query("SELECT * FROM members WHERE username='$user'"); while($row = mysql_fetch_array($get)) { $balance = $row["balance"]; } $new_balance = $balance + $deposit; OR is the above script supposed to be like this (the end bracket has changed position) $get = mysql_query("SELECT * FROM members WHERE username='$user'"); while($row = mysql_fetch_array($get)) { $balance = $row["balance"]; $new_balance = $balance + $deposit; } Quote Link to comment https://forums.phpfreaks.com/topic/233711-while-end-bracket/ Share on other sites More sharing options...
monkeytooth Posted April 14, 2011 Share Posted April 14, 2011 well that depends.. Both ways are fine. However.. if there is balance's to add then within the while loops brackets you'd want to put your statement. If there is only one balance to add to a deposit then outside is best.. All depends on what you want to do in the end with the data.. Quote Link to comment https://forums.phpfreaks.com/topic/233711-while-end-bracket/#findComment-1201527 Share on other sites More sharing options...
Muddy_Funster Posted April 14, 2011 Share Posted April 14, 2011 That depends on if you want the $new_balance variable upadated with the value of $deposit every time a record is returned by the query (put it inside the while{}), or if you just want the $deposit added once to the last value of $balance retrieved from the database (put it outside the while {}). What I expect you are trying to do, and where the confussion is coming from, is that you want to sum the balance and then add the deposit. if that is the case then neither code snipit is going to work. Quote Link to comment https://forums.phpfreaks.com/topic/233711-while-end-bracket/#findComment-1201529 Share on other sites More sharing options...
blacknight Posted April 14, 2011 Share Posted April 14, 2011 the first end bracket would work where you have defined $balance it will allways set to what the last row output from your whale statement is $balance + $row['balance']; will add the total balance from all users same goes for $new_balance = $balance + $deposit; if you have it outside the last } wil it add the totals Quote Link to comment https://forums.phpfreaks.com/topic/233711-while-end-bracket/#findComment-1201536 Share on other sites More sharing options...
Muddy_Funster Posted April 14, 2011 Share Posted April 14, 2011 the first end bracket would work where you have defined $balance it will allways set to what the last row output from your whale statement is $balance + $row['balance']; will add the total balance from all users . . . Yeah, but that's not how you want to sum the values, it's much better to do it in the SQL beforehand, rather than run through a loop. Quote Link to comment https://forums.phpfreaks.com/topic/233711-while-end-bracket/#findComment-1201543 Share on other sites More sharing options...
blacknight Posted April 14, 2011 Share Posted April 14, 2011 that is true $get = mysql_query("SELECT SUM(balance) as total FROM members WHERE username='$user'"); $row = mysql_fetch_row($get); $row['total'] would rep the ballance total for the user Quote Link to comment https://forums.phpfreaks.com/topic/233711-while-end-bracket/#findComment-1201557 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.