We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,703 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

PHP array to HTML table

basically i am using xpath for a search query, the results display like this

Array ( [0] => SimpleXMLElement Object ( [TITLE] => Empire Burlesque [ARTIST] => Bob Dylan [COUNTRY] => USA [COMPANY] => Columbia [PRICE] => 10.90 [YEAR] => 1985 ) 
[1] => SimpleXMLElement Object ( [TITLE] => Greatest Hits [ARTIST] => Dolly Parton [COUNTRY] => USA [COMPANY] => RCA [PRICE] => 9.90 [YEAR] => 1982 ) 
[2] => SimpleXMLElement Object ( [TITLE] => Hard rains gonna fall [ARTIST] => Bob Dylan [COUNTRY] => USA [COMPANY] => Columbia [PRICE] => 5.50 [YEAR] => 1962 ) )

is it possible to get this displaying in a table so the header rows would show: Title, Artist, Country, Company, Price , Year

2
Contributors
2
Replies
2 Hours
Discussion Span
3 Months Ago
Last Updated
3
Views
Question
Answered
FakeTales
Light Poster
33 posts since Jan 2012
Reputation Points: 23
Solved Threads: 2
Skill Endorsements: 0

assumint that the variable name of your array above is $array

$index = 1;
echo '<table>';
foreach($array as $val){
    if($index==1){
        echo '<tr>';
        foreach($val as $key => $value ){
            echo '<th>'.$key.'</th>';
        }
        echo '<tr>';
    }
        echo '<tr>';
        foreach($val as $key => $value){
            echo '<td>'.$value.'</td>';
        }
        echo '</tr>';
    $index++;

}
echo '</table>';
code739
Posting Whiz in Training
208 posts since May 2012
Reputation Points: 17
Solved Threads: 28
Skill Endorsements: 5
Question Answered as of 3 Months Ago by code739

Brilliant i managed to get the actual data into rows but was still trying to figure out how to get the header data in thanks code739

FakeTales
Light Poster
33 posts since Jan 2012
Reputation Points: 23
Solved Threads: 2
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0900 seconds using 2.76MB