Solved

How can I use SimpleXML to build a table?

Posted on 2010-09-14
11
417 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

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…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

729 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