<?php

// Yahoo Yql Black Country History Search Tutorial - RJM Programming - 2014

$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("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20blackcountryhistory.search%20where%20q%3D'" . $selname . "'&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=");
 //file_put_contents('/Applications/MAMP/htdocs/yql.txt', $pageContent);
 $json_output = json_decode($pageContent);  // .true
 //file_put_contents('/Applications/MAMP/htdocs/yqlvd.txt', var_dump($json_output));

        // Confirm that results were returned before parsing
        if(!is_null($json_output->query->results)){
                //echo "yessir";
            foreach($json_output->query->results->item as $event){
                //echo "yes";
                echo "<table border=1 cellpadding=5><tr><td><p style='border: red 2px solid; padding: 2 2 2 2;'>".$event->title." "."</p>";
                echo "<p padding: 2 2 2 2;'>".$event->description."</p>";
                //echo "<p style='border: yellow 1px solid; padding: 2 2 2 2;'>".$event->description."</p>";
                if (strpos($event->link[0]->content, "http") !== false) {
                echo "<p style='border: lightgreen 3px solid; padding: 2 2 2 2;'><a target=_blank title='New Window' href='".$event->link[0]->content."'>".$event->link[0]->content."</a></p></td></tr></table>";
                } else {
                echo "<p style='border: lightgreen 3px solid; padding: 2 2 2 2;'><a target=_blank title='New Window' href='http://blackcountryhistory.org".$event->link[0]->content."'>http://blackcountryhistory.org".$event->link[0]->content."</a></p></td></tr></table>";
				}
            }
        }
}
?> 

