Jump to content

[solved] problem assigning GET into a variable


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!

Edited by newrehmi
Link to post
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 post
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! Edited by newrehmi
Link to post
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.

Edited by thescientist
Link to post
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.

Edited by newrehmi
Link to post
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!!!!

Edited by newrehmi
Link to post
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.
Edited by thescientist
Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...