1 year ago

#139894

test-img

Steven Savior

PHP Create XML CDATA Section which contains the result of a function

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)

php

xml

cdata

0 Answers

Your Answer

Accepted video resources