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
Solved

How can I use SimpleXML to build a table?

Posted on 2010-09-14
11
415 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 109

Expert Comment

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

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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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 109

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 109

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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JSON  parse help 7 40
Currency in SQL? 2 30
How to make a good PHP + MySQL + JS pagination system? 3 33
Extracting store locations from Google maps or site 2 23
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

837 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