Solved

How can I use SimpleXML to build a table?

Posted on 2010-09-14
11
412 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 108

Expert Comment

by:Ray Paseur
Comment Utility
I will post the solution for you in a second.  It uses PHP OOP notation.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility

<?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
Comment Utility
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
 
LVL 30

Expert Comment

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

Expert Comment

by:Ray Paseur
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 4

Assisted Solution

by:trencH87
trencH87 earned 250 total points
Comment Utility
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 108

Accepted Solution

by:
Ray Paseur earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
My code above do exactly that, but instead of using simplexml_load_string, use simplexml_load_file('needs.xml');
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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…
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now