Please excuse my newness. Currently I know how to execute a php script by simply naming the script [filename].php and browsing to that file on my webserver. I have also learned how to parse and display an xml file using php. What I want to do is have a .xml file like:

<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
  <book category="COOKING">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book category="CHILDREN">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
</bookstore>

Current I can display all of the information like:

<?php
$xml = simplexml_load_file("test.xml");

echo $xml->getName() . "<br />";

foreach($xml->children() as $child)
  {
  foreach($child->children() as $subchild)
    {
    echo $subchild->getName() . ": " . $subchild . "<br />";
    }
  }
?>

But instead, what I want to do is on one page, say bookstore.html, list all of the book titles. Then if I click on a book title, I want to display a new page with all of the information about that book. The only way I can figure out to do this is to manually make a [booktitle].php for each book, but that is completely defeating the purpose of using xml :)

Can anyone point me in the right direction?

Thanks,

David

<?php$xml = simplexml_load_file("test.xml"); echo $xml->getName() . "<br />";
 foreach($xml->children() as $child)  {
  foreach($child->children() as $subchild)    {
    echo "<a href=bookdetails.php?".$child->getName().">".$child->getName()"</a>";
    }
  }
?>

in the bookdetails.php use $_GET to retrive the parameter value. then use this value to fetch the matching data from the XML file.

I got the first part working:

<?php
$xml = simplexml_load_file("bookstore.xml");

foreach($xml->children() as $child)
  {
  echo "<a href=bookdetails.php?title=" . $child->title . ">" . $child->title . "</a> <br/>";
  }
?>

(I believe what I have done is pass a named parameter where the code you posted passed an unnamed parameter - is this correct?)

The above code lists all of the book titles and passes the title as a parameter to the bookdetails.php page.

However, now I need to get the child that has a 'title' that matches the 'title' parameter in bookdetails.php. I don't want to have to iterate through all of the children of the root element and compare their titles to the parameter - there must be a better way :)

Thanks for your help so far!

David

This works (to display the author of a particular title):

<?php
$xml = simplexml_load_file("bookstore.xml");

$res = $xml->xpath("/bookstore/book[title = 'Everyday Italian']"); 

echo $res[0]->author

?>

So I tried this:

<?php
$xml = simplexml_load_file("bookstore.xml");

$res = $xml->xpath("/bookstore/book[title = $_GET['title']]"); 

echo $res[0]->author

?>

after clicking the link which expands to

bookdetails.php?title="Everyday Italian"

but I get a 500 error.

Did I get something wrong in the GET syntax?

Thanks,

David

Member Avatar for P0lT10n

do this... assing an id to every book and then use <a href="bookdetails.php?id=".$id.""> I don't know about 500 error... maybe you done to many requests !

This works (to display the author of a particular title):

<?php
$xml = simplexml_load_file("bookstore.xml");

$res = $xml->xpath("/bookstore/book[title = 'Everyday Italian']"); 

echo $res[0]->author

?>

So I tried this:

<?php
$xml = simplexml_load_file("bookstore.xml");

$res = $xml->xpath("/bookstore/book[title = $_GET['title']]"); 

echo $res[0]->author

?>

after clicking the link which expands to

bookdetails.php?title="Everyday Italian"

but I get a 500 error.

Did I get something wrong in the GET syntax?

Thanks,

David

500 error is due to error in the PHP script.

$res = $xml->xpath("/bookstore...

in the above snipped $res variable is not an array. so it can store only one value at a time.

echo $res[0]->author

this line should be like

echo $res->author

but in your case $res will not contain direct data. data like author are contained within the <author>... tags. $xml->xpath ("....") should include "author". something like this.

$res = $xml->xpath("/bookstore/book[title = $_GET['title']]/author");

now

echo $res

should print the author name.

pardon me if the above code snipped is wrong. i'm not sure of the syntax of the parameter.. hope i've got it right.

@P0lT10n
Thanks for the suggestion. It seems like extra work though to add an id when there is already a unique field for each book.

@rje7
The error seems to happen in the xpath line (when I comment the echo line after it, the 500 error still occurs, then when I comment the xpath line itself, the error goes away).

I was told to drop the ' ' around "title" in your suggestion:
i.e.

$res = $xml->xpath("/bookstore/book[title = $_GET['title']]/author");
$res = $xml->xpath("/bookstore/book[title = $_GET[title]]/author");

(I tried it both ways, both give 500 errors)

My original line ( to get the whole object instead of just the author) also produces the 500 error:

$res = $xml->xpath("/bookstore/book[title = $_GET[title]]");

I did this:

echo $_GET['title']

to make sure the GET is working correctly and indeed it is.

Any more thoughts?

Thanks,

David

Member Avatar for P0lT10n

do this

$res = $xml->xpath("/bookstore/book[title = ".$_GET['title']."]");

I actually had to change it to :

$res = $xml->xpath("/bookstore/book[title = '".$_GET["title"]."']");

(added single quotes around the GET part of the command string).

Thanks for all of the help!

Member Avatar for P0lT10n

I actually had to change it to :

$res = $xml->xpath("/bookstore/book[title = '".$_GET["title"]."']");

(added single quotes around the GET part of the command string).

Thanks for all of the help!

what i told you !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.