Creating a News System with PHP - Part 2 - Adding Options to the Display
(Page 2 of 5 )
The first thing we need to do is add links next to each news item so that we have a way to tell the script we want to edit or delete the item. So, let's get to adding those links.
For the administrative script, I really don't care if the newest news is first. In fact, I would rather have the oldest news first. It just makes things easier. So, remember where we used the array_reverse function? Well, I'm just going to take it out for this administrative script.
Now that we have the news in the order of oldest to newest, let's add some links for delete and edit next to the "Posted by" line of each item. We are going to need to have a way to address each news item. We already have the information in an array, and there is a key associated with that array. So, why don't we just use that? In order to use the key, we need it defined somehow. The following change will allow us to use the key.
<?php foreach($data as $element) {
should be changed to:
foreach($data as $key=>$element) { ?> |
Now that we have the key, we can add a unique URL for each news item to delete or edit it. Simply make the following change.
<?php echo $pieces[2] . "<BR>" . "<b>Posted by " . $pieces[1] . " on " . $pieces[0] . "</b><BR><BR>";
should be changed to:
echo $pieces[2] . "<BR>" . "<b>Posted by " . $pieces[1] . " on " . $pieces[0] . "</b>\n"; echo "&nbsp;<a href=\"$PHP_SELF?action=delete&id=$key\">Delete</a>\n"; echo "&nbsp;<a href=\"$PHP_SELF?action=edit&id=$key\">Edit</a>\n"; echo "<BR><BR>\n"; ?> |
This is going to give us two links after each "Posted by" line that will allow us to delete and edit news.
Next: Deleting an Item >>
More Miscellaneous Articles
More By Matt Wade