Miscellaneous

  Home arrow Miscellaneous arrow Page 4 - 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 - 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("&lt;BR&gt;","\r\n",$pieces[2]);
        echo 
    "Make the changes you would like and press save.&lt;BR&gt;\n";
        echo 
    "&lt;FORM ACTION=\"$PHP_SELF?action=edit\" METHOD=\"POST\" NAME=\"editform\"&gt;\n";
        echo 
    "Name:&lt;BR&gt;\n";
        echo 
    "&lt;INPUT TYPE=\"text\" SIZE=\"30\" NAME=\"name\" value=\"".$pieces[1]."\"&gt;&lt;BR&gt;\n";
        echo 
    "The News:&lt;BR&gt;\n";
        echo 
    "&lt;TEXTAREA NAME=\"news\" COLS=\"40\" ROWS=\"5\"&gt;".$news."&lt;/TEXTAREA&gt;&lt;BR&gt;&lt;BR&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=\"date\" VALUE=\"".$pieces[0]."\"&gt;\n";
        echo 
    "&lt;INPUT TYPE=\"hidden\" NAME=\"id\" VALUE=\"$id\"&gt;\n";
        echo 
    "&lt;INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Save\"&gt;&lt;BR&gt;\n";
        echo 
    "&lt;/FORM&gt;\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" &amp;&amp; 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" &amp;&amp; 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","&lt;BR&gt;",$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!&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 4 - Follow our Sitemap