Database Articles
  Home arrow Database Articles arrow Page 7 - PHP/MySQL News with Comments
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 
Dedicated Servers  
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? 
DATABASE ARTICLES

PHP/MySQL News with Comments
By: Matt Wade
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2002-09-03

    Table of Contents:
  • PHP/MySQL News with Comments
  • The Tables
  • Displaying the news
  • Displaying the comments
  • Displaying one item with comments
  • Adding comments
  • Summing it up and final script

  • 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


    PHP/MySQL News with Comments - Summing it up and final script


    (Page 7 of 7 )

    Now all that is left to do is writing a little bit of code that will tie everything all together and call the proper functions at the proper time. For that, I will use a case statement and pass a parameter via the URL called action. Then, all I need to do is check action and see what it matches and call the appropriate function.

    Right about now you are saying, what about adding news? How do I do that? Well, if you don’t want to take this any further, then you will just need to fire up PHPMyAdmin and input it there, or another MySQL client. What you should do is code a way to input the news. It will be very similar to inputting the comments. The one thing you will need to pay attention to is that you don’t want just anyone inputting news. Protect it somehow, whether that be with an .htaccess file or some PHP method.

    Without further delay, here is the final code. Until we meet again, Happy Coding!

    <?php
    /* user config variables */
    $max_items 5/* max number of news items to show */

    /* make database connection */
    $db mysql_connect ('localhost','root','Berskin99');
    mysql_select_db ('test',$db);

    function 
    displayNews($all 0) {
        
    /* bring in two variables
         * $db is our database connection
         * $max_items is the maximum number
         * of news items we want to display */
        
    global $db$max_items;
        
        
    /* query for news items */
        
    if ($all == 0) {
            
    /* this query is for up to $max_items */
            
    $query "SELECT id,title,newstext," 
                     
    "DATE_FORMAT(postdate, '%Y-%m-%d') as date " 
                     
    "FROM news ORDER BY postdate DESC LIMIT $max_items";
        } else {
            
    /* this query will get all news */
            
    $query "SELECT id,title,newstext," 
                     
    "DATE_FORMAT(postdate, '%Y-%m-%d') as date " .
                     
    "FROM news ORDER BY postdate DESC";
        }
        
    $result mysql_query ($query);
        while (
    $row mysql_fetch_assoc ($result)) {
            
    /* display news in a simple table */
            
    echo "&lt;TABLE border=\"1\" width=\"300\"&gt;\n";

            
    /* place table row data in 
             * easier to use variables.
             * Here we also make sure no
             * HTML tags, other than the
             * ones we want are displayed */
            
    $date $row['date'];        
            
    $title htmlentities ($row['title']);
            
    $news nl2br (strip_tags ($row['newstext'], '&lt;a&gt;&lt;b&gt;&lt;i&gt;&lt;u&gt;'));
            
            
    /* display the data */
            
    echo "&lt;TR&gt;&lt;TD&gt;&lt;b&gt;$title&lt;/b&gt; posted on $date&lt;/TD&gt;&lt;/TR&gt;\n";
            echo 
    "&lt;TR&gt;&lt;TD&gt;$news&lt;/TD&gt;&lt;/TR&gt;\n";
            
            
    /* get number of comments */
            
    $comment_query "SELECT count(*) FROM news_comments " .
                             
    "WHERE news_id={$row['id']}";
            
    $comment_result mysql_query ($comment_query);
            
    $comment_row mysql_fetch_row($comment_result);
            
            
    /* display number of comments with link */
            
    echo "&lt;TR&gt;&lt;TD&gt;&lt;a href=\"{$_SERVER['PHP_SELF']}" .
                 
    "?action=show&amp;id={$row['id']}\"&gt;Comments&lt;/a&gt;" .
                 
    "($comment_row[0]}&lt;/TD&gt;&lt;/TR&gt;\n";
            
            
    /* finish up table*/
            
    echo "&lt;/TABLE&gt;\n";
            echo 
    "&lt;BR&gt;\n";
        }
        
        
    /* if we aren't displaying all news, 
         * then give a link to do so */
        
    if ($all == 0) {
            echo 
    "&lt;a href=\"{$_SERVER['PHP_SELF']}" .
                 
    "?action=all\"&gt;View all news&lt;/a&gt;\n";
        }
    }

    function 
    displayOneItem($id) {
        global 
    $db;
        
        
    /* query for item */
        
    $query "SELECT * FROM news WHERE id=$id";
        
    $result mysql_query ($query);
        
        
    /* if we get no results back, error out */
        
    if (mysql_num_rows ($result) == 0) {
            echo 
    "Bad news id\n";
            return;
        }
        
    $row mysql_fetch_assoc($result);
        echo 
    "&lt;TABLE border=\"1\" width=\"300\"&gt;\n";

        
    /* easier to read variables and 
         * striping out tags */
        
    $title htmlentities ($row['title']);
        
    $news nl2br (strip_tags ($row['newstext'], '&lt;a&gt;&lt;b&gt;&lt;i&gt;&lt;u&gt;'));
        
        
    /* display the items */
        
    echo "&lt;TR&gt;&lt;TD&gt;&lt;b&gt;$title&lt;/b&gt;&lt;/TD&gt;&lt;/TR&gt;\n";
        echo 
    "&lt;TR&gt;&lt;TD&gt;$news&lt;/TD&gt;&lt;/TR&gt;\n";
        
        echo 
    "&lt;/TABLE&gt;\n";
        echo 
    "&lt;BR&gt;\n";
        
        
    /* now show the comments */
        
    displayComments($id);
    }

    function 
    displayComments($id) {
        
    /* bring db connection variable into scope */
        
    global $db;
        
        
    /* query for comments */
        
    $query "SELECT * FROM news_comments WHERE news_id=$id";
        
    $result mysql_query ($query);
        echo 
    "Comments:&lt;BR&gt;&lt;HR width=\"300\"&gt;\n";
        
        
    /* display the all the comments */
        
    while ($row mysql_fetch_assoc ($result)) {
            echo 
    "&lt;TABLE border=\"1\" width=\"300\"&gt;\n";
            
            
    $name htmlentities ($row['name']);
            echo 
    "&lt;TR&gt;&lt;TD&gt;&lt;b&gt;by: $name&lt;/b&gt;&lt;/TD&gt;&lt;/TR&gt;\n";
        
            
    $comment strip_tags ($row['comment'], '&lt;a&gt;&lt;b&gt;&lt;i&gt;&lt;u&gt;');
            
    $comment nl2br ($comment);
            echo 
    "&lt;TR&gt;&lt;TD&gt;$comment&lt;/TD&gt;&lt;/TR&gt;\n";
        
            echo 
    "&lt;/TABLE&gt;\n";
            echo 
    "&lt;BR&gt;\n";
        }
        
        
    /* add a form where users can enter new comments */
        
    echo "&lt;HR width=\"300\"&gt;";
        echo 
    "&lt;FORM action=\"{$_SERVER['PHP_SELF']}" .
             
    "?action=addcomment&amp;id=$id\" method=POST&gt;\n";
        echo 
    "Name: &lt;input type=\"text\" " .
             
    "width=\"30\" name=\"name\"&gt;&lt;BR&gt;\n";
        echo 
    "&lt;TEXTAREA cols=\"40\" rows=\"5\" " .
             
    "name=\"comment\"&gt;&lt;/TEXTAREA&gt;&lt;BR&gt;\n";
        echo 
    "&lt;input type=\"submit\" name=\"submit\" " .
             
    "value=\"Add Comment\"\n";
        echo 
    "&lt;/FORM&gt;\n";
         
    }

    function 
    addComment($id) {
        global 
    $db;
        
        
    /* insert the comment */
        
    $query "INSERT INTO news_comments " .
                 
    "VALUES('',$id,'{$_POST['name']}'," .
                 
    "'{$_POST['comment']}')";
        
    mysql_query($query);
        
        echo 
    "Comment entered. Thanks!&lt;BR&gt;\n";
        echo 
    "&lt;a href=\"{$_SERVER['PHP_SELF']}" .
             
    "?action=show&amp;id=$id\"&gt;Back&lt;/a&gt;\n";
    }

    /* this is where the script decides what do do */

    echo "&lt;CENTER&gt;\n";
    switch(
    $_GET['action']) {
        
        case 
    'show':
            
    displayOneItem($_GET['id']);
            break;
        case 
    'all':
            
    displayNews(1);
            break;
        case 
    'addcomment':
            
    addComment($_GET['id']);
            break;
        default:
            
    displayNews();
    }
    echo 
    "&lt;/CENTER&gt;\n";
    ?>


    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.

       · dfsf
       · Thanks for a great tutorial.
       · hiI've search the web for ages to find a newssystem that does excatly what this...
       · I seem to be getting the problem where it's not showing the news table at all and...
       · sounds like it doesn't interact with your &quot;news&quot;-database as it...
       · file loaded: http://luna-illusions.co.uk/weetom/newsscript.phpThere's no design...
       · Hi.I just love this Tutorial and it works perfectly except for 1 thing. When you...
       · Warning: mysql_connect() [function.mysql-connect]: Access denied for user...
       · I've discovered a problem with this tutorial. The Comment can only be 500 Characters...
       · Tutorial is nice, but previous one was better, this one is just final script cut...
       · Hello Mr/Mrs Tutorial Creator (xD)I'm new toP and MySQL and I suck basically...
     

    DATABASE ARTICLES ARTICLES

    - More on Query Optimization for Oracle Databa...
    - Query Optimization in Oracle
    - Clusters and Other Data Structures for Oracle
    - Using Indexes with an Oracle Database
    - The Basics of Data Structures in Oracle
    - Oracle Data Structures
    - Best Practices for PL/SQL Variables
    - What`s Code Without Variables?
    - Clauses, Sorting, and SQL Queries
    - The From Clause and SQL Queries
    - Query Primer
    - Full Text Searches and Strings
    - Searching with Strings
    - Pattern Matching with Strings
    - Working with Cases of Strings






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway