Or1g1naL Posted April 1, 2010 Share Posted April 1, 2010 and my first post! I have lurked phpfreaks forum many times and copy little bits of code. The information on this site is a lot of help and the people here are great. Lately my code is looking messy because I dont' know how to create my own functions! :'( Here is the code I want to turn into a function. All this does is update a value in a xml file with the the attribute id 4bafdebc54aac. $contact_id = "4bafdebc54aac"; $xmlfile = "temp.xml"; $dom = new DOMDocument(); $dom->load($xmlfile); $contacts = $dom->getElementsByTagName("contact"); foreach($contacts as $contact){ if($contact->getAttribute('id') == $contact_id){ $contact->getElementsByTagName('firstname')->item(0)->nodeValue = "Jim"; } } $dom->save($xmlfile); This is my attempt to create a function from that, but it isn't working. The code above this works perfectly but I can't put it into a function. function updateNode($node, $nodeID, $childNode, $cnodeValue, $xmlFile){ $doc = new DOMDocument(); $doc->load($xmlFile); $node = $dom->getElementsByTagName($node); foreach($node as $nodes){ if($nodes->getAttribute('id') == $nodeID){ $nodes->getElementsByTagName($childNode)->item(0)->nodeValue = $cnodeValue; } } } updateNode("contact", "4bafdebc54aac", "firstname", "Peter", "temp.xml"); Everything looks ok to me but it does not work. Could anybody give me any tips? I would really be greatful. Link to comment https://forums.phpfreaks.com/topic/197186-my-first-function/ Share on other sites More sharing options...
mikesta707 Posted April 1, 2010 Share Posted April 1, 2010 what is happening? is there an error? code not doing what you want? a little more info would be needed to give a concrete answer. the function looks ok to me, but seeing the function call itself would probably help also. edit: oh, didn't even see the scroll bar in second code snippet... yeah thorpe is right. Link to comment https://forums.phpfreaks.com/topic/197186-my-first-function/#findComment-1035037 Share on other sites More sharing options...
jcbones Posted April 1, 2010 Share Posted April 1, 2010 $node = $dom->getElementsByTagName($node); //should be $node = $doc->getElementsByTagName($node); Link to comment https://forums.phpfreaks.com/topic/197186-my-first-function/#findComment-1035039 Share on other sites More sharing options...
trq Posted April 1, 2010 Share Posted April 1, 2010 Your missing.... $dom->save($xmlfile); Link to comment https://forums.phpfreaks.com/topic/197186-my-first-function/#findComment-1035041 Share on other sites More sharing options...
jcbones Posted April 1, 2010 Share Posted April 1, 2010 Your missing.... $dom->save($xmlfile); //Should be $doc->save($xmlfile); Link to comment https://forums.phpfreaks.com/topic/197186-my-first-function/#findComment-1035042 Share on other sites More sharing options...
Or1g1naL Posted April 1, 2010 Author Share Posted April 1, 2010 function updateNode($node, $nodeID, $childNode, $cnodeValue, $xmlFile){ $dom = new DOMDocument(); $dom->load($xmlFile); $node = $dom->getElementsByTagName($node); foreach($node as $nodes){ if($nodes->getAttribute('id') == $nodeID){ $nodes->getElementsByTagName($childNode)->item(0)->nodeValue = $cnodeValue; } } $dom->save($xmlFile); } updateNode("contact", "4bafdebc54aac", "firstname", "Peter", "temp.xml"); And it works! Looks like it was my own confidence in my self that was holding me back. I was sure I was creating the function wrong. Thanks guys Link to comment https://forums.phpfreaks.com/topic/197186-my-first-function/#findComment-1035050 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.