开发者

Building a comment system in PHP, deciding which approach to take

I just threw together a little commenting system. But, I think it might be kind of onorthodox. For the submitted comment I'm us开发者_运维问答ing $_SERVER['SCRIPT_NAME'] and storing it as a varchar. Then, if the recorded location/varchar of the comment matches the current active page, the comments are displayed. Is this a good way to do this? What other ways would there be to approach this? Thank you.

<?php
require('inc/connect.inc.php');
require('inc/core.inc.php');
?>

<form action="<?php echo $current_file; ?>" method="post">
<input type="hidden" name="page" />
Name:<br /><input type="text" name="name" /><p>
Comment:<br /><textarea rows="8" cols="45" name="comment"></textarea><p>
<input type="submit" value="submit" />
</form>

<?php

$page_on = $_SERVER['SCRIPT_NAME'];

if(isset($_POST['page'])&&isset($_POST['name'])&&isset($_POST['comment'])) {
    $page = $_POST['page'];
    $name = $_POST['name'];
    $comment = $_POST['comment'];

    $page = $_SERVER['SCRIPT_NAME'];

    if(!empty($page)&&!empty($name)&&!empty($comment)) {
        $query = mysql_query("INSERT INTO comments VALUES ('','".mysql_real_escape_string($name)."','".mysql_real_escape_string($comment)."','".mysql_real_escape_string($page)."')");
        }
    }


echo '<br /><br />';


$result = mysql_query("SELECT * FROM comments ORDER BY id DESC");
while($row = mysql_fetch_assoc($result)) {
    $name = $row['name'];
    $comment = $row['comment'];
    $currentfile = $row['currentfile'];
        if($page_on==$currentfile) {
        echo $name.'\'s comment:<br />'.$comment.'<p>';
        }
    }

?>


As you are more looking for "an approach" rather than code, I will try helping you with a different way to look at things, hopefully making your coding more experienced, fast to process and less complex.

I will assume you have a blog, a gallery, a review site or whatever are those "objects" you want people to comment on.. For this small tutorial, lets call them just the comment(able) object, or simple "objects".

It makes logic that each object on your website or application will have a unique ID, or URL or whatever other identifier that you build your query around, correct? For this small tutorial, let's just call it the object's ID.

Now, create a very small SQL table and call it 'user_comments', with a structure like:

ID: the comment's ID
Object_ID: The object being commented
Authod_ID: username, email, or a user's id
TimeStamp: some date or timestamp
Child_OF: INT(11)

The trick/gimmick here resides in the *Child_Of* property, which will be responsible for categorizing the hierarchy of your comments. Let's see:

-- each comment with a Child_OF=0 means is the main comment thread of your object

Examples:

  • If comes user *John_Doe* and makes a comment, it will have an ID of 1
  • If another user makes a stand-alone comment, it will have an ID of 2, Child_Of 0 also..
  • If a third user comments on *John_Doe's* comment, that will have ID of 4, and Child_Of 1..

For sure, you can use the timestamp to order by date and of course add other fields to your database as you see fit.. That can be screening by IP address, SPAM protection, CAPTHAS, and so on.. The above is just another approach of doing things in a light way of code.

best wishes \ Ruslan


You are retrieving all of your comments on each page load and then determining which ones to display. Much better (and simpler to code) would be to retrieve only the ones you actually need, by using the same $_SERVER['SCRIPT_NAME']:

$result = mysql_query("SELECT * FROM comments WHERE currentfile='" . mysql_real_escape_string($_SERVER['SCRIPT_NAME']) . "' ORDER BY id DESC");

This eliminates the need to check in your fetching while loop whether or not to display the comment, since you've only pulled the ones you need. It's also a lot more scalable as your database increases in size -- it is highly inefficient to retrieve all rows on each page load.

It is a little strange to use $_SERVER['SCRIPT_NAME'] as a unique identifier, however. But if the page names are unique and will remain unique, that is, you never intend to show multiple articles/topics per page, and will always keep the same URL structure, it should work fine. A more orthodox approach would be to maintain some kind of identifier as part of the URL:

yourarticle.php?id=12345

...and store/retrieve comments identified by $_GET['id']


It looks like it should work ok, there is just an inefficiency in your SQL. You should change to this:

$result = mysql_query("SELECT * FROM comments WHERE currentfile = '".mysql_real_escape_string($page_on)."' ORDER BY id DESC");
while($row = mysql_fetch_assoc($result)) {
    $name = $row['name'];
    $comment = $row['comment'];
    echo $name.'\'s comment:<br />'.$comment.'<p>';
}

You can select only comments that belong to the current page by using a WHERE clause in your sql query. This way there is no need to use an if check to see if the comment belongs on the page, because the sql query will only return the appropriate comments.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