0

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>&nbsp;<span class="magdate">' . $search['issue_name'] . '</span><br /><br /><span class="resultlabel">Query Match:&nbsp;</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>";
							
							

}
2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by rdc
0

I figure out the answer... Here's the code for anyone who may find it useful:

// 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++;
}

// Set # of columns
$cols = 3;

// Open the container table
echo "<table>";

$currentColumn = 0;

// Print out the data in 3 columns
foreach($output->search as $search) {

    // If this is the first column, open container row
    if(0 == $currentColumn){
        echo "<tr>";
    }

    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>&nbsp;<span class="magdate">' . $search['issue_name'] . '</span><br /><br /><span class="resultlabel">Query Match:&nbsp;</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 />';

    // Increment the column counter
    $currentColumn++;

    // If this is this the last column, close out the container row
    // and reset the column counter to zero
    if($currentColumn >= $cols){
        echo "</tr>";
        $currentColumn = 0;
    }
}

// Close out the container table
echo "</table>";
0

Thanks hbat66. I'll start with your code and I hope be able to answer my own needs. It was very good of you to post the solution once you figured it out.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.