script does not seem to factor in isset in If

[php]
Nationstates Nationname:


Population (in millions): Leave blank if you want your nation’s population to be used.

[/php]

[php]

Nationstates Calculator

Version .21


<?php $start = (float) array_sum(explode(' ',microtime()));

$nationname = $_POST[“fname”];
$population = $_POST[“fpop”];

function get_data($nation, $shards = FALSE) {
if($shards != FALSE) {
$file = ‘http://www.nationstates.net/cgi-bin/api.cgi?nation=’ . $nation . ‘&q=’ . $shards;
}
else {
$file = ‘http://www.nationstates.net/cgi-bin/api.cgi?nation=’ . $nation;
}

  $xml = simplexml_load_file($file);
  return $xml;

}

function display_pop_civilrights($nation) {
$data = get_data($nation, ‘name+population+freedomscores’);

  $name = (string) $data->NAME;
  if (isset ($population)) {
  }
  else {
  $population = (string) $data->POPULATION;
  }
  $civil_rights = (string) $data->FREEDOMSCORES->ECONOMY;

  $gdp = round(pow($civil_rights, 1.5) * 350 * pow($population, .71) + $population * 100)* 1.5;
  $income = round($gdp / $population);
  $gdp =  number_format($gdp / 1000);
  $income = number_format($income);
  
  echo '<p><b>' . $name . '</b><br />Population: ' . $population . '<br />Economic Freedom: ' . $civil_rights . '<br>Avg. Capita Income/Annum ' . $income . ' <br> GNP ' . $gdp . ' billions </p>' ;   

  }

display_pop_civilrights(‘testlandia’);
display_pop_civilrights($nationname);

// Outputs:
// (bold) Testlandia
// Population: 18245
// Civil Rights: 74
// (bold) Glen-Rhodes
// Population: 7738
// Civil Rights: 77

include ‘bottom.php’;
include ‘linkback.php’;

$end = (float) array_sum(explode(’ ',microtime()));

echo “Processing time: “. sprintf(”%.4f”, ($end-$start))." seconds";
?>

[/php] Script basically should check if $population isset, and if so doesn't change population, but if not set then it takes population variable from an xml feed. But it doesn't.

if it’s only to change it when it’s not set then you don’t need an if/else, just an if.
[php]if(!isset($population)) {
$population = $data->POPULATION;
}[/php]

Red :wink:

http://usiga.vlexofree.com/nscalc1.php
It still don’t work.

not quite sure what i’m looking at there - or rather what is supposed to happen.

I entered some data and got some results, see screenshots

Red.


Capture.JPG

Capture2.JPG

I thought the code is self-explanatory.
If population has been entered, use that number.
If population has not been entered, use XML data.

Sponsor our Newsletter | Privacy Policy | Terms of Service