Jump to content

PHP SimpleXML and Counter


fagnonk

Recommended Posts

I am using simpleXML to add data to an existing XML file. The script is working properly, however I need to also include a dynamic counter that increases by one integer every time the user adds a new entry to the XML file.

 

For example, my XML file looks like this:

 
<content>
    <category link="0">
        <title>Animals</title>
        <xml>animals.xml</xml>
    </category>
    <category link="1">
        <title>Bears</title>
        <xml>Bears.xml</xml>
    </category>
</content>

 

If I were to add another line to this xml file, the 'link' attribute of category tag would need to increase to 2.

 

The PHP code I am currently using to add data to the XML file looks like this:

$str = $_SESSION['name']; 
$file = PREG_REPLACE("/[^0-9a-zA-Z]/i", '', $str);

if (isset($str) && !empty($str)) {

$simp = simplexml_load_file('galleries.xml');
$node = $simp->addChild('category');
$node->addChild('title', $str);
$node->addChild('xml', 'uploadify/xml/'.$file.'.xml');
$s = simplexml_import_dom($simp);
$s->saveXML('galleries.xml');
}

 

I can't figure out how to implement a counter that recognizes the current count of the imported XML file. Any suggestions?

Link to comment
https://forums.phpfreaks.com/topic/160048-php-simplexml-and-counter/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.