Semantic Web PHP

814 Views Asked by At

I'm using some query to get data from DBpedia. First there is a drop down which has all the names of the museums. After selecting one museum and clicking the search button it has to give me some query results but I'm having problems printing it.

The error says:

Notice: Undefined variable: r in G:\Xampp\htdocs\mercury\project\index.php on line 266

Notice: Trying to get property of non-object in G:\Xampp\htdocs\mercury\project\index.php on line 266

This is my code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css" />
<title>Semantic Web</title>
</head>
<body>

<div id="wrap">

<div id="header">
<h1><a href="#">Semantic Museum Search</a></h1>
</div>

<div id="menu">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Links</a></li>
<li><a href="#">Documentation</a></li>
</ul>
</div>

<div id="contentwrap"> 

<div id="content">

<?php



 function DBPedia ($museum)
   { 

   //  $str = substr($mueseum1, 27);

   // $mueseum = 'http://dbpedia.org/page'.$str;


        // $result = array();

        // $graph = EasyRdf_Graph::newAndLoad("".$museum.""); //it loads all the RDF

        // $name = $graph->getLiteral("".$museum."","rdfs:label","en");//It gets all the given data (The URI of the resource (e.g. http://example.com/joe#me) ,The name of the property (e.g. foaf:name), The language to filter by (e.g. en))
        // $flag = $graph->get("".$museum."","foaf:homepage");
        // $lat = $graph->get("".$museum."",'geo:lat');
        // $long =$graph->get("".$museum."",'geo:long')  ;    
        // $abstract = $graph->getLiteral("".$museum."","dbpedia-owl:abstract","en");


        // $result = array($name,$lat,$long,$flag,$abstract);

        // return $result;


   }


   function home($home1){

   $str = substr($home1, 27);

   $home = 'http://dbpedia.org/page'.$str;

   echo $home.'<br/><br/>';


    $g = EasyRdf_Graph::newAndLoad("".$home."");

    $hom = $graph->get("".$museum."",'foaf:homepage');

    return $hom;

   }


    /**
* Making a SPARQL SELECT query
*
* This example creates a new SPARQL client, pointing at the
* dbpedia.org endpoint. It then makes a SELECT query that
* returns all of the countries in DBpedia along with an
* english label.
*
* Note how the namespace prefix declarations are automatically
* added to the query.
*
* @package EasyRdf
* @copyright Copyright (c) 2009-2012 Nicholas J Humfrey
* @license http://unlicense.org/
*/


    set_include_path(get_include_path() . PATH_SEPARATOR . '../lib/');
    require_once "EasyRdf.php";
    require_once "html_tag_helpers.php";



    // Setup some additional prefixes for DBpedia
    EasyRdf_Namespace::set('category', 'http://dbpedia.org/resource/Category:');
    EasyRdf_Namespace::set('dbpedia', 'http://dbpedia.org/resource/');
    EasyRdf_Namespace::set('dbo', 'http://dbpedia.org/ontology/');
    EasyRdf_Namespace::set('dbp', 'http://dbpedia.org/property/');
    EasyRdf_Namespace::set('factbook', 'http://wifo5-04.informatik.uni-mannheim.de/factbook/ns#');
    // EasyRdf_Namespace::set('uniport','http:xx//purl.uniprot.org/uniprot/');

    $sparql = new EasyRdf_Sparql_Client('http://dbpedia.org/sparql');
?>
<html>
<head>
<title>EasyRdf Basic Sparql Example</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
</head>
<body>

<h1>EasyRdf Basic Sparql Example</h1>
<h2>List of museums</h2>





<?php
    $result = $sparql->query(
    'SELECT DISTINCT * WHERE {'.
    '?architectural_structure rdf:type dbpedia-owl:Museum .'.
    '?architectural_structure dbpprop:name ?name .'.
    '?architectural_structure dbpprop:location ?location .'.
    '?architectural_structure dbpedia-owl:abstract ?abstract .'.
    '?architectural_structure foaf:homepage ?homepage.'.
    '?architectural_structure dbpprop:collection ?collection.'.
    '?architectural_structure dbpedia-owl:thumbnail ?thumbnail.'.


 'FILTER(langMatches(lang(?abstract),"EN")) .'.

'}ORDER BY ?name'


    );



function data($uri){



global $sparql;


  $result = $sparql->query(
    'SELECT DISTINCT * WHERE {'.
    '$uri rdf:type dbpedia-owl:Museum .'.
    '?architectural_structure dbpprop:name ?name .'.
    '?architectural_structure dbpprop:location ?location .'.
    '?architectural_structure dbpedia-owl:abstract ?abstract .'.
    '?architectural_structure foaf:homepage ?homepage.'.
    '?architectural_structure dbpprop:collection ?collection.'.
    '?architectural_structure dbpedia-owl:thumbnail ?thumbnail.'.


 'FILTER(langMatches(lang(?abstract),"EN")) .'.

'}ORDER BY ?name'


    );
  //return $result;

  foreach ($result as $r){

if($r->architectural_structure == $uri)
{
  return $r;
}
  }


}




echo '<form method="post" align="center" action="index.php?page=architectural_structure">';
//echo '<input align="center" type="text" name="search" />';
echo '<select name="architectural_structure1" align="center"><OPTION>';
echo "Select an option</OPTION> ";
    foreach ($result as $row) {       
        echo "<OPTION value=\"$row->architectural_structure\">$row->name</OPTION>";
    }
echo '</SELECT>';


echo '<input name="search" type="submit" id="search" value="Search" />';
echo '</form>';

if(isset($_GET['page']))
{ 
  $museum1=$_POST['architectural_structure1'];
 }


if(isset($_GET['page'])&& ($_GET['page']== 'architectural_structure')){




$c = data($result->architectural_structure);
echo ($c->name);









}




?>

</div>



<div style="clear: both;"> </div>

</div>

<div id="footer">
<p>&copy; Copyright 2015 You | Design by Bashkim Bekteshi </p>
</div>

</div>

</body>
</html>
1

There are 1 best solutions below

0
On

This is not the cause of your problem, but it is a very similar problem, which will definitely give a similar error if you ever call this function ($g is unused, and $graph is undeclared):

function home($home1){
    $str = substr($home1, 27);
    $home = 'http://dbpedia.org/page'.$str;
    echo $home.'<br/><br/>';
    // $g is unused, and $graph is undeclared...
    $g = EasyRdf_Graph::newAndLoad("".$home."");
    $hom = $graph->get("".$museum."",'foaf:homepage');
    return $hom; 
}

I think this is the cause of your problem:

foreach ($result as $r){
    if($r->architectural_structure == $uri)
        {
        return $r;
        }
    }
}
/** SNIP **/
if(isset($_GET['page'])&& ($_GET['page']== 'architectural_structure')){
    $c = data($r->architectural_structure);
    /** SNIP **/
}

(Note how I cut out irrelevant parts)

If $result is empty $r is never assigned, and therefore it is undefined once you try to use it.