<?php

// Yahoo Yql Geodata Primer Tutorial - RJM Programming - 2015

$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%20geo.placefinder%20where%20text%3D%22' . $selname . '%22&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=',0,null,null);
 		$json_output = json_decode($pageContent); //  ,true);

        // Confirm that results were returned before parsing
        $delim="";
        $prefix="";
        $suffix="";
        if (!is_null($json_output->query->results)) {
          $i = 0;
          if ($json_output->query->count == 1) {
            echo "\n<table border=1 cellpadding=5><tr>";
            $title = str_replace("'", "`", $json_output->query->results->Result->city.", ".$json_output->query->results->Result->county.", ".$json_output->query->results->Result->state.", ".$json_output->query->results->Result->country);
            $imageurl = "https://www.google.com.au/search?q=" . str_replace(" ", "+", $title) . "&biw=1280&bih=672&source=lnms&tbm=isch&sa=X&ei=LvkMVYztFMLfmgXAroKgAw&ved=0CAcQ_AUoAg";
            echo "<td><p style='border: red 2px solid; padding: 2 2 2 2;'><a target=_blank title='Images of " . $title . "' href='" . $imageurl . "'>" . $title . "</a></p>";
            echo "\n<iframe height=500 title='" . $title . "' src='http://www.rjmprogramming.com.au/PHP/Map/map.php?title=" . urldecode($title) . "&label=[%27Lat%27,&value=%27Lon%27,%20%27Name%27]&data=,%20[" . $json_output->query->results->Result->latitude . "," . $json_output->query->results->Result->longitude . ",~" . $selname . "~]'></iframe>";
            echo "\n</td>";
          } else {
            $title = str_replace("'", "`", $selname);
            $imageurl = "https://www.google.com.au/search?q=" . str_replace(" ", "+", $title) . "&biw=1280&bih=672&source=lnms&tbm=isch&sa=X&ei=LvkMVYztFMLfmgXAroKgAw&ved=0CAcQ_AUoAg";
            $delim = "\n<table border=1 cellpadding=5><tr><td style='border:9px yellow solid;; padding: 2 2 2 2;'><a target=_blank title='Images of " . $title . "' href='" . $imageurl . "'>" . $title . "</a></p>\n<iframe height=500 title='" . $title . "' src='http://www.rjmprogramming.com.au/PHP/Map/map.php?title=" . urldecode($title) . "&label=[%27Lat%27,&value=%27Lon%27,%20%27Name%27]&data=,%20[";
            $suffix = "]'></iframe></td>";
            for ($i=0; $i<$json_output->query->count; $i++) {
                $title = str_replace("'", "`", $json_output->query->results->Result[$i]->city.", ".$json_output->query->results->Result[$i]->county.", ".$json_output->query->results->Result[$i]->state.", ".$json_output->query->results->Result[$i]->country);
                $imageurl = "https://www.google.com.au/search?q=" . str_replace(" ", "+", $title) . "&biw=1280&bih=672&source=lnms&tbm=isch&sa=X&ei=LvkMVYztFMLfmgXAroKgAw&ved=0CAcQ_AUoAg";
                $prefix .= ($delim . $json_output->query->results->Result[$i]->latitude . "," . $json_output->query->results->Result[$i]->longitude . ",~" . urldecode($title) . "~");
                $delim = "]%20,%20[";
                $suffix .= "<td><p style='border: red 2px solid; padding: 2 2 2 2;'><a target=_blank title='Images of " . $title . "' href='" . $imageurl . "'>" . $title . "</a></p>";
                $suffix .= "\n<iframe height=500 title='" . $title . "' src='http://www.rjmprogramming.com.au/PHP/Map/map.php?title=" . urldecode($title) . "&label=[%27Lat%27,&value=%27Lon%27,%20%27Name%27]&data=,%20[" . $json_output->query->results->Result[$i]->latitude . "," . $json_output->query->results->Result[$i]->longitude . ",~" . $selname . "~]'></iframe>";
                $suffix .= "\n</td>";
            }
          }
          echo $prefix . $suffix . "</tr></table>";
        }
}
?> 

