<?php
// worldbank_population_data.php
// RJM Programming
// February, 2017

$page=1;
$othercnt=0;
$pagecontent="<wb:indicator";
$year="2015";
$poplist="";
$popdelim=",%20";
$idelim="<wb:indicator";
$jdelim='<wb:country id="';
$lookingfor='"Afghanistan<';
$zero=0;
$one=1;
$cdelim="<";
$ddelim='<wb:value>';
if (isset($_GET['year'])) $year=$_GET['year'];
if (isset($_POST['year'])) $year=$_POST['year'];
$startswith='';
if (isset($_GET['startswith'])) $startswith=urldecode($_GET['startswith']);
if (isset($_POST['startswith'])) $startswith=urldecode($_POST['startswith']);
$url="http://www.rjmprogramming.com.au/PHP/PieChart/pie_chart.php?title=Population%20by%20World%20Country%20" . $startswith . "%20for%20year%20" . $year . "&onclick=y&task=Population&desc=Population&data=";

while (strpos($pagecontent, "<wb:indicator") !== false) {
  $pagecontent=@file_get_contents("http://api.worldbank.org/countries/all/indicators/SP.POP.TOTL?format=json&date=" . $year . ":" . $year . "&page=" . $page);
  if (strpos($pagecontent, $idelim) === false) {
     $idelim="Population, tot";
     $jdelim="Population, tot";
     $ddelim='"';
     $lookingfor='"Afghanistan"';
     $cdelim=',"value":"';
     $zero=10;
     $one=14;
     $pagecontent=str_replace(':null',':"0"',$pagecontent);
  }
  if (strpos($pagecontent, $idelim) !== false) {
     $entries=explode($jdelim, $pagecontent);
     //echo sizeof($entries);
     //echo $idelim . " ... " . $entries[1];
     //exit;
     for ($i=1; $i<sizeof($entries); $i++) {
       if ($lookingfor != "") {
         if (str_replace($lookingfor, '', $entries[$i]) != $entries[$i]) $lookingfor="";
       }
     //echo $ddelim . " ...vs... " . $entries[1];
     //exit;
       if ($lookingfor == "" && strpos($entries[$i], $ddelim) !== false) {
        $pentries=explode($ddelim, $entries[$i]);
        if (strpos('*' . explode("<", str_replace('">', '', $entries[$i]))[0], '*' . $startswith) !== false || strpos('*' . $pentries[$zero], '*' . $startswith) !== false) { 
         //echo $entries[$i];
         //echo explode("<", str_replace('">', '', $entries[$i]))[0];
         //exit;
          //echo $entries[$i] . "\n" . $ddelim . "\n" . $pentries[0] . " is 0\n";
          //echo $pentries[1] . " is 1\n";
          //echo $pentries[2] . " is 2\n";
          //echo $pentries[3];
          //exit;
          if ($idelim == "Population, tot") {
          $poplist.=$popdelim . "[~" . str_replace("%27","",str_replace(".","",str_replace("+","%20",urlencode($pentries[$zero])))) . "~," . str_replace("decimal","0",$pentries[$one]) . "]";
          } else {
          $poplist.=$popdelim . "[~" . str_replace("%27","",str_replace(".","",str_replace("+","%20",explode('"', explode($cdelim, str_replace('">', '', urlencode($entries[$i])))[$zero])[0]))) . "~," . str_replace("decimal","0",explode($cdelim, $pentries[$one])[0]) . "]";
          }
          $popdelim=",%20";
        } else {
          if ($idelim == "Population, tot") {
          $othercnt+=str_replace("decimal","0",$pentries[$one]);
          } else {
          $othercnt+=str_replace("decimal","0",explode($cdelim, $pentries[$one])[0]);
          }
        }
       }       
     }
     $pagecontent="<wb:indicator";
  }
  $page++;
}

if ($othercnt > 0 && !isset($_GET['justletters']) && !isset($_POST['justletters'])) {
  $url.=$poplist . $popdelim . "[~Non-" . $startswith . "~," . $othercnt . "]";
} else {
  $url.=$poplist;
}

echo "<!doctype html><html><head><title>World Bank API Population Data Google Chart Pie Chart - RJM Programming - February, 2016</title><scr" . "ipt type='text/javascript'> 
  var url='" . explode("&data=",$url)[1] . "'; 
  function preiframeviaurl() {
    if (document.getElementById('data').value != '') url=document.getElementById('data').value;
    return true;
  }
  function iframeviaurl() {
    if (parent.document.getElementById('childiframe')) {
       document.body.style.backgroundColor='lightblue';
    }
    if (('" . explode("&data=",$url)[0] . "&data=' + url).length > 750) {
       //alert(':' + url);
       document.getElementById('myiframe').style.display='block';
       document.getElementById('data').value=decodeURIComponent(url);
       document.getElementById('mysubmit').click();
    } else if (url != '') {
       //alert('" . explode("&data=",$url)[0] . "&data=' + url);
       document.getElementById('myiframe').style.display='block';
       document.getElementById('myiframe').src='" . explode("&data=",$url)[0] . "&data=' + url;
    }
    return true;
  }
</scr" . "ipt>
</head>
<body onload='iframeviaurl();'>
<h1>World Bank API Population Data Google Chart Pie Chart</h1>
<h3>RJM Programming</h3>
<h4>Thanks to <a target=_blank title='World Bank API documentation' href='https://datahelpdesk.worldbank.org/knowledgebase/articles/898581-api-basic-call-structure'>World Bank API</a></h4>
<iframe style='width:800px;height:600px;display:none;' name='myiframe' id='myiframe' src='about:blank'></iframe>
<form onsubmit='return preiframeviaurl();' target='myiframe' id='myform' style='display:none;' method='POST' action='http://www.rjmprogramming.com.au/PHP/PieChart/pie_chart.php'>
<input type='hidden' name='title' id='title' value='Population by World Country " . $startswith . " for year " . $year . "'></input>
<input type='hidden' name='onclick' id='onclick' value='y'></input>
<input type='hidden' name='task' id='task' value='Population'></input>
<input type='hidden' name='desc' id='desc' value='Population'></input>
<input type='hidden' name='wellinever' value='y'></input>
<input type='hidden' name='data' id='data' value='" . explode("&data=",$url)[1] . "'></input>
<input id='mysubmit' type='submit' value='Draw Pie Chart'></input>
</form>
</body>
</html>";

?>