...

View Full Version : Small peice of code giving me big dramas



JudgementDay
01-30-2012, 12:36 PM
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?

jmj001
01-30-2012, 01:05 PM
... error ...

JudgementDay
01-30-2012, 01:20 PM
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!

jmj001
01-30-2012, 01:35 PM
sorry, wasn't trying to be a smart ***.. was trying to make a joke

obv i'm not very funny..

Fou-Lu
01-30-2012, 01:42 PM
$received = 0;
while ($row = mysql_fetch_assoc($result))
{
$received += $row['received'];
}

JudgementDay
01-30-2012, 01:43 PM
Yeah, it would also be cool if there wern't so many smart asses around here. ;)


<?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";
?>

Solved.

leslie.jones
01-30-2012, 02:03 PM
Yeah, it would also be cool if there wern't so many smart asses around here. ;)

Solved.

That, my friend, is so true and it's one of the reasons this forum gets so few visits.
Thing is, many of them ain't that smart - just look at some of the questions they've asked when they first came here ;-)

JudgementDay
01-30-2012, 02:06 PM
hehe yeah.
I just want to note that I didn't say that out loud for nothing. One of the dudes up said something for me to say it, but he has edited out what he said now.
I was actually coming back to edit my response out, but you've replied to it now. :P



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum