Miscellaneous
  Home arrow Miscellaneous arrow Page 3 - Creating a News System with PHP - Part...
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MISCELLANEOUS

Creating a News System with PHP - Part 2
By: Matt Wade
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 2
    2002-03-11

    Table of Contents:
  • Creating a News System with PHP - Part 2
  • Adding Options to the Display
  • Deleting an Item
  • Editing an Item
  • The Final Product

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Creating a News System with PHP - Part 2 - Deleting an Item


    (Page 3 of 5 )

    Ok, let's make the links actually do something! Let's deal with the delete link first. It is a bit less complicated than the edit link. First thing we will need to do is add a line of code that checks to see if the delete link has been clicked. We will check to see if the $action variable is set to delete with some code like this.

    <?php
    if($action == "delete") {
    ?>

    Simple enough eh? Ok, now we need to check a password to make sure it is ok to delete it and at the same time we will be confirming the deletion. We are basically going to do the same thing as we would to display all the news, except we are going to use the id we passed to the delete portion of the script to only display the one news item we want to delete.

    <?php
    if($action == "delete") {
        echo 
    "&lt;H2&gt;You are about to delete the following news item.&lt;/H2&gt;\n";
        
    $data file('news.txt');
        
    $element trim($data[$id]);
        
    $pieces explode("|"$element);
        echo 
    $pieces[2] . "&lt;BR&gt;" "&lt;b&gt;Posted by " $pieces[1] . " on " $pieces[0] . "&lt;/b&gt;\n";
        echo 
    "&lt;BR&gt;&lt;BR&gt;\n";
        echo 
    "Are you sure you want to delete this news item? If so, enter the password and click on Delete.&lt;BR&gt;\n";
        echo 
    "&lt;FORM ACTION=\"$PHP_SELF?action=delete\" METHOD=\"POST\" NAME=\"deleteform\"&gt;\n";
        echo 
    "Password:&lt;BR&gt;\n";
        echo 
    "&lt;INPUT TYPE=\"password\" SIZE=\"30\" NAME=\"password\"&gt;&lt;BR&gt;\n";
        echo 
    "&lt;INPUT TYPE=\"hidden\" NAME=\"id\" VALUE=\"$id\"&gt;\n";
        echo 
    "&lt;INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Delete\"&gt;&lt;BR&gt;\n";
        echo 
    "&lt;/FORM&gt;\n";
        exit;
    }
    ?>

    You'll notice that I included a hidden field that holds the id that was passed to the delete portion of the script. The reason I did this is that we will still need this id when we actually delete the news item. Now, what we need to do is add some code that checks to see if the action is set to delete AND the password field of the form has been set.

    <?php
    if($action == "delete" &amp;&amp; isset($HTTP_POST_VARS['password'])) {
    ?>

    Ok, that should suffice. That line should go to the very top of our script. Definitely make it above the last snippet that checked for an action of delete. If we don't check this first, things won't execute in the order we want them to. Now, let's check the password and then actually delete the news item.

    <?php
    if($action == "delete" &amp;&amp; isset($HTTP_POST_VARS['password'])) {
        
    //obviously you should change this password on the next line
        
    if($HTTP_POST_VARS['password'] == "deletepass") {
            
    $data file('news.txt');
            
    //this next line will remove the single news item from the array
            
    array_splice($data,$id,1);
            
    //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 deleted!&lt;BR&gt;&lt;BR&gt;\n";    
            echo 
    "&lt;a href=\"$PHP_SELF\"&gt;Go Back&lt;/a&gt;\n";
            exit;
        } else {
            echo 
    "Bad password!\n";
            exit;
        }
    }
    ?>

    More Miscellaneous Articles
    More By Matt Wade


       · need an html output to see exactly what is happening
       · it is really annoying
       · Just a note that the final product doesn't show up in the pdf, so if you print out...
       · How would I make it post news from newest to oldest?
       · Forget I ever posted that, I didn't read through the whole thing. It says it in the...
       · I've gone through this tutorial and updated the HTTP_POST_VAR to $_POST and I can...
       · I've been playing with the code and just can't seem to get it to work. I kept...
     

    MISCELLANEOUS ARTICLES

    - Using PHP to Stream MP3 Files and Prevent Il...
    - 10 Must Have Firefox Improvements
    - All About OpenOffice 3.0
    - Shell Script Writing
    - Loops in the UNIX Shell
    - The Test in the UNIX Shell
    - Data Streams and the UNIX Shell
    - Control Mechanisms of the UNIX Shell
    - Variables Within the UNIX Shell
    - The Shell and UNIX
    - In Detail: UNIX File Systems
    - Rights Management in UNIX
    - UNIX File Systems
    - The Terminal in UNIX
    - Operating Systems and UNIX





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT