This one has been driving me crazy and i'm sure its pretty simple. I have to create an xml document that looks like this (note xml modules are not an option - i have to hard code the tags)
As you can see, views may have many parameters, and my root element <views> may have many views. This data is coming from a database that returns a table like this:<views> <view id="123456" name="all"> <parameter id="123">Param 1</parameter> <parameter id="124">Param 2</parameter> </view> <view id="123457" name="detail"> <parameter id="125">Param 3</parameter> <parameter id="126">Param 4</parameter> <parameter id="127">Param 5</parameter> <parameter id="128">Param 6</parameter> </view> </views>
VIEW_ID VIEW_NAME PARAM_ID PARAM_NAME ---------------------------------------------- 123456 all 123 Param 1 123456 all 124 Param 2 123457 detail 125 Param 3 123457 detail 126 Param 4 123457 detail 127 Param 5 123457 detail 128 Param 6
I have tried lots of variations on while loops, keeping track of ids etc, but cannot quite seem to grasp it. Some code is below that i have tried
There are a few confusing points for me (where would i put the closing </view> tags for example?)$xml = "<views>\n"; while ($row = $sth->fetchrow_arrayref){ if (!defined($id{$row->[0]})){ $xml .= $xml . "<view id=\"$row->[0]\" name=\"".$row->[1]."\"> +\n"; $id{$row->[0]} = $row->[0]; if (!defined($param{$row->[3]})){ $xml .= $xml . "<parameter id=\"$row->[3]\">".$row->[4]."< +/parameter>\n"; $id{$row->[3]} = $row->[3]; } }elsif(defined($id{$row->[0]})){ if (!defined($param{$row->[3]})){ $xml .= $xml . "<parameter id=\"$row->[3]\">".$row->[4]."< +/parameter>\n"; } } }
If anyone has any ideas (if using a module, i will try to get it installed as hard as i can), i would be grateful!
Thanks
Joe
-----
Eschew obfuscation, espouse elucidation!
In reply to Creating simple XML from a DB query by joec_
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |