mrcoulson
asked on
How can I use SimpleXML to build a table?
Because I'm short on time and trying to multitask, I'm about to ask the experts to do my work for me. Don't judge!
Given the following XML structure:
<?xml version="1.0" encoding="utf-8"?>
<needs>
<record>
<artist>Dream Theater</artist>
<album>Majesty Demos</album>
</record>
</needs>
I need to fill the following HTML table structure:
<table id="needsTable">
<tr>
<td>Artist</td><td>Album</ td>
</tr>
<tr>
<td></td><td></td>
</tr>
</table>
I have stumbled upon SimpleXML, but I'm buried under projects and can't seem to figure it out. Ideas?
Given the following XML structure:
<?xml version="1.0" encoding="utf-8"?>
<needs>
<record>
<artist>Dream Theater</artist>
<album>Majesty Demos</album>
</record>
</needs>
I need to fill the following HTML table structure:
<table id="needsTable">
<tr>
<td>Artist</td><td>Album</
</tr>
<tr>
<td></td><td></td>
</tr>
</table>
I have stumbled upon SimpleXML, but I'm buried under projects and can't seem to figure it out. Ideas?
I will post the solution for you in a second. It uses PHP OOP notation.
<?php // RAY_temp_mrcoulson.php
error_reporting(E_ALL);
// THE XML STRING
$xml = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<needs>
<record>
<artist>Dream Theater</artist>
<album>Majesty Demos</album>
</record>
</needs>
XML;
// I need to fill the following HTML table structure:
$htm = <<<HTM
<table id="needsTable">
<tr>
<td>___ARTIST</td><td>___ALBUM</td>
</tr>
<tr>
<td></td><td></td>
</tr>
</table>
HTM;
// MAKE AN OBJECT FROM THE XML
$obj = SimpleXML_Load_String($xml);
// ACTIVATE THIS TO VISUALISE THE OBJECT
// echo "<pre>"; var_dump($obj);
// PERFORM STRING SUBSTITUTIONS
$htm = str_replace('___ARTIST', (string)$obj->record->artist, $htm);
$htm = str_replace('___ALBUM', (string)$obj->record->album, $htm);
// SHOW THE WORK PRODUCT
echo htmlentities($htm);
ASKER
I don't mean to be thick, but maybe I am. When I use your code, I get the result at http://jcoulson.com/needs.php instead of something that resembles the table at http://jcoulson.com/needs.html. Am I doing something wrong?
Jeremy
Jeremy
@Ray, you are The Teacher! Sometimes I don't understand your code, but when I do, I find it wonderful. Many thanks.
This is a demonstration script, and it works perfectly given what we had to work with here in the post at EE. We are experts, but not mind readers.
Please post the ACTUAL XML STRING you want to use for the test.
Please post the ACTUAL XML STRING you want to use for the test.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sorry, Ray. I didn't mean to be ambiguous. The XML file is actually separate from the PHP. There's a needs.php and needs.xml. The goal is to be able to edit just the XML file without ever having to touch the PHP.
Below is an example XML with more than one record.
Jeremy
Below is an example XML with more than one record.
Jeremy
<?xml version="1.0" encoding="utf-8"?>
<needs>
<record>
<artist>Dream Theater</artist>
<album>Majesty Demos</album>
</record>
<record>
<artist>For Today</artist>
<album>Ekklesia</album>
</record>
<record>
<artist>For Today</artist>
<album>Portraits</album>
</record>
<record>
<artist>Place of Skulls</artist>
<album>A Dog Returns</album>
</record>
<record>
<artist>Overkill</artist>
<album>Ironbound</album>
</record>
</needs>
My code above do exactly that, but instead of using simplexml_load_string, use simplexml_load_file('needs .xml');
This question has been classified as abandoned and is being closed as part of the Cleanup Program. See my comment at the end of the question for more details.