[SOLVED] "Edit News" does not edit... just adds?

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
mhulse
Forum Commoner
Posts: 41
Joined: Fri Jun 11, 2004 12:50 am

[SOLVED] "Edit News" does not edit... just adds?

Post by mhulse »

Hi, me again...

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);
}
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. :D

Cheers
<m />
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I didn't see a real question in that post so all I have is a question:

Code: Select all

if($_GET['action'] == "edit") { 
    $data = file('news.dat'); 
    $element = trim($data[$id]); 
    $pieces = explode("%~#", $element);
Where does $id get set?
mhulse
Forum Commoner
Posts: 41
Joined: Fri Jun 11, 2004 12:50 am

Post by mhulse »

Hi, this is where I am (trying) to set it:

Code: Select all

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:

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") . "%~#" . $_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";
}
?>
Sorry if my original question was vague or ambiguous...

Thanks for any help you can give me.

Cheers
<m />
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

change $id to $_GET['id']
mhulse
Forum Commoner
Posts: 41
Joined: Fri Jun 11, 2004 12:50 am

Post by mhulse »

marked this problem [SOLVED] It all works great now (thanks to Alen over at http://www.free-php-scripts.net/forum/index.php)

Code: Select all

<?
require('config.php');

echo date("d M Y");
echo "<br />";
echo '<a href="'.$_SERVER['PHP_SELF'].'?action=add">Add News</a>'."\n";
echo "&nbsp;|&nbsp;\n";
echo "<a href="logoff.php">Log Off</a>\n";
echo "<hr />\n";

if($_POST['do']=='addnew') {
	$title=$_POST['title'];
	$news=$_POST['news'];
	$fp=fopen(NEWS_FILE,"a");
	$formatted=str_replace("\r\n","<br />",$news);
	$formatted=str_replace("\n","<br />",$formatted);
	$newsto=date("d M Y")."%~#".$title."%~#".$formatted;
	if(ENCODING=='yes') $newsto=base64_encode($newsto);
	fwrite($fp,StripSlashes($newsto)."\n");
	fclose($fp);
}

if($_GET['action']=='add') {
	echo "<form  action="$PHP_SELF?" method="POST" name="form0">\n";
	echo "Title: \n";
	echo "<input type="text" name="title" size="35">\n";
	echo "<br />\n";
	echo "<textarea name="news" cols="40" rows="13"></textarea>\n";
	echo "<br />\n";
	echo "<input type="submit" name="formbutton1" value="Add News">\n";
  	echo "<input type="hidden" name="do" value="addnew">\n";
	echo "</form>\n";
	echo "</body>\n";
	echo "</html>\n";
    exit();
}

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="$PHP_SELF?action=editnow&id=" .$_GET['id']. "" method="post" name="editform">\n"; 
 	//echo '<form action="'.$_SERVER['PHP_SELF'].'?action=editnow&id='.$_GET['id'].'" 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 "<br />\n";
    echo "<input type="text" size="30" name="title" value="".$pieces[1]."">\n";
    echo "<br />\n";
	echo "The news:\n";
	echo "<br />\n";
    echo "<textarea name="news" cols="40" rows="13">".$news."</textarea>\n";
	echo "<br />\n";
    echo "<input type="submit" name="submit" value="Submit">\n";
    echo "</form>\n";
    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[$_GET['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);
}

$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=edit&id='.$key.'">Edit</a>'."\n";
	echo "&nbsp;|&nbsp;\n";
 	echo '<a href="'.$_SERVER['PHP_SELF'].'?action=delete&id='.$key.'">Delete</a>'."\n"; 
    echo "<br /><br />\n";
}

echo "<hr />\n";

?>
I hope that helps others out! Thanks all for the help! :D Feel free to offer more suggestions on how to make the above code better! :)

Cheers!
<m />
Post Reply