Help - Search - Members - Calendar
Full Version: How To Add A Comment Box For Different Articles
Zymic Webmaster Forums > Web Design & Development > Server Side Scripting > PHP
JStuckel21
Hopefully some of you here can provide some help. I did a lot of web design about 10 years ago when I was young and I am trying to get back into it now. Back then all I used was html. Now there is so much more for what I want to accomplish.

Now, I'm setting up a website where me and some guys are writing about sports. Basic articles, rants, raves, whatnots....

Anyways, I want the readers to be able to add comments after each article.

How should I go about this? I've been researching this all day and I'm pretty overwhelmed. I have setup a MySQL database, as I know that is something I will need for the comments.

Does anyone maybe have a good tutorial for something like this? I've read through so many different ones today but I'm just having trouble getting off the ground.

Thanks for any help.

IamShipon1988
Ok let's assume you have a simple blogging system with code like this:
CODE
<?

$blogno = "5";             //Number of blog entries to show
require "db.php";     //Remember to set options in db.php

if (isset($_POST['submit'])) {     //New comment submission

    $insert = mysql_query("INSERT INTO comments (
    blog_id,
    name,
    comment)
    VALUES (
    '".$_POST['id']."',
    '".$_POST['name']."',
    '".$_POST['comment']."')") or die (mysql_error());
    
}

$result = mysql_query("SELECT id, poster, subject, post, post_date FROM blog ORDER by id DESC", $linkID) or die (mysql_error());

for ($i=0; $i < $blogno; $i++) {     //loops through blog entries

    $data = mysql_fetch_row($result);
    $poster = $data[1];
    $subject = $data[2];
    $id = $data[0];
    $post = $data[3];
    $post_date = $data[4];
    $commentresult = mysql_query("SELECT id, blog_id, name, comment FROM comments WHERE blog_id = '$id' ORDER by id DESC", $linkID) or die (mysql_error());
    $commentlimit = mysql_num_rows($commentresult );

    if (isset($poster)) {     //html for blog output

        ?>
        
        <h2><? echo $subject; ?></h2>
<p>Posted on <strong><? echo $post_date; ?></strong> and has <strong><? echo $commentlimit; ?></strong> comments     </p>
<div class="entry">
<p></p>
<p><? echo $post; ?></p>


To add an option for comments, you will have to add the following set of codes
CODE
<blockquote>    
          <?         

        if ($commentlimit == "0"){

            echo "<em>There are no comments to this post. Be the first to comment by pressing the comment button below.</em><br />";
        
        } else {
    
            for ($j=0; $j < $commentlimit; $j++) {
    
                $commentsdata= mysql_fetch_row($commentresult );
                echo "<b>".$commentsdata[2]."</b> <br />";
                ?> <p class="comment">
                <? echo $commentsdata[3]."</p></blockquote>"; ?>
                
<?
        echo "<blockquote>";
        
            }
    
        }
        
        ?>

        <br />
                <?
    
        if (isset($_POST['comment'])) {     //html for add comment form

            ?>
                <em>I coded this blog to allow users to use HTML in their post</em><br />    
            <form name="addcomment" action="goo.php" method="post">
            Name:<br/><input type="text" name="name" /><br /><br />
            Comment:<br/><textarea name="comment" cols="30" rows="2"></textarea><br /><br />
            <input type="image" name="submit" value="submit" src="images/add.gif" />
            <input type="hidden" name="id" value="<? echo $id; ?>">
            </form>
                        <?
    
        }
    
    ?>

        <form action="goo.php" method="post">
        <input type="image" name="comment" value="comment" src="images/comment.gif" /> <br />
        </form>

        </blockquote>


The name of this entire page is goo.php. The db.php file is simply a connection method to the database:
CODE
<?
$host="localhost";     //This is the host of your webserver
$db="db_username";         //The database name
$user="db_user";         //The username
$dbpassword="password";     //The password

$linkID = mysql_connect($host, $user, $dbpassword);
          mysql_select_db($db, $linkID);

?>


Your sql structure should look something like this for the post and comments:
CODE
CREATE TABLE `blog` (
  `id` int(11) NOT NULL auto_increment,
  `poster` text NOT NULL,
  `subject` text NOT NULL,
  `post` text NOT NULL,
  `post_date` text NOT NULL,
  KEY `id` (`id`)
) TYPE=MyISAM AUTO_INCREMENT=1;

CREATE TABLE `comments` (
  `id` int(11) NOT NULL auto_increment,
  `blog_id` int(11) NOT NULL default '0',
  `name` text NOT NULL,
  `comment` text NOT NULL,
  KEY `id` (`id`)
) TYPE=MyISAM AUTO_INCREMENT=1;


To add an admin option to delete comments, you can use the following code:
CODE
if (isset($_POST['deletecomments'])) {

        $delete = mysql_query("DELETE FROM comments WHERE id = '".$_POST['id']."'") or die (mysql_error());
        echo "comment deleted <br /><br />";

    }
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2012 Invision Power Services, Inc.