[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How can I use SimpleXML to build a table?

Posted on 2010-09-14
11
Medium Priority
?
419 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 111

Expert Comment

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

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 111

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 1000 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 111

Accepted Solution

by:
Ray Paseur earned 1000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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 i…
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.
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…

656 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