<?php

// Yahoo Yql Worldbank Country Search Tutorial - RJM Programming - 2015

$rest = "";
$xrest = array("");
$gglist = array("/ColumnChart/column_chart.php", "/BarChart/bar_chart.php", "/AreaChart/area_chart.php");
$ourgg = $gglist[rand(0, (sizeof($gglist) - 1))];

//$dollars = "US\$";
//$altsuffix = str_replace(" ", "%20", " AND indicator_id = 'NY.GNP.PCAP.CD' "); // GNI per capita, Atlas method (current US$)
//$alttitle = "Worldbank Data ... GNI per capita, Atlas method (current US\$)";
$dollars = "Years";
$altsuffix = str_replace(" ", "%20", " AND indicator_id = 'BAR.PRM.SCHL.3539' "); // average years of primary schooling, 35-39, total
$alttitle = "Worldbank Data ... Average Years of Primary Schooling, 35-39, Total";
$pgm = "";
if (isset($_POST['mode'])) {
 $pgm = $_POST['mode'];
}
if (isset($_GET['mode'])) {  
 $pgm = $_GET['mode'];
}
if ($pgm != "") {
  $xrest = explode(",", $pgm);
  if (sizeof($xrest) > 1) {
   $rest = str_replace("~", "", str_replace("," . $xrest[(sizeof($xrest) - 1)] . "~", "", ($pgm . "~")));
   $pgm = $xrest[(sizeof($xrest) - 1)];
  }
  if (str_replace(".", "", $pgm) != $pgm) {
    $dollars = "US\$";
    if ($pgm == "NY.GNP.PCAP.CD") $alttitle = "Worldbank Data ... GNI per capita, Atlas method (current US\$)";
    if ($pgm == "NY.GNP.PCAP.KD") $alttitle = "Worldbank Data ... GNI per capita (constant 2005 US\$)";
    if ($pgm == "NY.GDP.PCAP.KD") $alttitle = "Worldbank Data ... GDP per capita (constant 2005 US\$)";
    if ($pgm == "NY.GDP.PCAP.CD") $alttitle = "Worldbank Data ... GDP per capita, Atlas method (current US\$)";
    $altsuffix = str_replace(" ", "%20", " AND indicator_id = '" . $pgm . "' ");
  }
  if ($pgm == "3539" || $pgm == "1519") {
   $dollars = "Years";
   $altsuffix = str_replace(" ", "%20", " AND indicator_id = 'BAR.PRM.SCHL." . $pgm . "' ");
   if ($pgm == "1519") {
     $alttitle = "Worldbank Data ... Average Years of Primary Schooling, 15-19, Total";
   } else if ($pgm == "3539") {
     $alttitle = "Worldbank Data ... Average Years of Primary Schooling, 35-39, Total";
   }
  }
}
$alttsuff = "data";
$altthing = "data";
$altthingtwo = "results->data";
$suff = "countries";
$yesh1 = " ";
$thing = "country";
$thw = "";
$thingtwo = "results";
$suffix = "";
$selname = "";
$ititle = " ... Worldbank Countries";
if (isset($_POST['noh1'])) $yesh1 = "";
if (isset($_GET['noh1'])) $yesh1 = "";
if (isset($_POST['selname'])) $selname = str_replace(" ","%20",$_POST['selname']);
if (isset($_GET['selname'])) $selname = str_replace(" ","%20",$_GET['selname']);
if (isset($_POST['mode'])) $suffix = str_replace(" ","%20", $altsuffix);
if (isset($_GET['mode'])) $suffix = str_replace(" ","%20", $altsuffix);
if ($suffix != "") $suff = $alttsuff;
if ($suffix != "") $ititle = $alttitle;
if ($suffix != "") $thing = $altthing;
if ($suffix != "") $thingtwo = $altthingtwo;

$bcolours = array("white", "pink", "aqua", "lime", "silver", "lightyellow", "cyan", "fuchsia", "orange", "lightblue", "lightgreen", "lightgray");
$bits = "margin-top:-390px;";

