fagnonk Posted May 28, 2009 Share Posted May 28, 2009 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 More sharing options...
JakeTheSnake3.0 Posted May 28, 2009 Share Posted May 28, 2009 $simp->category[n]->attributes()->link += 1; My reference may be off, but you get the nomenclature. PHP doesn't complain that 0 or 1 is as a string. Link to comment https://forums.phpfreaks.com/topic/160048-php-simplexml-and-counter/#findComment-844567 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.