Miscellaneous
  Home arrow Miscellaneous arrow Page 4 - 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 - 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


       · 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 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek