我試圖從XML文件顯示記錄。但我得到以下錯誤:PHP-XML:試圖獲取非對象的屬性
A PHP Error was encountered Severity: Notice Message: Trying to get property of non-object Filename: controllers/events.php Line Number: 67
xml文件
<events>
<event>
<date>15/06/11</date>
<venue>Parijs - Frankfort</venue>
<name>SuperRunner 2011</name>
<url>cgi-bin/phts.sh/x/xbevwindex.p?xtren=1234&xmlac=np&tpl=sp11</url>
</event>
. . .
. . .
</events>
在我CONTROLER 'events.php'
function index(){
$data['events'] = $this->readXMLFile();
$data['title'] = "Latest Events";
$this->load->view('events/eventlistxml',$data);
}
function readXMLFile(){
$doc = new DOMDocument();
$doc->load('events.xml');
$events = $doc->getElementsByTagName("event");
$data= array();
foreach($events as $key=>$event):
$dates = $event->getElementsByTagName("date");
$data[$key]['date'] = $dates->item(0)->nodeValue;
$venues = $event->getElementsByTagName("venue");
$data[$key]['venue'] = $venues->item(0)->nodeValue;
$names = $event->getElementsByTagName("name");
$data[$key]['name'] = $names->item(0)->nodeValue;
$names = $event->getElementsByTagName("url");
$data[$key]['url'] = $names->item(0)->nodeValue;
endforeach;
return $data;
}
在視圖「事件/ eventlistxml.php '
<?php
foreach($events as $key=>$val):
echo $val['date']."<br />";
echo $val['venue']."<br />";
echo $val['url']."<br />";
endforeach;
?>
這是由於URL模式在XML 的cgi-bin/phts.sh/X/xbevwindex.p?xtren = 1234 & xmlac = NP & TPL = SP11 有沒有解決這個錯誤的方法?
謝謝你的支持。
WHere是行號:67上的文件 – safarov 2012-03-21 08:19:43
哪行67個events.php? – Magic 2012-03-21 08:26:46