Creating a News System with PHP - Part 2 - Editing an Item
(Page 4 of 5 )
Great, we now are able to delete items that we previously added to the news. So, now we need to be able to edit them also. Let's start off the same way that we did with the delete. We will add a line of code to check for the action of edit.
<?php if($action == "edit") { ?> |
We will just grab the information out of the file like we did with the delete, except this time we will put it in a HTML form so that it can be changed. Before we can display it in the form, however, we need to convert the HTML breaking returns back into end of lines so that the lines are separated in the textarea.
<?php if($action == "edit") { $data = file('news.txt'); $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 "Make the changes you would like and press save.<BR>\n"; echo "<FORM ACTION=\"$PHP_SELF?action=edit\" METHOD=\"POST\" NAME=\"editform\">\n"; echo "Name:<BR>\n"; echo "<INPUT TYPE=\"text\" SIZE=\"30\" NAME=\"name\" value=\"".$pieces[1]."\"><BR>\n"; echo "The News:<BR>\n"; echo "<TEXTAREA NAME=\"news\" COLS=\"40\" ROWS=\"5\">".$news."</TEXTAREA><BR><BR>\n"; echo "Password:<BR>\n"; echo "<INPUT TYPE=\"password\" SIZE=\"30\" NAME=\"password\"><BR>\n"; echo "<INPUT TYPE=\"hidden\" NAME=\"date\" VALUE=\"".$pieces[0]."\">\n"; echo "<INPUT TYPE=\"hidden\" NAME=\"id\" VALUE=\"$id\">\n"; echo "<INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Save\"><BR>\n"; echo "</FORM>\n"; exit; } ?> |
Cool, now you can edit that information to your hearts content. Now let's worry about what happens when the Save button is pushed. First off, we need a line of code, similar to the one for the delete section, that checks that the action is edit and the password field has been populated.
<?php if($action == "edit" && isset($HTTP_POST_VARS['password'])) { ?> |
Next thing is to load the whole file into and array and then change the news item we want to change and finally save it back to the file.
<?php if($action == "edit" && isset($HTTP_POST_VARS['password'])) { //obviously you should change this password on the next line if($HTTP_POST_VARS['password'] == "editpass") { //First let's recompile that line with the pipe symbols so we can reinsert it $line = $HTTP_POST_VARS['date'] . "|" . $HTTP_POST_VARS['name']; $line .= "|" . $HTTP_POST_VARS['news']; $line = str_replace("\r\n","<BR>",$line); $line .= "\r\n"; $data = file('news.txt'); $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.txt','w'); foreach($data as $element) { fwrite($fp, $element); } fclose($fp); echo "Item Edited!<BR><BR>\n"; echo "<a href=\"$PHP_SELF\">Go Back</a>\n"; exit; } else { echo "Bad password!\n"; exit; } } ?> |
Next: The Final Product >>
More Miscellaneous Articles
More By Matt Wade