Note that there are some explanatory texts on larger screens.

plurals
  1. POFor loop to parse xml to table
    primarykey
    data
    text
    <p>The following is part of an XML Response that I parse using simpleXML. I need to create a table like the example below, but i'm quite unsure of how to achieve that.</p> <pre><code>$roomCategory =' &lt;ChargeConditions&gt; &lt;ChargeCondition Type="cancellation"&gt; &lt;Condition Charge="true" ChargeAmount="2730.00" Currency="USD" FromDay="0" ToDay="0"/&gt; &lt;Condition Charge="true" ChargeAmount="390.00" Currency="USD" FromDay="1" ToDay="2"/&gt; &lt;Condition Charge="false" FromDay="3"/&gt; &lt;/ChargeCondition&gt; &lt;ChargeCondition Type="amendment"&gt; &lt;Condition Charge="false" FromDay="0"/&gt; &lt;/ChargeCondition&gt; &lt;/ChargeConditions&gt;'; </code></pre> <p>Desired Table</p> <pre><code> __________________________________________________ |____________|_____Cancelation____|___Ammendment___| | FromDay 0 | 2730.00 | No Charge | | ToDay 0 | | | |____________|____________________|________________| | FromDay 1 | 390.00 | No Charge | | ToDay 2 | | | |____________|____________________|________________| | FromDay 3 | No Charge | No Charge | | | | | |____________|____________________|________________| </code></pre> <p>I am having a trouble since the ammendment condition comes in a different node, I am trying to do something like this, but I'm not sure if I am on the right track, and how to echo everything to form the table above.</p> <p>UPDATED CODE:</p> <pre><code>$chargeConditions = $roomCategory-&gt;ChargeConditions; echo '&lt;table&gt;'; echo '&lt;tr&gt;'; echo '&lt;th&gt;&lt;/th&gt;'; foreach ($chargeConditions-&gt;ChargeCondition as $condition) { echo '&lt;th&gt;' . ucfirst($condition['Type']) . '&lt;/th&gt;'; } echo '&lt;/tr&gt;'; echo '&lt;tr&gt;'; $val = array(); $c = 0; foreach ($chargeConditions-&gt;ChargeCondition as $condition) { $val[$c][0] = $condition["Type"]; $charges = $condition-&gt;Condition; var_dump($charges); foreach($charges as $charge){ if($charge["Charge"] !== "false"){ $val[$c][1] = $charge["FromDay"]; $val[$c][2] = $charge["ToDay"]; $val[$c][3] = $charge["ChargeAmount"]; } } $c++; } for($x=0; $x &lt;= $c; $x++){ echo '&lt;tr&gt;'; echo '&lt;td&gt;From Day' . $val[$x][1] .'&lt;br&gt;To Day' . $val[$x][2] . '&lt;/td&gt;'; echo '&lt;td&gt;' . $val[$x][3] . '&lt;/td&gt;'; echo '&lt;td&gt;' . $val[$x+1][3] . '&lt;/td&gt;'; echo '&lt;/tr&gt;'; } echo '&lt;/table&gt;'; </code></pre> <p>The code above produces a table similar to this one:</p> <pre><code> __________________________________________________ |____________|_____Cancelation____|___Ammendment___| | FromDay 3 | | | | ToDay | | | |____________|____________________|________________| | FromDay 0 | | | | ToDay | | | |____________|____________________|________________| | FromDay | | | | ToDay | | | |____________|____________________|________________| </code></pre> <p>var_dump($charges) gives the following output:</p> <pre><code>object(SimpleXMLElement)#455 (1) { ["@attributes"]=&gt; array(5) { ["Charge"]=&gt; string(4) "true" ["ChargeAmount"]=&gt; string(7) "2730.00" ["Currency"]=&gt; string(3) "USD" ["FromDay"]=&gt; string(1) "0" ["ToDay"]=&gt; string(1) "0" } } object(SimpleXMLElement)#504 (1) { ["@attributes"]=&gt; array(2) { ["Charge"]=&gt; string(5) "false" ["FromDay"]=&gt; string(1) "0" } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload