I have the following xml which i want to filter to show only data from a specify publisher.
<?xml version="1.0"?>
<books>
<book isbn="978-1594489501">
<title>A Thousand Splendid Suns</title>
<author>Khaled Hosseini</author>
<publisher>Riverhead Hardcover</publisher>
<amazon_price>14.27</amazon_price>
</book>
<book isbn="978-1594489587">
<title>The Brief Wondrous Life of Oscar Wao</title>
<author>Junot Diaz</author>
<publisher>AB</publisher>
<amazon_price>14.97</amazon_price>
</book>
<book isbn="978-0545010221">
<title>Harry Potter and the Deathly Hallows</title>
<author>J. K. Rowling</author>
<publisher>AB</publisher>
<amazon_price>19.24</amazon_price>
</book>
</books>
i want to put in an html table only data from example publisher AB.
<?php
// load SimpleXML
$books = new SimpleXMLElement("books.xml);
$myVar="AB";
$books = $s->xpath("//book");
echo <<<EOF
<table style='width: 99%;'>
<thead>
<tr>
<th>Title</th>
<th>Author</th>
</tr>
</thead>
EOF;
foreach($books as $book) // loop through our books
{
//if the type of $product books user input (ie $myVar)
if ($book->type == $myVar)
{
echo <<<EOF
<tbody>
<tr>
<td>{$book->title} </td>
<td>{$book->author}</td>
</tr>
EOF;
}}
echo '</tbody>';
echo '</table>';
?>
I am getting Call to a member function xpath() on a non-object, any help please.
If you want to load data from a file via the constructor of SimpleXMLElement you have to set the third parameter to
true. Otherwise the string you pass as first parameter is interpreted as the “raw” xml data, i.e. eitheror
or
and there are some other mistakes in the code. E.g. you want to test for a specific value in the
<publisher>element but you are testingif ($book->type == $myVar).It’s probably better in this case to put the condition into the xpath expression