Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
PHP: Echo SimpleXMLElement Object Data Out to 3 Column HTML Table
Old 08-16-2010, 11:09 AM PHP: Echo SimpleXMLElement Object Data Out to 3 Column HTML Table
Junior Talker

Posts: 4
Name: Chris
Trades: 0
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.

Please see the attached screenshot for an example, which I explain in the next paragraph.





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>";



}
hbat66 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-16-2010, 07:50 PM Re: PHP: Echo SimpleXMLElement Object Data Out to 3 Column HTML Table
Crimson's Avatar
Skilled Talker

Posts: 56
Name: Connor
Location: United States
Trades: 0
It looks like the problem is that you have the for loop nested inside the foreach loop. Basically it's looping over each element in $output->search three times.

This code should work better:

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

// 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>";
__________________

Please login or register to view this content. Registration is FREE

I solve code problems, browser compatibility (including IE 6), Wordpress trouble, the works.
Crimson is offline
Reply With Quote
View Public Profile Visit Crimson's homepage!
 
Old 08-17-2010, 10:56 AM Re: PHP: Echo SimpleXMLElement Object Data Out to 3 Column HTML Table
Junior Talker

Posts: 4
Name: Chris
Trades: 0
This works perfectly and I see the logic with the if conditions within the overall foreach loop. Beauty in simplicity. This is a great post for anyone working with echoing out data from a SimpleXMLElement object. Hopefully, others will find it as useful as I have.

THANK YOU.
hbat66 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to PHP: Echo SimpleXMLElement Object Data Out to 3 Column HTML Table
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.46763 seconds with 12 queries