<?php

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


$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";
$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",$_POST['mode'] . $altsuffix);
if (isset($_GET['mode'])) $suffix = str_replace(" ","%20",$_GET['mode'] . $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");

if ($selname != "") {

 date_default_timezone_set('London/England');
 $date = date('d/m/Y h:i:s a', time()) . " GMT";            
 
 $pageContent = file_get_contents("https://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:1400px;overflow:scroll;' ";
  $postsuff = explode(',"content":"', $pageContent);
  if (sizeof($postsuff) > 1) {
   $ecountry = explode('"', $postsuff[sizeof($postsuff) - 1]);
   $ititle = "Worldbank Data ... Average Years of Primary Schooling in " . str_replace("+", " ", str_replace("+d%27", " d", urlencode($ecountry[0]))) . ", 35-39, Total";
   $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";
            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.data+WHERE+country_id+%3D+%27TUR%27+AND+indicator_id+%3D+%27BAR.PRM.SCHL.3539%27'>Data</a>", $ititle) . " ... " . $date . "</h1><br>";
			    echo "<!doctype HTML><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'>";
                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]

                  $stuff = "<iframe align='center' style='width:1000px;height:360px;margin-top:-390px;' src='http://www.rjmprogramming.com.au/PHP/ColumnChart/column_chart.php?title=" . urlencode($ititle) . "&label=Year&value=Years&data=";
                  $pstuff = "";
                  $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 " colspan=1 align='center'>" . str_replace("margin-top:-90px;","align:top;",$stuff . $pstuff) . "'></iframe><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='http://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='http://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>";
                }
                echo "</td></tr></tbody></table>";
            }
 			echo "</div></body></html>";
// }

}
?> 

