Solved

How can I use SimpleXML to build a table?

Posted on 2010-09-14
11
416 Views
Last Modified: 2013-12-12
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?
0
Comment
Question by:mrcoulson
  • 4
  • 2
  • 2
  • +2
11 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 33672071
I will post the solution for you in a second.  It uses PHP OOP notation.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 33672137

<?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);

Open in new window

0
 

Author Comment

by:mrcoulson
ID: 33672296
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
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 31

Expert Comment

by:Marco Gasi
ID: 33672332
@Ray, you are The Teacher! Sometimes I don't understand your code, but when I do, I find it wonderful. Many thanks.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 33672360
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.
0
 
LVL 4

Assisted Solution

by:trencH87
trencH87 earned 250 total points
ID: 33672396
If you have multiple records in your xml, this should work:
<?php
$xmlstring = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<needs>
	<record>
		<artist>Dream Theater 1</artist>
		<album>Majesty Demos 1</album>
	</record>
	<record>
		<artist>Dream Theater 2</artist>
		<album>Majesty Demos 2</album>
    </record>
	<record>
		<artist>Dream Theater 3</artist>
		<album>Majesty Demos 3</album>
  </record>
</needs>
XML;

// If you are using a file and not a string, use simplexml_load_file().
$xml = simplexml_load_string($xmlstring);
$p_cnt = count($xml->record);

// Headers
echo '<table id="needsTable">
	<tr>
		<td>Artist</td><td>Album</td>
	</tr>';

// Result
for($i = 0; $i < $p_cnt; $i++) {
	$param = $xml->record[$i];
	echo '<tr><td>'.$param->artist.'</td><td>'.$param->album.'</td></tr>';
}

// Close
echo '</table>';

Open in new window

0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 33672479
Sorry, I don't have any more time for this question.  You can install this script and run it.  This teaches a design pattern that will work with multiple records, which is a very different question from how to work with a singleton record.  Good luck with it, ~Ray
<?php // RAY_temp_mrcoulson.php
error_reporting(E_ALL);

// PUT YOUR DESIRED XML STRING IN HERE
$xml = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<needs>
      <record>
            <artist>Dream Theater</artist>
            <album>Majesty Demos</album>
      </record>
      <record>
            <artist>Second Artist</artist>
            <album>Second Demos</album>
      </record>
      <record>
            <artist>Third Artist</artist>
            <album>Third Demos</album>
      </record>

</needs>
XML;

// THREE HTML STRINGS
$htm_top = <<<HTMTOP
<table id="needsTable">
HTMTOP;

$htm_mid = <<<HTMMID
    <tr>
          <td>___ARTIST</td><td>___ALBUM</td>
    </tr>

HTMMID;

$htm_end = <<<HTMEND
    <tr>
          <td></td><td></td>
    </tr>
</table>
HTMEND;


// MAKE AN OBJECT FROM THE XML
$obj = SimpleXML_Load_String($xml);

// ACTIVATE THIS TO VISUALISE THE OBJECT
// echo "<pre>"; var_dump($obj);

// START THE HTML OUTPUT
echo $htm_top;

// ITERATE OVER THE OBJECT
foreach ($obj->record as $record)
{
    // PERFORM STRING SUBSTITUTIONS
    $htm = $htm_mid;
    $htm = str_replace('___ARTIST', (string)$record->artist, $htm);
    $htm = str_replace('___ALBUM',  (string)$record->album,  $htm);
    echo $htm;
}

// END THE HTML OUTPUT
echo $htm_end;

Open in new window

0
 

Author Comment

by:mrcoulson
ID: 33672545
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
<?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>

Open in new window

0
 
LVL 4

Expert Comment

by:trencH87
ID: 33672626
My code above do exactly that, but instead of using simplexml_load_string, use simplexml_load_file('needs.xml');
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34224776
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.
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question