I have XML output written in PHP which looks like 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 to put this output inside a HTML table. Please note that I'm not using external XML document. I retrieve data from MySQL and print directly to XML using savexml()
method
To render data inside the HTML table I tried to use the following code:
$twitcdm->saveXML();
foreach($twitcdm->Twitch_XML->twitch as $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 I run this it gives me an error:
Warning: Invalid argument supplied for foreach() in
C:\Winginx\home\localhost\public_html\chamith\twitch_id.php on line 67
This is the whole PHP code that I used to generate XML output
$id = $_GET["twitch_id"];
$command = "SELECT id,species,age,sex,location,image,username FROM chamith_twitch.twitch WHERE 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 as $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 to populate these XML data inside table, how can I fix this ?