Miscellaneous

  Home arrow Miscellaneous arrow Page 3 - Creating a News System with PHP - Part...
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

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    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

    blog comments powered by Disqus

    MISCELLANEOUS ARTICLES

    - Oracle Database XE: Indexes and Sequences
    - Modifying Tables in Oracle Database XE
    - Oracle Database XE: Tables and Constraints
    - More on Oracle Databases and Datatypes
    - Oracle Database XE Datatypes: Datetime and L...
    - Oracle Database XE Datatypes: Character and ...
    - From Databases to Datatypes
    - Firefox 3.6.6 Released with Improved Plug-in...
    - Attention Bloggers: WordPress 3.0 Now Releas...
    - Reflection in PHP 5
    - Inheritance and Other Advanced OOP Features
    - Advanced OOP Features
    - Linux from Scratch V.6.6 Review
    - Linux Gaining in Strength
    - Install Slackware on Your Old PC


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 1 - Follow our Sitemap