Miscellaneous
  Home arrow Miscellaneous arrow Page 4 - Creating a News System with PHP - Part 1
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  
Forums Sitemap 
Download TestComplete 
JMSL Numerical Library 
IBM® developerWorks
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 1
By: Matt Wade
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2002-02-23

    Table of Contents:
  • Creating a News System with PHP - Part 1
  • Validating the Form
  • Storing the Form Data
  • Displaying the News

  • 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 1 - Displaying the News


    (Page 4 of 4 )

    So, we have a way to get the data from our browser into a text file. Now all we need to do is display the news. This is the simple part. Basically, all we are going to do is read the file into an array. Then, because the newest information is at the end of the array, we need to reverse the order of the data in the array. Then just separate our data into its different parts. Last, we display it. So, let's do it.

    First step, let's get the data into an array. For that we will use the file function. This function reads a file into an array. Each line in the file becomes an element in the array. Exactly what we need.

    <?php
    $data 
    file('news.txt');
    ?>

    Next, we want to reverse the array so that we are reading the newest news first. For that we will use the array_reverse function. Simple eh?

    <?php
    $data 
    file('news.txt');
    $data array_reverse($data);
    ?>

    Ok, so we have the data back out of the text file. What we need to do now is split each line back up into a date, a name and the news. The explode function will do exactly what we want. So, we will run a loop for as many elements as there are in our array and split each element with the explode function. While we are at it, we will echo out our results. I am also using the trim function on each element to remove the newline at the end.

    <?php
    $data 
    file('news.txt');
    $data array_reverse($data);
    foreach(
    $data as $element) {
        
    $element trim($element);
        
    $pieces explode("|"$element);
        echo 
    $pieces[2] . "&lt;BR&gt;" "&lt;b&gt;Posted by " $pieces[1] . " on " $pieces[0] . "&lt;/b&gt;&lt;BR&gt;&lt;BR&gt;";
    }
    ?>

    Place that into your page wherever you want the news to display, and bam you've got news.

    Thanks for tuning in for this first lesson on creating a news system in PHP. In the next lesson we will start saving all our data in a database. Till then, keep coding!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Denne artikel er nu ret interessandt, mon ikke der er noget der kan bruges her ?
       · im new on php, all this code goes embedded on another html file or in the same...
     

    MISCELLANEOUS ARTICLES

    - Stopping CSRF Attacks in Your PHP Applicatio...
    - Quick and Dirty AJAX Tutorial
    - Flickr Puzzle Mashup
    - The PAVISE of Security
    - Creating a CAPTCHA with PHP
    - Sending SMS Thru HTTP
    - The Postal Fix - Part 2
    - Adding Mail with Exim
    - The Postal Fix - Part 1
    - Create Your Own Custom API
    - Adding Drop Shadows with PHP
    - Writing a Basic Authentication System in PHP
    - Overlapping Images with GD
    - Using Sockets in PHP
    - Dynamic CSS with PHP





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT