In: Computer Science
Displaying Content of an XML File Using PHP Script or Code
In this week, you are going to write the PHP script or code to read an XML file and display its content on the screen. The file you will modify is the Products page, which you created in Week 1 Project. Given the following data in XML format, modify the Products page to read and display the information in the Products page:
<Product>
<Item>
<Name>T-Shirt</Name>
<Price>12.5</Price>
</Item>
<Item>
<Name>Pants</Name>
<Price>45</Price>
</Item>
<Item>
<Name>Hat</Name>
<Price>10.5</Price>
</Item>
</Product>
Your end page result needs to show the items, their cost (price) in a table format. Use both PHP script as well as HTML tags to create the dynamic table from the given XML file.
<?php
$xml = simplexml_load_file('employees.xml');
echo '<h2>Product Listing</h2>';
$list = $xml->Item;
echo '<table border = 1px solid black>';
echo '
<tr>
<th>Name</th>
<th>Price</th>
</tr>';
for ($i = 0; $i < count($list); $i++) {
echo '<tr>';
echo '<td>' . $list[$i]->Name . '</td>';
echo '<td>' . $list[$i]->Price . '</td>';
echo '</tr>';
}
echo '</table>';
?>