[SOLVED] "Edit News" does not edit... just adds?
Posted: Sun Jun 13, 2004 6:27 am
Hi, me again...
Before I go to bed, I thought I would post this code:
I have been piecing together snippets to make myself a very simple news admin section... I have "delete" and "post" working great (thanks to phpdn), but the above edit code does not work the way I want it to: it adds a post to my news, but it does not edit the existing post... I can't seem to see the reason why...
My goal is to have my edit feature work like this:
- Click on edit for particular news-post
- The edit form pops up on the page
- Title input has a value of the original title string
- News-post textarea has a value of the original news-post string
User then edits the title/news.
- Press submit
The edited post then writes back to the news.dat file, and the news is updated on admin page (but no new post is added, just the edited one).
I feel like I am 90% there... So any help would be greatly appreciated... Suggestions... Does anyone know of any simular "edit" code/snippets that I could use to learn from?
Again, thanks in advance, I appreciate any help you can give me.
Cheers
<m />
Before I go to bed, I thought I would post this code:
Code: Select all
if($_GET['action'] == "edit") {
$data = file('news.dat');
$element = trim($data[$id]);
$pieces = explode("%~#", $element);
//the next line is to reverse the process of turning the end of lines into breaking returns
$news = str_replace("<br />","\r\n",$pieces[2]);
echo "<form action="$PHP_SELF?action=editnow" method="post" name="editform">\n";
echo "<input type="hidden" name="date" value="".$pieces[0]."">\n";
echo "<input type="hidden" name="id" value="$id">\n";
echo "Title:\n";
echo "<input type="text" size="30" name="title" value="".$pieces[1]."">\n";
echo "The news:\n";
echo "<textarea name="news" cols="40" rows="5">".$news."</textarea>\n";
echo "<input type="hidden" value = "editpass" size="30" name="password"><br />\n";
echo "<input type="submit" name="submit" value="Submit">\n";
echo "</form>";
exit();
}
if($_GET['action'] == "editnow") {
//First let's recompile that line with the pipe symbols so we can reinsert it
$line = date("m.d.y") . "%~#" . $HTTP_POST_VARS['title'];
$line .= "%~#" . $HTTP_POST_VARS['news'];
$line = str_replace("\r\n","<br />",$line);
$line .= "\r\n";
$data = file('news.dat');
$data[$id] = $line;
//the next line makes sure the $data array starts at the beginning
reset($data);
//now we open the file with mode 'w' which truncates the file
$fp = fopen('news.dat','w');
foreach($data as $element) {
fwrite($fp, $element);
}
fclose($fp);
}My goal is to have my edit feature work like this:
- Click on edit for particular news-post
- The edit form pops up on the page
- Title input has a value of the original title string
- News-post textarea has a value of the original news-post string
User then edits the title/news.
- Press submit
The edited post then writes back to the news.dat file, and the news is updated on admin page (but no new post is added, just the edited one).
I feel like I am 90% there... So any help would be greatly appreciated... Suggestions... Does anyone know of any simular "edit" code/snippets that I could use to learn from?
Again, thanks in advance, I appreciate any help you can give me.
Cheers
<m />