Hi All,
I was originally going to use .txt files, then thought it'd be way better to use .xml files. So, now I have to write to them. My issue is, how to read between the "lines". Right now I have:
PHP Code:
if(isset($_POST['newpost'])){
//get the info from the form, post to text file.
$title = stripslashes($_POST['title']);
$category = stripslashes($_POST['category']);
$body = stripslashes($_POST['body']);
//add data XML style
$data = "<post>" . "\n" . "<category>". $category . "</category>" . "\n" . "<title>" . $title . "</title>" . "\n" . "<date>" . $todaysdate . "</date>" . "\n" . "<body>" . "\n". $body . "</body>" . "\n" . "</post>". "\n\r";
//add data to CSV Text File
//$data = $category . "||" . $title . "||" . $body . "\n";
//open file and store contents
$file = fopen('posts.xml','a+');
if (!$file) {echo 'ERROR: Unable to open file.'; exit;}
//post current post, then write $currentdata back to file
// Write the data to the file
fwrite($file, $data);
// Close the file
fclose($file);
$postupdated = "You have created a new post!";
}
...which posts fine to the file. However, if I have:
Code:
<?xml version="1.0" standalone="yes"?>
<posts>
<post>
<title>Some people like meat.</title>
<category>Meat</category>
<pubDate>2008-January-15</pubDate>
</post>
</posts>
What is the best way to get the <posts> and then write to it without messing the xml file up?
Code:
<?xml version="1.0" standalone="yes"?>
<posts>
//insert $data here
</posts>
What I am thinking is I should get all the <post> data from the XML file and store it in $dataxml then add $data to it, then write it back to the file.
Would that be the best way?
Thanks