JudgementDay Posted January 30, 2012 Share Posted January 30, 2012 I was wondering if someone can tell me what I have done wrong with this code: <?php foreach ($row['received'] as $data) { $data += $received; } echo $received; ?> It should be adding the number 2 three times, so it should be echoing 6 but its not. Problem 1 I have is that nothing is being added to the variable $received, and problem 2 is that I suspect $received will not become 6, but rather 222. Am I correct? Quote Link to comment https://forums.phpfreaks.com/topic/256036-small-peice-of-php-code-with-big-problems/ Share on other sites More sharing options...
Drongo_III Posted January 30, 2012 Share Posted January 30, 2012 i dont quite see how the value of $received is being assigned? You are adding it's value to$data and then echoing out $received - but why would that display your new value? try this to test it foreach($row['received'] as &$data) { $data += $received; echo $data; echo "<br/>"; } print_r($row['received']); Also you need to pass data by reference or it wont change outside of the loop. Quote Link to comment https://forums.phpfreaks.com/topic/256036-small-peice-of-php-code-with-big-problems/#findComment-1312544 Share on other sites More sharing options...
lonewolf217 Posted January 30, 2012 Share Posted January 30, 2012 You are using the same object for the loop iteration as you are the counter so your $data object is reset each time during the loop. Use a different object for your counter and you should be fine, and make sure you initialize it to zero. Quote Link to comment https://forums.phpfreaks.com/topic/256036-small-peice-of-php-code-with-big-problems/#findComment-1312547 Share on other sites More sharing options...
JudgementDay Posted January 30, 2012 Author Share Posted January 30, 2012 I did this: <?php $received = 0; while ($row = mysql_fetch_assoc($result)) { foreach ($row as $data) { $received = $data['received'] + $received; } } echo $received; ?> I now have two rows of "received", which are '15' and '1'. So, with the above code I was expecting to get 16... but guess what I got?! 7!... I don't know why this happened! Quote Link to comment https://forums.phpfreaks.com/topic/256036-small-peice-of-php-code-with-big-problems/#findComment-1312550 Share on other sites More sharing options...
PaulRyan Posted January 30, 2012 Share Posted January 30, 2012 This should work, untested though. <?php $received = 0; while ($row = mysql_fetch_assoc($result)) { $received += $row['received']; } echo $received; ?> Try that out, and post back what you got. Regards, PaulRyan. Quote Link to comment https://forums.phpfreaks.com/topic/256036-small-peice-of-php-code-with-big-problems/#findComment-1312554 Share on other sites More sharing options...
JudgementDay Posted January 30, 2012 Author Share Posted January 30, 2012 It didn't work, but this did: <?php $received = 0; $query = "SELECT * FROM finances"; $result = mysql_query($query); while ($row = mysql_fetch_assoc($result)) { $received = $row['received'] + $received; } echo "Received: $".$received." AUD"; ?> Much thanks to you, I solved it. Quote Link to comment https://forums.phpfreaks.com/topic/256036-small-peice-of-php-code-with-big-problems/#findComment-1312557 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.