I'm trying to get a piece of code to insert a new "Post-it" (written in XML) into an xml file at a given point (After the heading "Notes:").
At the moment all i have got it to do is append my new note to the end of the file
ok ... xml file looks like this:
<postits>
<html:h1>Notes:</html:h1>
<!-- NEW NOTE _SHOULD_ GO HERE -->
<note>
<date>19.08.03</date>
<ip>1.2.3.4</ip>
<by>Nick</by>
<heading>Uploads</heading>
<body>index.php<br />
styles.css <b>x2</b><br />
functions.php <b>x2</b><br />
home.php<br />
services.php</body>
</note>
</postits>
<!-- IT ACTUALLY GOES HERE :/ -->
ip, heading, by etc etc etc ... are provided by addnote.php, which is as follows:
<?php
if ($doform == true) {
include("form.php");
} else {
<h4>Check that the details are correct</h4>
<strong>Local date is:</strong>
<?php echo $date; ?>
<br />
<strong>Your IP is:</strong>
<?php echo $ip; ?>
<small>
— This will be stored with your note for security purposes.
</small>
<br /><br />
<strong>Your name is:</strong>
<?php echo $name; ?>
<br />
<strong>Note heading:</strong>
<?php echo $head; ?>
<br /><br />
<strong>Content:</strong>
<br />
<textarea rows="10" cols="60" readonly="readonly">
<?php echo $content; ?>
</textarea>
<br />
<form action="doadd.php" method="post">
<input type="hidden" name="date" value="<?php echo $date; ?>" />
<input type="hidden" name="ip" value="<?php echo $ip; ?>" />
<input type="hidden" name="name" value="<?php echo $name; ?>" />
<input type="hidden" name="head" value="<?php echo $head; ?>" />
<input type="hidden" name="content" value="<?php echo $content; ?>" />
<input type="hidden" name="doadd" value="1" />
<input type="submit" value="OK" />
<input type="button" value="Go Back" onclick="history.go(-1)" />
</form>
<?php
}
}
?>
and form.php is, well, i'll leave you to guess on that one .. its not hard 😉
the info is then passed to doadd.php which is like this:
<?php
$file = "notes.xml";
$myline = "<h1>Notes:</h1>";
$note = $myline;
$note .= '
<note>
<date>'.$date.'</date>
<by>'.$name.'</by>
<ip>'.$ip.'</ip>
<heading>'.$head.'</heading>
<body>
'.$content.'
</body>
</note>
';
if($fp=fopen($file, "r+")) {
while($line=trim(fgets($fp))) {
if($line == $myline) {
if (is_writable($file)) {
if (!fputs($fp, $note)) {
print "<br />Cannot write to file ($file)";
} else {
print("<br />Success!");
}
} else {
print "The file $file is not writable";
}
}
}
}
?>
I hope someone can help me. surely there is some way i can replace $myline with $note instead of appending $note to the end of the file :/
Thanks in advance
Borior