Render XML output in HTML table using PHP -
i have xml output written in php looks below
<twitch_xml> <twitch> <bird id="1"> <species>willow ptarmigan</species> <age>2</age> <sex>male</sex> <location>london</location> <image> http://www.kidzone.ws/images-changed/birds/willow_ptarmigan.jpg </image> <user>chamith</user> </bird> <bird id="5"> <species>grey partridge</species> <age>2</age> <sex>male</sex> <location>london</location> <image> https://www.rspb.org.uk/images/greypartridge_tcm9-17615.jpg?width=530&crop=(196,364,1006,820) </image> <user>raveen</user> </bird> </twitch> </twitch_xml>
i want put output inside html table. please note i'm not using external xml document. retrieve data mysql , print directly xml using savexml()
method
to render data inside html table tried use following code:
$twitcdm->savexml(); foreach($twitcdm->twitch_xml->twitch $xmv){ echo "<tr>"; echo "<td>{$xmv->id}</td>"; echo "<td>{$xmv->species}</td>"; echo "<td>{$xmv->age}</td>"; echo "<td>{$xmv->sex}</td>"; echo "<td>{$xmv->location}</td>"; echo "<td>{$xmv->image}</td>"; echo "<td>{$xmv->user}</td>"; echo "<br />"; echo "</tr>"; }
but when run gives me error:
warning: invalid argument supplied foreach() in c:\winginx\home\localhost\public_html\chamith\twitch_id.php on line 67
this whole php code used generate xml output
$id = $_get["twitch_id"]; $command = "select id,species,age,sex,location,image,username chamith_twitch.twitch id = $id"; $dboutput = $mysqlconnection->query($command); $mysqlconnection->close(); $no_of_raws = $dboutput->num_rows; if ($no_of_raws > 0) { while ($line = $dboutput->fetch_array()) { $lines[] = $line; } $dboutput->close(); $twitcdm = new domdocument(); // $twitcdm->formatoutput = true; $twitcdm->appendchild($twitcdm->createelement('twitch_xml')); $fetch_xml = $twitcdm->documentelement; $xmldoc = $twitcdm->createelement('twitch'); foreach ($lines $line) { $dbpkey = $line['id']; $twitch = $twitcdm->createelement('bird'); $twitch->setattribute("id", $line['id']); $species = $twitcdm->createelement('species'); $age = $twitcdm->createelement('age'); $sex = $twitcdm->createelement('sex'); $address = $twitcdm->createelement('location'); $twitch_photo = $twitcdm->createelement('image'); $record_owner = $twitcdm->createelement('user'); $header = $twitcdm->createtextnode($line['species']); $species->appendchild($header); $twitch->appendchild($species); $header = $twitcdm->createtextnode($line['age']); $age->appendchild($header); $twitch->appendchild($age); $header = $twitcdm->createtextnode($line['sex']); $sex->appendchild($header); $twitch->appendchild($sex); $header = $twitcdm->createtextnode($line['location']); $address->appendchild($header); $twitch->appendchild($address); $header = $twitcdm->createtextnode($line['image']); $twitch_photo->appendchild($header); $twitch->appendchild($twitch_photo); $header = $twitcdm->createtextnode($line['username']); $record_owner->appendchild($header); $twitch->appendchild($record_owner); $xmldoc->appendchild($twitch); } $fetch_xml->appendchild($xmldoc); // header('content-type: text/xml'); $twitcdm->savexml();
i want populate these xml data inside table, how can fix ?
several issues here:
1. invalid xml
see http://www.xmlvalidation.com , test xml. turns out line
https://www.rspb.org.uk/images/greypartridge_tcm9-17615.jpg?width=530&crop=(196,364,1006,820)
is causing problems because contains invalid characters. see invalid characters in xml on site. may want check out how make strings xml"safe"? solutions.
2. wrong referencing of xml object
foreach ($twitcdm->twitch_xml->twitch $xmv) { // etc. }
$twitcdm
represents root node<twitch_xml>
- you forgot
<bird>
node in above xml path
do instead:
foreach ($twitcdm->twitch->bird $xmv) { echo $xmv->image . php_eol; // etc. }
see working example using cdata
include invalid characters in xml , correct xml path in foreach
: https://eval.in/321334
(using simplexml instead of dom, no big deal though)
notes:
since create xml yourself, can include handling of invalid characters there. moreover, i'd advise use consistent uppercase/lowercase in naming nodes, e.g. <twitch>
<bird>
.
Comments
Post a Comment