<?php
// country_capital_quiz.php
// RJM Programming
// November, 2016
// Country capital quiz ... thanks to TikiWiki regarding flag country list, and to https://www.countries-ofthe-world.com/capitals-of-the-world.html for capitals themselves


// Eg. <tr class="grey"><td>Bahamas</td><td>Nassau</td></tr> ... thanks
$capbits=@file_get_contents("https://www.countries-ofthe-world.com/capitals-of-the-world.html");

$thiscap="";

function countryid($cinis) {
  return str_replace('.', '_', str_replace(' ', '_', str_replace('"', '_', str_replace("'", "_", $cinis))));
}

function mapcapital($cincountry) {
  global $capbits, $thiscap;
  $retcap="";
  $thiscap="";
  $ctrys=explode(" title='", $cincountry);
  if (sizeof($ctrys) > 1) {
    $ctryiss=explode("'", $ctrys[1]);
    $huhbits=explode(">" . $ctryiss[0], $capbits);
    if (sizeof($huhbits) == 1) $huhbits=explode("(" . $ctryiss[0], $capbits);
    if (sizeof($huhbits) > 1) {
      $posthuhbits=explode("</td><td>", $huhbits[1]);
      if (sizeof($posthuhbits) > 1) {
       $preposthuhbits=explode("</td>", $posthuhbits[1]);
       $thiscap=str_replace('.', '_', str_replace(' ', '_', str_replace('"', '_', str_replace("'", "_", str_replace("'", "`", $preposthuhbits[0])))));
       $retcap=str_replace("title='" . $ctryiss[0] . "", "title='" . str_replace("'", "`", $preposthuhbits[0]) . "", $cincountry);
      }
    }
  }
  return $retcap;
}

$num=0;
$countries=[];
$capitals=[];
$countryids=[];
$htmlis="<!doctype html><html><head></head><body onload=choose();><h1>Country Capital Quiz</h1><h2 id=score>Score: 0 Goes: 0</h2><h3>RJM Programming</h3><h3>November, 2016</h3><br><br><br><table style='width:100%;'><tbody style='background-color:#f0f0f0;'><tr><th style='text-align:right;'>Country</th><th style='text-align:left;'><select onchange=' location.href=\"http://www.rjmprogramming.com.au/PHP/country_\" + this.value.toLowerCase() + \"_quiz.php\"; '><option value=Capital>Capital</option><option value=Currency>Currency</option><option value=Flag>Flag</option></select>?</th></tr><tr><td id=tdcountry style='background-color:pink;text-align:right;'></td><td id=tdcapital style='text-align:left;'><input id=icapital onblur='check(this);' type=text value=></input></td></tr></tbody></table></body></html>";
$scriptis="<meta charset='UTF-8'><title>Country Capital Quiz</title> \n<link href='//www.rjmprogramming.com.au/PHP/emboss_h1.css' rel='stylesheet' type='text/css'> \n <style> body { background-color:lightblue; }   </style> \n <scri" . "pt type='text/javascript'> var choice=-1, score=0, goes=0, num=0, capitals=[], countries=[], countryids=[]; function ourcomp(c1,c2) { var xc1=c1.replace(/_/g,' ').replace(/`/g,' '); var xc2=c2.split('-')[1].replace(/_/g,' ').replace(/`/g,' ');  if (xc2.toLowerCase().indexOf(xc1.toLowerCase()) != -1 && xc1.toLowerCase().length > 3) { return true; } return false; }  \n function check(inv) { if (inv.value != '') { var suffix=''; goes++; if (ourcomp(inv.value,document.getElementById('country').value)) { score++; } else {  suffix=' The country ' + document.getElementById(countryids[choice]).id.replace(/_/g,' ') + ' represented by <img src=' + document.getElementById(countryids[choice]).src + '></img> has the capital city ' + document.getElementById(countryids[choice]).title; }  document.getElementById('score').innerHTML='Score: ' + score + ' Goes: ' + goes + suffix;   choose(); } } \n function choose() {  choice = Math.floor(Math.random() * num); document.getElementById('country').value=countryids[choice] + '-' + capitals[choice]; document.getElementById('icapital').value=''; setTimeout(fit, 1500);  } function fit() {  document.getElementById('icapital').focus();  } \n</scr" . "ipt>";
$seloneis="<select id=country><option value=>Country shown below ...</option></select>";
$divis="<div id=flags></div>";

foreach (glob("../tikiwiki/img/flags/*.gif") as $filename) {
  $bits=explode("/", $filename);
  $postbits=explode(".", $bits[-1 + sizeof($bits)]);
  $idis=countryid($postbits[0]);
  $outcapital=mapcapital("<img src='http://www.rjmprogramming.com.au/tikiwiki/img/flags/" . $postbits[0] . ".gif' style='display:none;' id='" . $idis . "' title='" . str_replace("_", " ", $postbits[0]) . "'></img>");
  if ($outcapital != "") {
   $countries[$num]=$postbits[0];
   $scriptis=str_replace("</scr" . "ipt>", " countries.push(\"" . str_replace("_", " ", $postbits[0]) . "\");  countryids.push(\"" . $idis . "\");  capitals.push(\"" . $thiscap . "\"); num++; \n</scr" . "ipt>", $scriptis);
   $countryids[$num]=$idis;
   $seloneis=str_replace("</select>", "<option value=" . $idis . "-" . $thiscap . ">" . str_replace("_", " ", $postbits[0]) . "</option></select>", $seloneis);
   $divis=str_replace("</div>", $outcapital . "</div>", $divis);
   $num++;
  }
}

echo  str_replace("</body>", $divis . "<input type='text' style='margin-left:-6786px;width:1px;height:1px;' value=''></input></body>", str_replace("<head></head>", "<head>" . $scriptis . "</head>", str_replace("</td><td", $seloneis . "</td><td", $htmlis)));

?>
