Thanks for the help, I can't wait to get this admin section working, I would love to post the whole code for all to use...
At a guess, it is because $id contains no value when you come to write to the file, so $data[$id] is parsed as $data[] (which would add a new index onto the end of $data). Perhaps
Hi, this is where I am (trying) to set it (I did not have the full code in original post, sorry...):
echo '<a href="'.$_SERVER['PHP_SELF'].'?action=delete&id='.$key.'">Delete</a>'."\n";
echo '<a href="'.$_SERVER['PHP_SELF'].'?action=edit&id='.$key.'">Edit</a>'."\n";
Here is the full code for the edit section:
<?
<?
if($_GET['action'] == "edit") {
$data = file('news.dat');
$element = trim($data.$_GET['id']);
//$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="'.$_SERVER['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") . "%~#" . $_POST['title'];
$line .= "%~#" . $_POST['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);
}
if($_GET['action'] == "delete") {
$data = file('news.dat');
//this next line will remove the single news item from the array
array_splice($data,$_GET['id'],1);
//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);
}
echo "Current News";
echo "<br />";
$data = file('news.dat');
//$data = array_reverse($data);
foreach($data as $key=>$element) {
$element = trim($element);
$pieces = explode("%~#", $element);
echo "Posted by: ".$pieces[1]." Date: ".$pieces[0]."<br />Post: ".$pieces[2]."<br />";
echo '<a href="'.$_SERVER['PHP_SELF'].'?action=delete&id='.$key.'">Delete</a>'."\n";
echo '<a href="'.$_SERVER['PHP_SELF'].'?action=edit&id='.$key.'">Edit</a>'."\n";
echo "<br /><br />\n";
}
?>
maybe this:
action=\"editnow\" instead of action=editnow\" on line 9
Well, you motivated me to update that part of the code 🙂
echo '<form action="'.$_SERVER['PHP_SELF'].'?action=editnow" method=\"post\" name=\"editform\">'."\n";
I am wondering if I sould change it to this:
echo '<form action="'.$_SERVER['PHP_SELF'].'?action=editnow&id='.$key.'" method=\"post\" name=\"editform\">'."\n";
Maybe it would all work much better if I put the two functions (edit and editnow) together as one function (Would an if satement suffice?)
Thanks for any help you can give me.
Cheers
<m />