Jump to content

[solved] problem assigning GET into a variable


newrehmi

Recommended Posts

hello there,I have a problem of assigning GET array into a variable, but, without an assignment and I use the array instead, it worked. Below is the example:The example i tried below is working:

<?php$fopen = fopen('comment.xml','r+');while(!feof($fopen))	{	$comment = $comment.fgets($fopen);	}$thecomment = str_replace('</commentdata>','',$comment);$newcomment = $thecomment.'<comment><user>'.$_GET['user'].'</user><content>'.$_GET['comment'].'</content><date>'.$_GET['date'].'</date></comment></commentdata>';fwrite(fopen('comment.xml','w'),$newcomment);fclose($fopen);?>

But when I assign it into a variable first, my xml became scattered with the value of the array around it. Below is the example, when I assign:

<?php$user = $_GET['user'];$comment = $_GET['comment'];$date = $_GET['date'];$fopen = fopen('comment.xml','r+');while(!feof($fopen))	{	$comment = $comment.fgets($fopen);	}$thecomment = str_replace('</commentdata>','',$comment);$newcomment = $thecomment.'<comment><user>'.$user.'</user><content>'.$comment.'</content><date>'.$date.'</date></comment></commentdata>';fwrite(fopen('comment.xml','w'),$newcomment);fclose($fopen);?>

Any explanation, why this things happen? Thanks a lot guys!

Link to comment
Share on other sites

my xml became scattered with the value of the array around it
I have no idea what that means. Maybe you can show us the difference between the two strings? (Just the part of the file you are changing.FWIW, there are better ways of reading and writing file data.You might also prefer to edit your XML as a DOM document rather than a string. That's what I usually do.
Link to comment
Share on other sites

thanks for answering.below is what i meant by scattered, after I submit a new comment:Before submit:

<?xml version="1.0" encoding="iso-8859-1"?><commentdata></commentdata>

After submit:

test<?xml version="1.0" encoding="iso-8859-1"?><commentdata><comment><user>test</user><content>test<?xml version="1.0" encoding="iso-8859-1"?><commentdata></commentdata></content><date>6/8/2011</date></comment></commentdata>

it should be working like this (after indent):

<?xml version="1.0" encoding="iso-8859-1"?><commentdata>  <comment>	<user>test</user>	<content>test</content>	<date>6/8/2011</date>  </comment></commentdata>

You might also prefer to edit your XML as a DOM document rather than a string. That's what I usually do.
I searched before about DOM serialization, but haven't found a clue about it yet. A simple explanation about what function does it use (like js method and etc), might help.Thanks a lot for your time!
Link to comment
Share on other sites

personally, I second DD's suggestion to use DOMDocument. Here's a decent tutorial to help get you more familiar with constructing XML.http://css.dzone.com/news/creating-xml-documents-phpand of course the obligatory reference to w3schoolshttp://www.w3schools.com/dom/dom_document.aspedit: also, in your second code snippet, you are overwriting the $_GET value of $comment when you go into the loop.

Link to comment
Share on other sites

personally, I second DD's suggestion to use DOMDocument. Here's a decent tutorial to help get you more familiar with constructing XML.http://css.dzone.com/news/creating-xml-documents-phpand of course the obligatory reference to w3schoolshttp://www.w3schools.com/dom/dom_document.aspedit: also, in your second code snippet, you are overwriting the $_GET value of $comment when you go into the loop.
thanks for the answer. yeah. i learned constructing DOM using javascript before (creating element, attribute, node and etc), but I wonder if there's a connection or relation with the tutorial you posted.edit:at the end of the tutorial, did this statement created a new .xml or just echo the xml structure, so i just use responseXML in ajax.
echo $xmlDoc->saveXML();

Thanks.

Link to comment
Share on other sites

urgh, guys, sorry for my mistake!. I mistakenly used the same variable with $comment. Duh... am so stupid... =_+ hahaI corrected the variable now.

<?php$user = $_GET['user'];$content = $_GET['comment'];$date = $_GET['date'];$fopen = fopen('comment.xml','r+');while(!feof($fopen))	{	$comment = $comment.fgets($fopen);	}	$thecomment = str_replace('</commentdata>','',$comment);$newcomment = $thecomment.'<comment><user>'.$user.'</user><content>'.$content.'</content><date>'.$date.'</date></comment></commentdata>';fwrite(fopen('comment.xml','w'),$newcomment);fclose($fopen);?>

Thanks a lot for answering, btw!!!!

Link to comment
Share on other sites

urgh, guys, sorry for my mistake!. I mistakenly used the same variable with $comment. Duh... am so stupid... =_+ haha
Yup...
edit: also, in your second code snippet, you are overwriting the $_GET value of $comment when you go into the loop.
Link to comment
Share on other sites

Archived

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

×
×
  • Create New...