stoopid error on editing a file:)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Idoru
Forum Newbie
Posts: 3
Joined: Thu Oct 10, 2002 7:52 am
Location: Portugal

stoopid error on editing a file:)

Post by Idoru »

Hi all,
I'm trying to edit a file but i have some bad side effects, when i 'edit' a record i change THE record and ALL the previous ones :| also the same when i delete, and adding a line, it's not doing everything it stops in the middle,
---code for deleting
<?php
$id = ($_POST["id"]);
$article = ($_POST["article"]);
$title = ($_POST["title"]);
$id = (int) $id;


$newfile = file("hope.xml");
$newstuff = "";
$fp = fopen("hope.xml","w");
fwrite($fp, $newfile[0]);
//fwrite($fp, $newstuff);
$changed = "&article=not deleted!";
for($i=1;$i<count($newfile);$i++) {
if (preg_match("\"$id\"", $newfile[$i]))
{
fwrite($fp, $newstuff);
}
else
{
fwrite($fp, $newfile[$i]);
$changed = "&article=deleted!";
}
}
fclose ($fp);
echo ("$changed");
?>

---code for adding
<?php
$id = ($_POST["id"]);
$article2 = ($_POST["article"]);
$title = ($_POST["title"]);
$id = (int) $id;
$id++;
$article = urlencode($article2);
$article = str_replace("%0D"," ",$article);
$article = urldecode($article);


$newfile = file("hope.xml");
$newstuff = "<newsitem id=\"$id\" link=\"$article\">$title</newsitem>\r\n";
$fp = fopen("hope.xml","w");
if (count($newfile) > 13) {
echo ("&article=The system support is limited to ten articles/subjects.");
}else {
fwrite($fp, $newfile[0]);
fwrite($fp, $newstuff);
for($i=1;$i<count($newfile);$i++) {
fwrite($fp, $newfile[$i]);
}
echo ("&article=Was added");
}
fclose ($fp);
?>
--------also how do i write enter charachter?
--code for editing
<?php
$id = ($_POST["id"]);
$article2 = ($_POST["article"]);
$title = ($_POST["title"]);
//$id = (int) $id;
$article = urlencode($article2);
$article = str_replace("%0D"," ",$article);
$article = urldecode($article);

$newfile = file("hope.xml");
$newstuff = "<newsitem id=\"$id\" link=\"$article\">$title</newsitem>\r\n";
$fp = fopen("hope.xml","w");
fwrite($fp, $newfile[0]);
//fwrite($fp, $newstuff);
$changed = "&article=not changed!";
for($i=1;$i<count($newfile);$i++) {
if (preg_match("\"$id\"", $newfile[$i]))
{
fwrite($fp, $newstuff);
}
else
{
fwrite($fp, $newfile[$i]);
$changed = "&article=changed!";
}
}
fclose ($fp);
echo ("$changed");
?>
---
---------file
<news>
<newsitem id="1" link="text1">title1</newsitem>
<newsitem id="0" link="text0">title0</newsitem>
</news>
---
Sorry for the long post, and the bad english, i really need this lil help(as everyone else) but please if you have any questions just ask.

Thx in advance!
Pace!
Idoru
Forum Newbie
Posts: 3
Joined: Thu Oct 10, 2002 7:52 am
Location: Portugal

Post by Idoru »

just solved it using strstr ;D
Post Reply