Hey everyone,
Currently, I am constructing an XML document in PHP that will yield the following:
<root>
<collection>
<region>1</region>
<primary>John</primary>
<collection>
<collection>
<region>1</region>
<primary>Jane</primary>
<collection>
<collection>
<region>2</region>
<primary>Jill</primary>
<collection>
<root>
However, I am looking to get the following:
<root>
<collection>
<region>1</region>
<primary>John</primary>
<primary>Jane</primary>
<collection>
<collection>
<region>2</region>
<primary>Jill</primary>
<collection>
<root>
To get the first XML doc, I am using the following PHP code:
$query = mysql_query("SELECT * FROM eventcal WHERE eventDate = '$date' ORDER BY region");
$doc = new DomDocument("1.0");
$root = $doc->createElement('data');
$root = $doc->appendChild($root);
if (@mysql_num_rows($query)) {
while ($row=@mysql_fetch_assoc($query)) {
$node = $doc->createElement('collection');
$node = $root->appendChild($node);
foreach($row as $fieldname => $fieldvalue){
$node->appendChild($doc->createElement($fieldname, $fieldvalue));
}
}
}
Is it possible for me to modify that PHP to have "primary" as a child of "region"?
Thanks!
Sorry guys, I meant primary as a sibling. You're right, the collection tag would become superfluous. Based on your comments, I think that the structure should be changed to:
<root>
<collection>
<region ID = "1">
<primary>John</primary>
<primary>Jane</primary>
</region>
<region ID = "2">
<primary>Jill</primary>
</collection>
<root>
My problem then is, how can isolate region as a parent from the MySQL resource that is returned from the query?
Thanks.