Creating a News System with PHP - Part 2
(Page 1 of 5 )
This is part two in a series on creating a news system with PHP. This second installment will expand on our simple news system that only used a file for storage. We will continue with only a file, but add in editing and deletion functions.
Well, I know in the first installment I said we would be adding database capabilities in this second installment. Sorry to disappoint, but I felt there were other things we needed to touch on before going to databases. Namely, the ability to delete and edit news that we have already added. If you haven't read the first tutorial in this series, I highly recommend you go check it out then come back to this one. I will be building on the foundations that we created in the first tutorial.
Remember back to the first tutorial, we ended up with two scripts. One that actually displayed our news and one that we used to enter the news. What I plan to do in this tutorial is to only modify the administrative script. But, in order to delete or edit news, we need to see that news. So, I am going to add the news displaying script to the admin script. And then we will go from there. The two added together should look something like the following.
<? echo "<H1><u>Current News</u></H1>\n"; $data = file('news.txt'); $data = array_reverse($data); foreach($data as $element) { $element = trim($element); $pieces = explode("|", $element); echo $pieces[2] . "<BR>" . "<b>Posted by " . $pieces[1] . " on " . $pieces[0] . "</b><BR><BR>"; } echo "<HR>\n"; echo "<H1><u>Add News</u></H1>\n"; if($HTTP_POST_VARS['submit']) { if($HTTP_POST_VARS['password'] == 'pass') { if(!$HTTP_POST_VARS['name']) { echo "You must enter a name"; exit; } if(!$HTTP_POST_VARS['news']) { echo "You must enter some news"; exit; } if(strstr($HTTP_POST_VARS['name'],"|")) { echo "Name cannot contain the pipe symbol - |"; exit; } if(strstr($HTTP_POST_VARS['news'],"|")) { echo "News cannot contain the pipe symbol - |"; exit; } $fp = fopen('news.txt','a'); if(!$fp) { echo "Error opening file!"; exit; } $line = date("m.d.y") . "|" . $HTTP_POST_VARS['name']; $line .= "|" . $HTTP_POST_VARS['news']; $line = str_replace("\r\n","<BR>",$line); $line .= "\r\n"; fwrite($fp, $line); if(!fclose($fp)) { echo "Error closing file!"; exit; } echo "<b>News added!</b>\n"; } else { echo "Bad Password"; } }
?> <FORM ACTION="<?=$PHP_SELF?>" METHOD="POST" NAME="newsentry"> Your name:<BR> <INPUT TYPE="text" SIZE="30" NAME="name"><BR> The News:<BR> <TEXTAREA NAME="news" COLS="40" ROWS="5"></TEXTAREA><BR><BR> News Password:<BR> <INPUT TYPE="password" SIZE="30" NAME="password"><BR> <INPUT TYPE="submit" NAME="submit" VALUE="Post it!"><BR> </FORM> |
Great, now that we have our "new" admin script. Let's get to doing something new with it.
Next: Adding Options to the Display >>
More Miscellaneous Articles
More By Matt Wade