Jump to content

Recommended Posts

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

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

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!  :D

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.