Database Articles

  Home arrow Database Articles arrow Page 4 - PHP/MySQL News with Comments
DATABASE ARTICLES

PHP/MySQL News with Comments
By: Matt Wade
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 28
    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

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    PHP/MySQL News with Comments - Displaying the comments


    (Page 4 of 7 )

    The next thing we want to do is display comments. Like I mentioned earlier, we need to query the comments table for rows that where news_id matches the piece of news being viewed.

    <?php
    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";
         
    }
    ?>

    As you can see, this is very similar to the displayNews function. In both, all we are doing is querying the database and looping to display all the data returned. On way you could enhance this would be to add a postdate field to the comments table and then order the comments query by date.

    More Database Articles Articles
    More By Matt Wade

    blog comments powered by Disqus

    DATABASE ARTICLES ARTICLES

    - Completing a Book Inventory Management System
    - Uploading Images for a Book Inventory Manage...
    - Finishing the Add Book Story for a Book Inve...
    - Integration Testing for a Book Inventory Man...
    - User Stories for a Book Inventory Management...
    - Unit Testing a Book Inventory Management Sys...
    - Testing a Book Inventory Management System
    - Implementing Models for a Book Inventory Man...
    - Book Inventory Application: Publishers and B...
    - Handling Publishers in a Book Inventory Mana...
    - Publisher Administration for Book Inventory ...
    - Book Inventory Management
    - Using the SQL Reference Manual
    - Using Oracle SQL Developer with SQL Statemen...
    - Fixing Errors with Oracle SQL Developer


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 5 - Follow our Sitemap