I want to echo following xml data in html table using php,
<type>Debit</type>
<item>Item-1</item>
<price>150</price>
<type>Debit</type>
<item>Item-2</item>
<price>250</price>
<type>Debit</type>
<item>Item-3</item>
<price>100</price>
type>Debit</type>
<item>Item-4</item>
<price>200</price>
............
...so on
<type>Credit</type>
<item>Item-50</item>
<price>200</price>
<type>Credit</type>
<item>Item-51</item>
<price>300</price>
<type>Credit</type>
<item>Item-60</item>
<price>100</price>
............
...so on
here is my Html table structure before inserting data
Now if you see my xml data, i want to dynamically insert debit items under Debit List and credit items in Credit List like the following sample:
I know how to add rows dynamically using insertRow(), getElementById("id").But in this case i need to maintain two different position [1 for debit, 1 for credit] so that i can insert them in correct position.Moreover rows are dynamic, so fixed id with fixed number of rows will not work i guess.How can i do this in php.Please share your idea to solve it in a vary simple way.Please let me know for any further information.Thanks
try this solution. If you use javascript, you use two tables and use append() element row in tbody.
<!DOCTYPE html>
<html>
<body>
<?php
$myXMLData =
"
<data>
<row>
<type>Debit</type>
<item>Item-1</item>
<price>150</price>
</row>
<row>
<type>Debit</type>
<item>Item-2</item>
<price>250</price>
</row>
<row>
<type>Debit</type>
<item>Item-3</item>
<price>100</price>
</row>
<row>
<type>Debit</type>
<item>Item-4</item>
<price>200</price>
</row>
<row>
<type>Credit</type>
<item>Item-50</item>
<price>200</price>
</row>
<row>
<type>Credit</type>
<item>Item-51</item>
<price>300</price>
</row>
<row>
<type>Credit</type>
<item>Item-60</item>
<price>100</price>
</row>
</data>";
$xml=simplexml_load_string($myXMLData) or die("Error: Cannot create object");
$debit = array();
$credit = array();
$debit_total = $credit_total = 0;
foreach ($xml as $row) {
if($row->type == 'Debit') {
$debit[] = array('item'=> (string)$row->item, 'price'=> (float)$row->price);
$debit_total += (float)$row->price;
} else {
$credit[] = array('item'=> (string)$row->item, 'price'=> (float)$row->price);
$credit_total += (float)$row->price;
}
}
?>
<table>
<tr>
<td colspan="2">Debit list<td>
</tr>
<?php
foreach ($debit as $key => $value) {
echo "<tr><td>{$value['item']}</td><td>{$value['price']}</td></tr>";
}
echo "<tr><td>Total</td><td>{$debit_total}</td></tr>";
echo "<tr><td colspan='2'></td></tr>";
echo "<tr><td colspan='2'>Credit list</td></tr>";
foreach ($credit as $key => $value) {
echo "<tr><td>{$value['item']}</td><td>{$value['price']}</td></tr>";
}
echo "<tr><td>Total</td><td>{$credit_total}</td></tr>";
echo "<tr><td colspan='2'></td></tr>";
?>
</table>
</body>
</html>