<?php

// Yahoo Yql First Name Search Tutorial - RJM Programming - 2014
// {"query":{"count":1,"created":"2014-11-13T09:39:20Z","lang":"en-US","diagnostics":{"url":[{"execution-start-time":"0","execution-stop-time":"110","execution-time":"110","content":"http://www.datatables.org/firstnames/firstnames.lookup.xml"},{"execution-start-time":"112","execution-stop-time":"830","execution-time":"718","content":"http://www.thomas-bayer.com/restnames/name.groovy?name=Sebastian"}],"publiclyCallable":"true","user-time":"831","service-time":"828","build-version":"0.2.212"},"results":{"restnames":{"nameinfo":{"name":"Sebastian","countries":{"country":[{"href":"http://www.thomas-bayer.com:80/restnames/namesincountry.groovy?country=Great+Britain","content":"Great Britain"},{"href":"http://www.thomas-bayer.com:80/restnames/namesincountry.groovy?country=Ireland","content":"Ireland"},{"href":"http://www.thomas-bayer.com:80/restnames/namesincountry.groovy?country=U.S.A.","content":"U.S.A."},{"href":"http://www.thomas-bayer.com:80/restnames/namesincountry.groovy?country=Malta","content":"Malta"},{"href":"http://www.thomas-bayer.com:80/restnames/namesincountry.groovy?country=Belgium","content":"Belgium"},{"href":"http://www.thomas-bayer.com:80/restnames/namesincountry.groovy?country=Frisia","content":"Frisia"},{"href":"http://www.thomas-bayer.com:80/restnames/namesincountry.groovy?country=Germany","content":"Germany"},{"href":"http://www.thomas-bayer.com:80/restnames/namesincountry.groovy?country=Austria","content":"Austria"},{"href":"http://www.thomas-bayer.com:80/restnames/namesincountry.groovy?country=Swiss","content":"Swiss"},{"href":"http://www.thomas-bayer.com:80/restnames/namesincountry.groovy?country=Denmark","content":"Denmark"},{"href":"http://www.thomas-bayer.com:80/restnames/namesincountry.groovy?country=Poland","content":"Poland"},{"href":"http://www.thomas-bayer.com:80/restnames/namesincountry.groovy?country=Romania","content":"Romania"}]},"gender":"male first name","male":"true","female":"false"}}}}}

$nsuffix = "<br><br>(Male first name: true<br>Female first name: true)";
$selname = "";
if (isset($_POST['selname'])) $selname = str_replace(" ","%20",$_POST['selname']);
if (isset($_GET['selname'])) $selname = str_replace(" ","%20",$_GET['selname']);

if ($selname != "") {
 $pageContent = file_get_contents("https://query.yahooapis.com/v1/public/yql?q=SELECT%20*%20FROM%20firstnames.lookup%20WHERE%20name%3D%22".$selname."%22&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=");

 $laststuff = explode('}]},"gender":', $pageContent);
 if (strpos($laststuff[1], '"female":"false"') !== false) $nsuffix = str_replace("Female first name: true", "Female first name: false", $nsuffix);
 if (strpos($laststuff[1], '"male":"false"') !== false) $nsuffix = str_replace("Male first name: true", "Male first name: false", $nsuffix);

 $precont = explode('"country":[', str_replace('}]},"gender":' . $laststuff[1],'',$pageContent));

 $cont = str_replace("{","",str_replace("]","",str_replace("}","",str_replace("[","",$precont[1]))));

 $commastuff = explode(",", $cont);
 if (strpos($nsuffix, "Female first name: true") !== false) {
   echo "<!doctype HTML><html><head><link href='//www.rjmprogramming.com.au/PHP/emboss_h1.css' rel='stylesheet' type='text/css'></head><body align='center' style='background-color:yellow;'><h1 align='center'>" . urldecode($selname) . "</h1><br><div align='center'><table border=1 cellpadding=5><tr><td style='background-color:pink;'><p id='fname'>" . urldecode($selname) . $nsuffix . "</p></td><td style='background-color:lightgray;'>";
 } else {
   echo "<!doctype HTML><html><head><link href='//www.rjmprogramming.com.au/PHP/emboss_h1.css' rel='stylesheet' type='text/css'></head><body align='center' style='background-color:yellow;'><h1 align='center'>" . urldecode($selname) . "</h1><br><div align='center'><table border=1 cellpadding=5><tr><td style='background-color:lightblue;'><p id='fname'>" . urldecode($selname) . $nsuffix . "</p></td><td style='background-color:lightgray;'>";
 }
 for ($i=0; $i<sizeof($commastuff); $i+=2) {
   echo "<a target=_blank title='" . urldecode($selname) . " in " . str_replace('content:','',str_replace('"','',$commastuff[1 + $i])) . "' href='" . str_replace('href:','',str_replace('"','',$commastuff[0 + $i])) . "'>" . str_replace('content:','',str_replace('"','',$commastuff[1 + $i])) . "</a><br>"; 
 }

 echo "</td></tr></table></div></body></html>";
}
?> 

