Semantic Web PHP


Semantic Web PHP

我正在使用一些查询从DBpedia获取数据。首先有一个下拉菜单,上面有所有博物馆的名字。在选择一个博物馆并点击搜索按钮后,它必须给我一些查询结果,但我有问题打印它。

错误提示:

注意:未定义变量:r in目录:'Xampp'htdocs'mercury'project'index.php第266行

注意:尝试获取非对象的属性目录:'Xampp'htdocs'mercury'project'index.php第266行

这是我的代码:

<!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>

这不是问题的原因,但它是一个非常相似的问题,如果您调用这个函数($g未使用,$graph未声明),肯定会给出类似的错误:

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; 
}

我认为这就是你的问题的原因。

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 **/
}

(注意我是如何剪掉不相关的部分的)

如果$result为空,$r从未被分配,因此一旦您尝试使用它,它是未定义的。