Jump to content

Extremely Frustrating Problem, Guys.


Amps

Recommended Posts

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?  :)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
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.