Miscellaneous

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


    (Page 1 of 5 )

    This is part two in a series on creating a news system with PHP. This second installment will expand on our simple news system that only used a file for storage. We will continue with only a file, but add in editing and deletion functions.

    Well, I know in the first installment I said we would be adding database capabilities in this second installment. Sorry to disappoint, but I felt there were other things we needed to touch on before going to databases. Namely, the ability to delete and edit news that we have already added. If you haven't read the first tutorial in this series, I highly recommend you go check it out then come back to this one. I will be building on the foundations that we created in the first tutorial.

    Remember back to the first tutorial, we ended up with two scripts. One that actually displayed our news and one that we used to enter the news. What I plan to do in this tutorial is to only modify the administrative script. But, in order to delete or edit news, we need to see that news. So, I am going to add the news displaying script to the admin script. And then we will go from there. The two added together should look something like the following.

    <?
    echo "<H1><u>Current News</u></H1>\n";
    $data = file('news.txt');
    $data = array_reverse($data);
    foreach($data as $element) {
        $element = trim($element);
        $pieces = explode("|", $element);
        echo $pieces[2] . "<BR>" . "<b>Posted by " . $pieces[1] . " on " . $pieces[0] . "</b><BR><BR>";
    }
    echo "<HR>\n";
    echo "<H1><u>Add News</u></H1>\n";
    if($HTTP_POST_VARS['submit']) {
        if($HTTP_POST_VARS['password'] == 'pass') {
            if(!$HTTP_POST_VARS['name']) {
                echo "You must enter a name";
                exit;
            }
            if(!$HTTP_POST_VARS['news']) {
                echo "You must enter some news";
                exit;
            }
            if(strstr($HTTP_POST_VARS['name'],"|")) {
                echo "Name cannot contain the pipe symbol - |";
                exit;
            }
            if(strstr($HTTP_POST_VARS['news'],"|")) {
                echo "News cannot contain the pipe symbol - |";
                exit;
            }
            $fp = fopen('news.txt','a');
            if(!$fp) {
                echo "Error opening file!";
                exit;
            }
            $line = date("m.d.y") . "|" . $HTTP_POST_VARS['name'];
            $line .= "|" . $HTTP_POST_VARS['news'];
            $line = str_replace("\r\n","<BR>",$line);
            $line .= "\r\n";
            fwrite($fp, $line);
            if(!fclose($fp)) {
                echo "Error closing file!";
                exit;
            }
            echo "<b>News added!</b>\n";    
        } else {
            echo "Bad Password";
        }
    }

    ?>
    <FORM ACTION="<?=$PHP_SELF?>" METHOD="POST" NAME="newsentry">
    Your name:<BR>
    <INPUT TYPE="text" SIZE="30" NAME="name"><BR>
    The News:<BR>
    <TEXTAREA NAME="news" COLS="40" ROWS="5"></TEXTAREA><BR><BR>
    News Password:<BR>
    <INPUT TYPE="password" SIZE="30" NAME="password"><BR>
    <INPUT TYPE="submit" NAME="submit" VALUE="Post it!"><BR>
    </FORM>

    Great, now that we have our "new" admin script. Let's get to doing something new with it.

    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 5 - Follow our Sitemap