Jump to content

How do I return 2 variables?


Dethman

Recommended Posts

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

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

Heh, that's javascript (my bad, it's what I've been coding lately :P). 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.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.