I would like my CDATA section display the result of a function that generates a text from an sql query.
Create sql query and call function createXMLfile:
$statement = $connection->query("SELECT * FROM events ORDER BY ID DESC LIMIT 1");
$statement->execute();
$eventArray = array();
while($row = $statement->fetch(PDO::FETCH_ASSOC))
{
array_push($eventArray, $row);
}
createXMLfile($eventArray);
Function that generate text from my sql query:
<?php
function write_records($eventArray) {
for($i=0; $i<count($eventArray); $i++){
$name = $eventArray[$i]['event_name'];
$city = $eventArray[$i]['event_city'];
?>A new event <?php echo $eventArray[$i]['event_name'];?> is coming in <?php $eventArray[$i]['event_city'];?>.<?php
}
return $eventArray;
}
?>
Function that create XML file (i use implode for convert array to a string):
function createXMLfile($eventArray){
$filePath = 'event.xml';
$content = implode(' ', write_records($eventArray));
$dom = new DOMDocument('1.0', 'utf-8');
$root = $dom->createElement('INCIDENTS');
for($i=0; $i<count($eventArray); $i++){
$eventName = $eventArray[$i]['event_name'];
$eventCity = $eventArray[$i]['event_city'];
$eventXml = $dom->createElement('INCIDENT');
$name = $dom->createElement('EVENT', $eventName);
$eventXml->appendChild($name);
$city = $dom->createElement('CITY', $eventCity);
$eventXml->appendChild($city);
$eventXml->appendChild(new DOMElement('COMMENTAIRE'))->appendChild(new DOMCdataSection($content));
$root->appendChild($eventXml);
}
$dom->appendChild($root);
$dom->save($filePath);
}
XML file is generated but CDATA section display only this :
<![CDATA[Array]]>
Please can you tell me what's wrong ? Thanks for your help.
EDIT:
function createXMLfile($eventArray){
$filePath = 'event.xml';
$content = implode(' ', write_records($eventArray[0]));
$dom = new DOMDocument('1.0', 'utf-8');
$root = $dom->createElement('INCIDENTS');
for($i=0; $i<count($eventArray); $i++){
$eventName = $eventArray[$i]['event_name'];
$eventCity = $eventArray[$i]['event_city'];
$eventXml = $dom->createElement('INCIDENT');
$name = $dom->createElement('EVENT', $eventName);
$eventXml->appendChild($name);
$city = $dom->createElement('CITY', $eventCity);
$eventXml->appendChild($city);
$eventXml->appendChild(new DOMElement('COMMENTAIRE'))->appendChild(new DOMCdataSection($content));
$root->appendChild($eventXml);
}
$dom->appendChild($root);
$dom->save($filePath);
}
Now sql values are displayed in CDATA section but not the generated text from function write_records($eventArray).
<![CDATA[concert Miami]]>
And i would like :
<![CDATA[A new concert is coming in Miami]]>
print_r($eventArray); give me same result :
$statement = $connection->query("SELECT * FROM events ORDER BY ID DESC LIMIT 1");
$statement->execute();
$eventArray = array();
while($row = $statement->fetch(PDO::FETCH_ASSOC))
{
array_push($eventArray, $row);
}
print_r($eventArray);
createXMLfile($eventArray);
Apache Log say PHP Notice: Undefined offset :0 from variables values in function write_records($eventArray)
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.