Dethman Posted July 12, 2008 Share Posted July 12, 2008 Hey guys I am trying to return two variables here is the code <?php function DP($id) { /* ok let begin this nightmare lol this functions is gonna call the planet names and list them for you */ $sql = "SELECT * FROM `plan` WHERE `userid` = '$id' ORDER BY `Plan_size` DESC"; $result = mysql_query($sql) or die('Query failed.' . mysql_error()); for ($i = 0; $i < mysql_num_rows($result);$i++) { $plan_Name = mysql_result($result,$i,"plan_name"); $plan_Size = mysql_result($result,$i,"plan_size"); $plan_Type = mysql_result($result,$i,"plan_attribute"); $plan_Prod = mysql_result($result,$i,"plan_product"); $plan_Defe = mysql_result($result,$i,"plan_defense"); if ($plan_Type == 1) { $attribute = number_format(ceil(125000 * $plan_Prod * pow(3.5,$plan_Size))); $type = " Defence"; $defamt = number_format(300000 * $plan_Defe); } elseif ($plan_Type == 2) { $attribute = number_format(ceil(135000 * $plan_Prod * pow(3.5,$plan_Size))); $type = " Attack"; $defamt = number_format(300000 * $plan_Defe); } elseif ($plan_Type == 3) { $attribute = number_format(ceil(500000 * $plan_Prod * pow(1.85,$plan_Size))); $type = " Spy"; $defamt = number_format(300000 * $plan_Defe); } elseif ($plan_Type == 4) { $attribute = number_format(ceil(480000 * $plan_Prod * pow(1.3,$plan_Size))); $type = " Income"; $defamt = number_format(300000 * $plan_Defe); } elseif ($plan_Type == 5) { $attribute = number_format(ceil($plan_Prod * pow(1.1,$plan_Size))); $type = " Up"; $defamt = number_format(300000 * $plan_Defe); } switch ($plan_Size) { case 0: $Size = "Tiny"; break; case 1: $Size = "Very Small"; break; case 2: $Size = "Small"; break; case 3: $Size = "Normal"; break; case 4: $Size = "Large"; break; case 5: $Size = "Big"; break; case 6: $Size = "Massive"; break; case 7: $Size = "On verg of a blackhole"; break; } $planet_stats=" <TR> <TD align=left>".$plan_name."</TD> <TD align=left>".number_format($plan_Prod)."</b> (+".$attribute.$type.")</TD> <TD align=left>".number_format($plan_Defe)."(".$defamt.")</TD> <TD align=lrft>".$Size."</TD> </TR> "; $planet_ammount=" <tr> <td colspan=4><center>".mysql_num_rows($result)." villages total</center></td> </tr> "; } return $planet_stats; } ?> I need to know how to return the variable "$planet_ammount" aswell as $planet_stats and am not sure how to anyone able to help? Thank you for your Replie! Link to comment https://forums.phpfreaks.com/topic/114408-how-do-i-return-2-variables/ Share on other sites More sharing options...
kenrbnsn Posted July 12, 2008 Share Posted July 12, 2008 You can return an array containing the two values and use the list() statement to get the individual values: <?php function test($x) { // // some code here // return(array($ret_value1,$ret_value2)); } // // list ($val1,$val2) = funciton($x); echo $val1 . ' ... ' . $val2; ?> Ken Link to comment https://forums.phpfreaks.com/topic/114408-how-do-i-return-2-variables/#findComment-588316 Share on other sites More sharing options...
JasonLewis Posted July 12, 2008 Share Posted July 12, 2008 Array. return [$planet_stats,$planet_ammount]; Heck I'm not even sure you can use [ and ] in PHP, been coding javascript lately, but just return them in an array. EDIT: And kenrbnsn beat me to it. Only by 17 seconds! Link to comment https://forums.phpfreaks.com/topic/114408-how-do-i-return-2-variables/#findComment-588317 Share on other sites More sharing options...
Dethman Posted July 12, 2008 Author Share Posted July 12, 2008 Hey ProjectFear your return didnt work anyone else want to tell me another way like Project's to return other than an array (Havnt Learned Arrays Yet :'() Link to comment https://forums.phpfreaks.com/topic/114408-how-do-i-return-2-variables/#findComment-588319 Share on other sites More sharing options...
JasonLewis Posted July 12, 2008 Share Posted July 12, 2008 Heh, that's javascript (my bad, it's what I've been coding lately ). Look at kens post above mine, it tells you how to do it. Your code should become this: <?php function DP($id) { /* ok let begin this nightmare lol this functions is gonna call the planet names and list them for you */ $sql = "SELECT * FROM `plan` WHERE `userid` = '$id' ORDER BY `Plan_size` DESC"; $result = mysql_query($sql) or die('Query failed.' . mysql_error()); for ($i = 0; $i < mysql_num_rows($result);$i++) { $plan_Name = mysql_result($result,$i,"plan_name"); $plan_Size = mysql_result($result,$i,"plan_size"); $plan_Type = mysql_result($result,$i,"plan_attribute"); $plan_Prod = mysql_result($result,$i,"plan_product"); $plan_Defe = mysql_result($result,$i,"plan_defense"); if ($plan_Type == 1) { $attribute = number_format(ceil(125000 * $plan_Prod * pow(3.5,$plan_Size))); $type = " Defence"; $defamt = number_format(300000 * $plan_Defe); } elseif ($plan_Type == 2) { $attribute = number_format(ceil(135000 * $plan_Prod * pow(3.5,$plan_Size))); $type = " Attack"; $defamt = number_format(300000 * $plan_Defe); } elseif ($plan_Type == 3) { $attribute = number_format(ceil(500000 * $plan_Prod * pow(1.85,$plan_Size))); $type = " Spy"; $defamt = number_format(300000 * $plan_Defe); } elseif ($plan_Type == 4) { $attribute = number_format(ceil(480000 * $plan_Prod * pow(1.3,$plan_Size))); $type = " Income"; $defamt = number_format(300000 * $plan_Defe); } elseif ($plan_Type == 5) { $attribute = number_format(ceil($plan_Prod * pow(1.1,$plan_Size))); $type = " Up"; $defamt = number_format(300000 * $plan_Defe); } switch ($plan_Size) { case 0: $Size = "Tiny"; break; case 1: $Size = "Very Small"; break; case 2: $Size = "Small"; break; case 3: $Size = "Normal"; break; case 4: $Size = "Large"; break; case 5: $Size = "Big"; break; case 6: $Size = "Massive"; break; case 7: $Size = "On verg of a blackhole"; break; } $planet_stats=" <TR> <TD align=left>".$plan_name."</TD> <TD align=left>".number_format($plan_Prod)."</b> (+".$attribute.$type.")</TD> <TD align=left>".number_format($plan_Defe)."(".$defamt.")</TD> <TD align=lrft>".$Size."</TD> </TR> "; $planet_ammount=" <tr> <td colspan=4><center>".mysql_num_rows($result)." villages total</center></td> </tr> "; } return array($planet_stats,$planet_ammount); } ?> Then when you call the function: list($planet_stats,$planet_ammount) = DP("whatever you put in here"); Then you can use the two variables. But ken did post this above mine. Link to comment https://forums.phpfreaks.com/topic/114408-how-do-i-return-2-variables/#findComment-588321 Share on other sites More sharing options...
Dethman Posted July 12, 2008 Author Share Posted July 12, 2008 Hey guys the code worked wonderfully. You guys are doing an awesome job here, keep up the good work! Thanks, Brian T. Flores C.E.O NimbusGames,llc Link to comment https://forums.phpfreaks.com/topic/114408-how-do-i-return-2-variables/#findComment-588341 Share on other sites More sharing options...
JasonLewis Posted July 12, 2008 Share Posted July 12, 2008 No worries! Please remember to hit the "Topic Solved" button at the bottom of the thread page. Link to comment https://forums.phpfreaks.com/topic/114408-how-do-i-return-2-variables/#findComment-588342 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.