Subject
PHP: Echo SimpleXMLElement Object Data Out to 3 Column HTML Table
Post
Hello everyone. I am working with Simple XML Element object data and PHP code. I want to echo out that data into a 3 column HTML table and have run into some trouble.
I am currently able to get a 3-column HTML table to echo out of my code and echo out the Simple XML Element data, BUT each column in a single row has the same data in it.
Here is a screenshot for an example, which I explain in the next paragraph:
http://rss.modernluxury.com/images/repdata.jpg
My current unsatisfactory result is multiple rows with 3 columns, but with duplicative data in each column of a single row. The data does change in the second and subsequent rows, but each of those rows has the same problem of displaying the same data as the previous column in the row.
I’ve copied a very commented version of my code out at the bottom of my post, so it should be easy to see what’s going.
Can someone help me figure out how to get each column of each row to echo out unique data returned by the Simple XMLElement object? Is this possible? I am able to do this with MySQL array data, but not with this Simple XMLElement object data.
Thank you!!!
Code
// Build a query URL with user-inputted data
$queryurl = 'http://digital.modernluxury.com/fs.php?' . '&id_issue=' . $_GET['id_issue'] . '&keyword=' . $_GET['keyword'] . '&searchin=' . $_GET['searchin'] . '&mode=3';
// Send the query URL to API
$searchresult = file_get_contents($queryurl);
// Turn the resulting XML output into a Simple XML Element object accessible by a variable
$output = new SimpleXMLElement($searchresult);
// Create a counter and set it to 0
$counter = 0;
// Echo out the results loop
foreach($output->search as $search) {
$counter++;
}
foreach($output->search as $search) {
// Set # of columns
$cols=3;
// Open container table and row
echo "<table>";
echo "<tr>";
// Print out the data in 3 columns
for($i=1;$i<=$cols;$i++) {
echo '<td><div id="searchresultcontainer" onclick="window.location.href=\'' . $search['url_mag'] . '\'">' . '<div class="searchresultcol1">' . '<img src="' . $search['pic_url'] . '" class="searchimage" />' . '</div>' . '<div class="searchresultcol2">' . '<span class="magcode">' . $search['publication_name'] . '</span> <span class="magdate">' . $search['issue_name'] . '</span><br /><br /><span class="resultlabel">Query Match: </span><span class="querymatch">' . $search->word . '</span><br /><span class="searchcopy">' . substr($search,0,85) . '...</span><p class="fullresult">>> Click for full result</p></div>' . '<br class="clearfloat" /></div></td><br />';
}
// Close out the container table and row
echo "</tr>";
echo "</table>";
}