Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Banned
    Join Date
    Jan 2012
    Posts
    93
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Small peice of code giving me big dramas

    I was wondering if someone can tell me what I have done wrong with this code:

    PHP 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?
    Last edited by JudgementDay; 01-30-2012 at 11:43 AM.

  • #2
    Regular Coder
    Join Date
    Jan 2012
    Posts
    271
    Thanks
    2
    Thanked 65 Times in 65 Posts
    ... error ...
    Last edited by jmj001; 01-30-2012 at 12:46 PM. Reason: error...

  • #3
    Banned
    Join Date
    Jan 2012
    Posts
    93
    Thanks
    8
    Thanked 0 Times in 0 Posts
    I did this:

    Code:
    <?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!

  • #4
    Regular Coder
    Join Date
    Jan 2012
    Posts
    271
    Thanks
    2
    Thanked 65 Times in 65 Posts
    sorry, wasn't trying to be a smart ***.. was trying to make a joke

    obv i'm not very funny..
    Last edited by jmj001; 01-30-2012 at 01:13 PM. Reason: error ...

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    PHP Code:
    $received 0;
    while (
    $row mysql_fetch_assoc($result))
    {
        
    $received += $row['received'];


  • #6
    Banned
    Join Date
    Jan 2012
    Posts
    93
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Yeah, it would also be cool if there wern't so many smart asses around here.

    Code:
    <?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.

  • #7
    New Coder
    Join Date
    Nov 2011
    Posts
    88
    Thanks
    4
    Thanked 26 Times in 26 Posts
    Quote Originally Posted by JudgementDay View Post
    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 ;-)

  • #8
    Banned
    Join Date
    Jan 2012
    Posts
    93
    Thanks
    8
    Thanked 0 Times in 0 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •