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
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
clobberx: Indeed. $$ is called using variable variables. You could read about them at http://uk2.php.net/manual/en/language.variables.variable.php
echo 'Unit Cost: ' . $Unit_Cost . '<br /><br />'; } $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