I get only one table. I need to display two tables, how to do it?
<?xml version="1.0"?>
<books>
<book>
<title>Heart of a Dog</title>
<author>Mikhail Bulgakov</author>
</book>
<book>
<title>Postmortem</title>
<author>Patricia Cornwell</author>
</book>
</books>
Maybe the wrong foreach loop construct?
<?php
$xml = simplexml_load_file('books.xml');
foreach ($xml as $x) {
$title = $x->title;
$author = $x->author;
}
?>
<table width="200" border="1" cellspacing="0" cellpadding="5">
<tr>
<td><?php echo "Title: ".$title; ?></td>
</tr><tr>
<td><?php echo "Author: ".$author; ?></td>
</tr>
</table>
According to your output Your code is correct, except just you need to put your Table inside your loop and generate table for each book.
<?php
$xml = simplexml_load_file('books.xml');
foreach ($xml as $x) {
$title = $x->title;
$author = $x->author;
?>
<table style="margin-top:10px;" width="200" border="1" cellspacing="0" cellpadding="5">
<tr>
<td><?php echo "Title: ".$title; ?></td>
</tr>
<tr>
<td><?php echo "Author: ".$author; ?></td>
</tr>
</table>
<?php
}
?>