if ($selname != "") {

 date_default_timezone_set('London/England');
 $date = date('d/m/Y h:i:s a', time()) . " GMT";            
 
 $pageContent = file_get_contents("http://query.yahooapis.com/v1/public/yql?q=SELECT%20*%20FROM%20worldbank." . $suff . "%20WHERE%20country_id%20%3D%20'" . $selname . "'%20" . $suffix . "&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=");


 if ($ititle == $alttitle) {
  $thw = " style='height:" . (1200 + sizeof($xrest) * 500) . "px;overflow:scroll;background-color:pink;' ";
  $bits = "margin-top:-" . (440 + 100 * sizeof($xrest)) . "px;";
  $postsuff = explode(',"content":"', $pageContent);
  if (sizeof($postsuff) > 1) {
   $ecountry = explode('"', $postsuff[sizeof($postsuff) - 1]);
   $clist = explode(",", $alttitle);
   $firstbit = $clist[0];
   $lastbit = str_replace($clist[0], "", $alttitle);
   if (strlen($lastbit) > 0) $lastbit = str_replace(",,", ",", "," . $lastbit);
   $ititle = $firstbit . " in " . str_replace("+", " ", str_replace("+d%27", " d", urlencode($ecountry[0]))) . $lastbit;
   $date = "";
  }
 }

 //echo $pageContent;

 $json_output = json_decode($pageContent);  
 
 // Confirm that results were returned before parsing
// if (!is_null(eval("$" . "json_output->query->results->" . $thing))) {
            //echo "yessir";
            $stuff = "\n";
            $pstuff = "";
            $prebits = " colspan=1 align='center'>";
            if (isset($_GET['andthen'])) $bits = "";
            if (isset($_GET['andthen'])) $prebits = "";
            foreach($json_output->query->results as $event) {
                if ($yesh1 != "") $yesh1 = "<h1 align='center'>" . $event->name . str_replace("Worldbank Data", "Worldbank <a target=_blank title='Thanks to Yahoo YQL and Worldbank and Barro-Lee' href='https://developer.yahoo.com/yql/console/?q=select%20*%20from%20flickr.photos.interestingness%2820%29&env=store://datatables.org/alltableswithkeys#h=SELECT+*+FROM+worldbank.indicators+WHERE+indicator_id+%3D+%27NY.GNP.PCAP.CD%27'>Data</a>", $ititle) . " ... " . $date . "</h1><br>";
                if ($yesh1 != "" && sizeof($xrest) > 1) $yesh1 = "<h1 align='center'>Worldbank <a target=_blank title='Thanks to Yahoo YQL and Worldbank and Barro-Lee' href='https://developer.yahoo.com/yql/console/?q=select%20*%20from%20flickr.photos.interestingness%2820%29&env=store://datatables.org/alltableswithkeys#h=SELECT+*+FROM+worldbank.indicators+WHERE+indicator_id+%3D+%27NY.GNP.PCAP.CD%27'>Data</a> for " . str_replace("+", " ", str_replace("+d%27", " d", urlencode($ecountry[0]))) . " ... " . $date . "</h1><br>";
                if (isset($_GET['andthen'])) $yesh1 = "";
			    echo "<!DOCTYPE html>\n<html><head><meta charset='UTF-8' /><link href='//www.rjmprogramming.com.au/PHP/emboss_h1.css' rel='stylesheet' type='text/css'></head><body align='center' style='background-color:yellow;'>" . $yesh1 . "<div align='center'>";
                if (!isset($_GET['andthen'])) echo "<table" . $thw . "><tbody><tr><td style='border:2px solid cyan;'";
                if ($suffix != "") {
                  //http://www.rjmprogramming.com.au/PHP/ColumnChart/column_chart.php?title=Areas%20of%20Indian%20Regions&label=Region&value=Area%20%28square%20km%29&data=,%20[~Odisha~,123456]%20,%20[~Tamil%20Nadu~,234567]
                  $pstuff = "";
                  if (sizeof($xrest) > 1) {
                    //$bits = "";
                    for ($jj=0; $jj<(sizeof($xrest) - 1); $jj++) {
                      $stuff .= "<iframe align='center' style='width:1000px;height:360px;" . $bits . "' src='worldbank_country.php?selname=" . $selname . "&andthen=y&mode=" . $xrest[$jj] . "'></iframe><br>";
                      $bits = "";
                    }
                  }
                  $stuff .= "<iframe align='center' style='width:1000px;height:360px;" . $bits . "' src='//www.rjmprogramming.com.au/PHP" . $ourgg . "?title=" . urlencode($ititle) . "&label=Year&value=" . $dollars . "&data=";
                  $dstuff = explode('"date":"', $pageContent);
                  for ($ids=1; $ids<=sizeof($dstuff); $ids++) {
                    $vstuff = explode('","value":"', str_replace("null,", "", $dstuff[$ids]));
                    $evstuff = explode('"', $vstuff[1]);
                    if (str_replace("decimal", "", $evstuff[0]) != "") {
                      $pstuff = ",%20[~" . $vstuff[0] . "~," . str_replace("decimal", "", $evstuff[0]) . "]%20" . $pstuff;
                    }
                  }
                  //echo " colspan=1 align='center'>" . $stuff . "'></iframe></td></tr><tr><td style='border:2px solid cyan;'><iframe style='width:1200px;height:600px;margin-top:-150px;' src='" . str_replace("&mode=", "&noh1=", $_SERVER['REQUEST_URI']) . "'></iframe>";
                  echo $prebits . str_replace("margin-top:-90px;","align:top;",$stuff . $pstuff) . "'></iframe>";
                  if (!isset($_GET['andthen'])) echo "<br><iframe style='width:1200px;height:600px;' src='" . str_replace("&mode=", "&noh1=", $_SERVER['REQUEST_URI']) . "'></iframe>";
                } else {
                  echo ">";
                  echo "<iframe style='width:500px;height:500px;' src='//www.rjmprogramming.com.au/PHP/Map/map.php?title=" . str_replace(" ", "%20", urlencode($event->capitalCity)) . "&label=[%27Lat%27,&value=%27Lon%27,%20%27Name%27]&data=,%20[" . $event->latitude . "," . $event->longitude . ",~" . str_replace(" ", "%20", urlencode($event->name)) . "~]'></iframe>";
                  echo "</td><td style='width:60%;border:2px solid pink;'>";
                  echo "<iframe style='width:500px;height:500px;' src='//www.rjmprogramming.com.au/PHP/GeoChart/geo_chart.php?title=" . str_replace(" ", "%20", urlencode($event->name)) . "&width=496&height=347&country=Country&popularity=Dummy&data=[~" . str_replace(" ", "%20", urlencode($event->name)) . "~,0]'></iframe>";
                }
                if (!isset($_GET['andthen'])) echo "</td></tr></tbody></table>";
            }
 			echo "</div></body></html>";
// }

}
?> 

