Amps Posted May 30, 2007 Share Posted May 30, 2007 Hey everyone, Im trying to write to an XML file (which already has contents) from PHP and add a line BEFORE the closing tag of the XML document. So, using PHP im basically trying to add something before the closing tag of the xml file. I have tried tons of stuff like this: <?php $file_handle = fopen('db.xml','a'); $content=(' <veri> <title>Whats your name</title> <description>My name is Zacharia</description> <emotion>Happy</emotion> </veri>'); fwrite($file_handle,$content); fclose($file_handle); ?> and using Fseek like this: fseek($file_handle, -8, SEEK_END); to get to pointer before the closing tag. All has failed so far, and I really need this to work. Can anyone help? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted May 30, 2007 Share Posted May 30, 2007 Is the closing tag on the last line of the file? If so you can do something like this: <?php $contents = file('db.xml'); $tmp = $contents[count($contents) - 1]; // save the last line $contents[count($contents) -1] = 'Your new content'; $contents[] = $tmp; $fp = fopen('db.xml','w'); // create a new version of the file foreach ($contents as $line) fwrite($fp,trim($line)."\r\n"); fclose($fp); ?> Note: not checked for syntax errors. YMMV Ken Quote Link to comment Share on other sites More sharing options...
Amps Posted May 30, 2007 Author Share Posted May 30, 2007 You my friend, are a genius. It worked perfectly. I only have one little problem that has confused me slightly aswell, this is actually a very small problem but maybe someone knows here. Basicaly, im passing 3 variables from flash using the code: on (release) { var question = _root.question; var answer = _root.answer; var emotion = _root.emotion; getURL("ai.php", "_blank", "POST"); } Right, that part is fine. It works at passing the variables to PHP. But its then the PHP part: <?php $question = $_POST["question"]; $answer = $_POST["answer"]; $emotion = $_POST["emotion"]; $contents = file('db.xml'); $tmp = $contents[count($contents) - 1]; // save the last line $contents[count($contents) -1] = ' <veri> <title>$question</title> <description>$answer</description> <emotion>$emotion</emotion> </veri>'; $contents[] = $tmp; $fp = fopen('db.xml','w'); // create a new version of the file foreach ($contents as $line) fwrite($fp,trim($line)."\r\n"); fclose($fp); ?> The variables are passed ok, but the added content to the XML file will be something like "<title>$question</title>" instead of "<title>Are you human?</title>" which is the variable passed. Quote Link to comment Share on other sites More sharing options...
chrisprse Posted May 30, 2007 Share Posted May 30, 2007 Try changing: <?php $question = $_POST["question"]; $answer = $_POST["answer"]; $emotion = $_POST["emotion"]; $contents = file('db.xml'); $tmp = $contents[count($contents) - 1]; // save the last line $contents[count($contents) -1] = ' <veri> <title>$question</title> <description>$answer</description> <emotion>$emotion</emotion> </veri>'; $contents[] = $tmp; $fp = fopen('db.xml','w'); // create a new version of the file foreach ($contents as $line) fwrite($fp,trim($line)."\r\n"); fclose($fp); ?> To: <?php $question = $_POST["question"]; $answer = $_POST["answer"]; $emotion = $_POST["emotion"]; $contents = file('db.xml'); $tmp = $contents[count($contents) - 1]; // save the last line $contents[count($contents) -1] = ' <veri> <title>'.$question.'</title> <description>'.$answer.'</description> <emotion>'.$emotion.'</emotion> </veri>'; $contents[] = $tmp; $fp = fopen('db.xml','w'); // create a new version of the file foreach ($contents as $line) fwrite($fp,trim($line)."\r\n"); fclose($fp); ?> Chris. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted May 30, 2007 Share Posted May 30, 2007 That is happening because you are delimiting your string with single quotes. Variables are not evaluated when enclosed in single quotes, only double quotes. Try this: <?php $contents[count($contents) -1] = " <veri> <title>$question</title> <description>$answer</description> <emotion>$emotion</emotion> </veri>"; ?> Also, when posting code to this forum use tags, not tags. Ken Quote Link to comment Share on other sites More sharing options...
Amps Posted May 30, 2007 Author Share Posted May 30, 2007 Ah, I see. Thanks everyone. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.