I am trying to get data from database and write it to xml file using DOM.
$doc = new DomDocument('1.0', 'UTF-8');
$doc->preserveWhiteSpace = false;
$doc->formatOutput = true;
$root = $doc->createElement('root');
$doc->appendChild($root);
$blocks = $doc->createElement('blocks');
$root->appendChild($blocks);
while($result_array = mysqli_fetch_assoc($result))
{
$cms_block = $doc->createElement('cms_block');
foreach($result_array as $fieldname => $fieldvalue)
{
/*This is part that is not working*/
$key = $doc->createElement($fieldname);
$cms_block->appendChild($key);
$value = $doc->createTextNode($fieldvalue);
$key->appendChild($value);
}
$stores = $doc->createElement('stores');
$cms_block->appendChild($stores);
$item = $doc->createElement('item');
$stores->appendChild($item);
$itemvalue = $doc->createTextNode('0');
$item->appendChild($itemvalue);
$blocks->appendChild($cms_block);
}
The above code works and mostly does what I need it to do but it fails within foreach loop and the elements and value that I am trying to add within foreach does not work.
Example of $result_array = mysqli_fetch_assoc($result)
Array ( [title] => Footer Links [identifier] => footer_links [is_active] => 1 )
Array ( [title] => Footer Links [identifier] => footer_links [is_active] => 1 )
It would really help me if someone can tell me what I am doing wrong within foreach loop which is not adding the data that I am getting from database to the xml file please.