1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Need PHP Help :: Not adding Properly

Discussion in 'PHP' started by Waizujin, Jun 8, 2008.

Thread Status:
Not open for further replies.
  1. #1
    Greetings, I am coding a game and I have this code below that will take the units, display their information, then add their offense amounts into 1 variable.
    Here is the PHP Code:
    
    			$grab_units = mysql_query("SELECT Unit_Name, Unit_Offense, Unit_Defense, Unit_Cost, Kingdom_Gold, Spy, Guard, Macemen, Archer, Swordsmen, Spearmen FROM " .$_SESSION['server']. "_Units, " .$_SESSION['server']. "_Kingdoms WHERE Kingdom_Owner = '$_SESSION[user]'") or die(mysql_error());
    			while(list($Unit_Name, $Unit_Offense, $Unit_Defense, $Unit_Cost, $Kingdom_Gold, $Spy, $Guard, $Swordsmen, $Spearmen, $Macemen, $Archer) = mysql_fetch_array($grab_units))
    			{
    				$Unit_Name = $Unit_Name;
    				echo 'Unit Name: ' . $Unit_Name . '<br />';
    				$Unit_Count = $$Unit_Name;
    				echo 'Unit Amount: ' . $Unit_Count . '<br />';
    				$Unit_Offense = $Unit_Offense;
    				echo 'Unit Offense: ' . $Unit_Offense . '<br />';
    				$Unit_Defense = $Unit_Defense;
    				echo 'Unit Defense: ' . $Unit_Defense . '<br />';
    				$Unit_Cost = $Unit_Cost;
    				echo 'Unit Cost: ' . $Unit_Cost . '<br /><br />';
    			}
    				$Offense = $Unit_Count * $Unit_Offense;
    				echo 'Kingdom Offense: ' . $Offense . '<br />';
    
    Code (markup):
    Now, here is what it displays.

    Any ideas what I am doing wrong?
    Any help is GREATLY appreciated,
    -Ryu
     
    Waizujin, Jun 8, 2008 IP
  2. clobberx

    clobberx Active Member

    Messages:
    73
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    61
    #2
    can u please explain me, why did u used $$Unit_Name; at line 6
     
    clobberx, Jun 8, 2008 IP
  3. Waizujin

    Waizujin Peon

    Messages:
    281
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    If I used $Unit_Name I would get the result "Spy".
    If I used $$Unit_Name I would get the result "0".
    Using $$ makes it possible to have a variable within a variable.

    -Ryu
     
    Waizujin, Jun 8, 2008 IP
  4. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #4
    Danltn, Jun 8, 2008 IP
  5. kirpikinzi

    kirpikinzi Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    echo 'Unit Cost: ' . $Unit_Cost . '<br /><br />';
    }:mad:
    $Offense = $Unit_Count * $Unit_Offense;
    echo 'Kingdom Offense: ' . $Offense . '<br />';


    i think this problem is just because of the paranthesis in the wrong place. you can see the mad smily. just make it like this

    echo 'Unit Cost: ' . $Unit_Cost . '<br /><br />';
    $Offense = $Unit_Count * $Unit_Offense;
    }
    echo 'Kingdom Offense: ' . $Offense . '<br />';

    if code is still wrong just pm me. i would be pleased to work.

    and may be i can help you with coding if you want
     
    kirpikinzi, Jun 8, 2008 IP
  6. kirpikinzi

    kirpikinzi Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    sorry,

    may be this will work

    $Offense += $Unit_Count * $Unit_Offense;
     
    kirpikinzi, Jun 8, 2008 IP
  7. clobberx

    clobberx Active Member

    Messages:
    73
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    61
    #7
    Anything multiplies with 0 becomes 0
    move the sum statement line into the loop and try
     
    clobberx, Jun 8, 2008 IP
  8. Waizujin

    Waizujin Peon

    Messages:
    281
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    It's already been fixed.

    -Ryu
     
    Waizujin, Jun 9, 2008 IP
Thread Status:
Not open for further replies.