Reputation: 153
Currently I have a code that extracts information from an XML sheet, the problem is that I can not show the information as I want.
demo.xml
<?xml version="1.0" encoding="UTF-8"?>
<ProductData version="2.0">
<Product>
<code>111111</code>
<name>The Name</name>
<brand>The Brand</brand>
<quantity>
<store1>5</store1>
<store2>4</store2>
</quantity>
</Product>
<Product>
<code>222222</code>
<name>The Name</name>
<brand>The Brand</brand>
<quantity>
<store1>6</store1>
<store2>4</store2>
</quantity>
</Product>
</ProductData>
index.php
<?php
if (file_exists('demo.xml')) {
$codes = [];
$store1 = [];
$xml = simplexml_load_file('demo.xml');
foreach($xml->Product as $i => $product) {
$codes[] = $product->code;
$store1[] = $product->quantity->store1;
}
echo implode($codes,',');
echo "<br>";
echo implode($store1,',');
} else { exit('Error en archivo!'); }
?>
Result:
111111,222222
5,6
What I need:
╔═════════════╦══════════╦══════════╗
║ Code ║ Store 1 ║ Store 2 ║
╠═════════════╬══════════╬══════════╣
║111111 ║ 5 ║ 4 ║
║222222 ║ 6 ║ 4 ║
╚═════════════╩══════════╩══════════╝
It could be shown in a table as shown above or simply in values separated by a comma to be processed later ...
111111,5,4
222222,6,4
...
Can anybody help me? Thank you!
Upvotes: 0
Views: 186
Reputation: 19780
You could store each "Product" data in an array.
$xml = simplexml_load_file('demo.xml');
$codes = [];
foreach($xml->Product as $i => $product) {
$data = [];
$data[] = (string)$product->code;
$data[] = (string)$product->quantity->store1;
$data[] = (string)$product->quantity->store2;
$codes[] = $data;
}
foreach ($codes as $code) {
echo implode(',', $code) . '<br>';
}
Outputs:
111111,5,4
222222,6,4
if you want a table:
echo "<pre>";
echo "╔═════════════╦══════════╦══════════╗"."<br>";
echo "║ Code ║ Store 1 ║ Store 2 ║"."<br>";
echo "╠═════════════╬══════════╬══════════╣"."<br>";
foreach ($codes as $code) {
echo "║";
echo sprintf("%12s ║", $code[0]);
echo sprintf("%9s ║", $code[1]);
echo sprintf("%9s ║", $code[2]);
echo "<br>";
}
echo "╚═════════════╩══════════╩══════════╝"."<br>";
echo "</pre>";
Outputs:
╔═════════════╦══════════╦══════════╗
║ Code ║ Store 1 ║ Store 2 ║
╠═════════════╬══════════╬══════════╣
║ 111111 ║ 5 ║ 4 ║
║ 222222 ║ 6 ║ 4 ║
╚═════════════╩══════════╩══════════╝
Upvotes: 1
Reputation: 1
<?php
if (file_exists('demo.xml')) {
$xml = simplexml_load_file('demo.xml');
$str = '';
foreach($xml->Product as $i => $product) {
$str .= "<tr><td>{$product->code}</td><td>{$product->quantity->store1}</td><td>{$product->quantity->store2}</td></tr>\n";
}
echo <<<END
<table style="width:100%">
{$str}
</table>
END;
} else { exit('Error en archivo!'); }
?>
Give you:
<table style="width:100%">
<tr><td>111111</td><td>5</td><td>4</td></tr>
<tr><td>222222</td><td>6</td><td>4</td></tr>
</table>
Upvotes: 0
Reputation: 57121
If you change the way you build the data up in the loop, so store all the data from the line into one element of an array instead of different arrays. You can then implode() this alternative array one line at a time...
if (file_exists('demo.xml')) {
$data = [];
$xml = simplexml_load_file('demo.xml');
foreach($xml->Product as $i => $product) {
$data[] = [(string)$product->code
,(string)$product->quantity->store1
,(string)$product->quantity->store2
];
}
foreach ( $data as $line ) {
echo implode(",",$line).PHP_EOL;
}
} else { exit('Error en archivo!'); }
Outputs...
111111,5,4
222222,6,4
Upvotes: 2