05.05.2013 Views

Programming PHP

Programming PHP

Programming PHP

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Example 11-9. books.xml file (continued)<br />

1-56592-610-2<br />

A great book!<br />

<br />

<br />

<strong>PHP</strong> Pocket Reference<br />

<br />

Rasmus Lerdorf<br />

<br />

1-56592-769-9<br />

It really does fit in your pocket<br />

<br />

<br />

Perl Cookbook<br />

<br />

Tom Christiansen<br />

Nathan Torkington<br />

<br />

1-56592-243-3<br />

Hundreds of useful techniques, most just as applicable to<br />

<strong>PHP</strong> as to Perl<br />

<br />

<br />

<br />

The <strong>PHP</strong> application parses the file and presents the user with a list of books, showing<br />

just the titles and authors. This menu is shown in Figure 11-1. The titles are links<br />

to a page showing the complete information for a book. A page of detailed information<br />

for <strong>Programming</strong> <strong>PHP</strong> is shown in Figure 11-2.<br />

Figure 11-1. Book menu<br />

We define a class, BookList, whose constructor parses the XML file and builds a list<br />

of records. There are two methods on a BookList that generate output from that list<br />

of records. The show_menu( ) method generates the book menu, and the show_book( )<br />

method displays detailed information on a particular book.<br />

Parsing the file involves keeping track of the record, which element we’re in, and<br />

which elements correspond to records (book) and fields (title, author, isbn, and<br />

274 | Chapter 11: XML<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2002 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!