Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Add Simple Comment Forum to Individual Web Pages

Posted on 2006-10-22
8
Medium Priority
?
421 Views
Last Modified: 2011-04-14
     My site includes dozens of static html pages that area similar but each contains information about a different subject. I want to add a section at the bottom of each page where anyone can post a comment about the page.
      I've looked at phpBB, WowBB, SMF, and others. They are all overkill for what I need.  I've also looked at the directions for making a very simple script from scratch ( http://www.devshed.com/index2.php?option=content&task=view&id=969&pop=1&hide_ads=1&page=0&hide_js=1 ) and even that seems to be overkill, as it makes a whole forum site. Plus, I won't want or need user login or anything like that. It will be for anyone (they would just type their name into a textbox).
    I would want my comment area to look sort of like the bottom of an experts-exchange.com question page: the text of my web page would be analogous to the  Question at the top of these ee pages; and then at the bottom of the page I would just want a comment box like the "Post a Comment" textarea in these ee pages.  Then, just like here on ee, the added comments would show chronologically (and show the name of the commenter), above the Post-a-Comment textarea.
    I'm using MySQL as the database backend. I assume I would need only one table for all my different pages, like this:

ID  int 20  (autoincrement, as the primary key of the table)
PageID  varchar 11   (letters/numbers combination to identify which page)
Name  varchar 40  (to identify who made the comment)
Comment  text  (the content of the comment)
Posted   datetime  (so the comments can be displayed in chron order)

I assume the first step would be to convert all these static html pages to have the .php extension so they would be processed by the PHP interpreter and able to dynamically gather and display the existing comments.

*   1) Assuming I have added the .php extension to my pages and have a variable or hidden form field for the PageID to distinguish one page from another, what is the simplest code to add to my pages to have a working comment-posting area at the bottom of each page?   (It would be similar to the way the script works at http://www.zimmertech.com/tutorials/php/25/comment-form-script-tutorial.php , but that one does some things I don't need, such as css, blocking html markup and using BBcode instead, etc. [I'm going to use a free javascript wysiwyg textarea replacement]; and the zimmertech.com script does not include some things I may want such as formatting similar to these e-e pages, and possibly threading. That script also seems more complicated than I need.)

*   2) How would that code be modified to allow commentators to direct their comments to an existing comment, so it would be like a simplified threaded-discussion, but all comments are shown openly (without clicking a link to open a topic or see replies, etc.); it would show everything at once like here on ee, but the comments would be indented and grouped hierarchically if a commentator replies to a specific comment. (I assume this part would require adding another column (like ParentID) to my table to track which comment goes under which other comment.) But if they just post a general comment, it would still simply be added to the general display, by chronological order, most recent on the bottom. Thanks.
0
Comment
Question by:Randall-B
  • 4
  • 4
8 Comments
 
LVL 2

Assisted Solution

by:davidateuropol
davidateuropol earned 1500 total points
ID: 17787514
Hi,

You would indeed only need 1 table for it. I think that the fields you would put in it, cover your needs.

1.) You would need to insert a code like this :
<form name="commentsform" action="post">
<input type="hidden" name="pageID" value="<?php echo $valuepageID ?>">
<label name="Name" for="visitorsname" value="Name"><input type="text" name="visitorsname">
<label name="Comment" for="visitorscomment" value="Name"><textarea name="visitorscomment" cols=80 rows=6></textarea>
</form>

<?php
$pageid=$_POST['pageID'];
$name=$_POST['visitorsname'];
$comment=$_POST['visitorscomment'];

$link=mysql_connect($dbhost,$dbuser,$dbpassword);
mysql_select_db($dbname);

$insert_sql = "INSERT INTO tblname (PageID,Name,Comment,Posted) VALUES ('$pageid','$name','$comment',NOW())";

if (mysql_query($insert_sql)) {
echo "<p>Your enquiry information has been updated in the enquiry table.</p>";  
}
else {
echo "<p>Error adding information for this enquiry: ".mysql_error()."</p>";
}

?>

This form off course is very rudimentary, but everything you need is in there.

As you tell me that you would like to have this form on every page of your website, I would advise you to put the code of the form into another page (extension .php), and include it at every page like this :
<?php
require("formscript.php");
$pageid="whateverpage";
?>

I think this should help you out for the first question.

For the second question however, you need to change your table. In this case, you need to create a parent-child relation which can be achieved by making your table like this :
ID  int 20  (autoincrement, as the primary key of the table)
PageID  varchar 11   (letters/numbers combination to identify which page)
Name  varchar 40  (to identify who made the comment)
Comment  text  (the content of the comment)
Posted   datetime  (so the comments can be displayed in chron order)
Parent_ID int 20 (foreign key) --> this field will contain the ID of the parent ID in the thread.

This would also mean that you would have to change the form and the treatment of it. For the order on how they should be displayed you could still use the date.

However, if you plan to do this, I suggest you look into the option of taking an already existing forum-like solution.
Maybe have a look to the following solution http://www.greywyvern.com/forum or do a search in google on "orca forum". This is a small solution that could suit your needs.
0
 

Author Comment

by:Randall-B
ID: 17787690
davidateuropol:
    Thanks for the help with part 1, but it seems like this code only shows a form and inserts the form data into the table.  More importantly, I also need it to also display all of the previous commentators' comments, just like here on experts exchange, where we see all prior comments displayed on the page, and we have a "post a comment" box underneath all of the previous comments.  I want the layout and format to look like this page. Thanks.
0
 
LVL 2

Accepted Solution

by:
davidateuropol earned 1500 total points
ID: 17787884
Okay...

You can add the following code :

<?php
//make connection to db (you could put this in the script I have put above then no connection is needed anymore.)

$select_sql = "SELECT * FROM tblname WHERE PageID LIKE '$pageid' ORDER BY Posted ASC" //Or DESC instead of ASC depending on which order you want.

$rs = mysql_query($select_sql);

while($row = mysql_fetch_array($rs)){
//now you can print it like you want.
echo "<b>Written by : ".$row["Name"]."</b><br>".
        $row["Comment"];
}
?>

Something like that.
But keep in mind that here on experts-exchange, they already use a system that relates towards your second option and not to your first one. This is a forum.

Also I forgot to tell you this but in my previous comment, before you execute anything of a query,you need to check first if the $_POST-variables exist. You can do this like
if(isSet($_POST["pageID"])){
Code that needs to be executed
}

because you need to check 3 variables I would do it as follows for each one :

$postvariableisset=true; //This should before you begin checking the variables

if(isSet($_POST["pageID"])){
Code that needs to be executed
}
else {
$postvariablesisset=false;
}

After this put the query-execution within another if :
if($postvariableisset){
query execution
}
else{
echo "You didn't fill in all the required fields.";
}

Hope this helps you further
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:Randall-B
ID: 17789490
davidateuropol,
    I got a php page working to show the existing comments and display the form, then I tried to "include" or "require" that page in a main web page, like this:
     <?php
         include("discussionQuestion2.php");
         $pageID = "A-35";
         ?>
   I can see that the included page is being included properly. But the included script is not receiving the $pageID from the main page.
   The main script is not passing the value to the included page.  What should I put in the included page to receive the pageID value?  
    I tried:   $pageID=$_POST['pageID'];  and also tried "GET", but those do not recieve the pageID value from the main page. How can I pass the value?
0
 
LVL 2

Assisted Solution

by:davidateuropol
davidateuropol earned 1500 total points
ID: 17789704
Hi,

Try to put $pageID = "A-35"; in front of the include-function otherwise the variable isn't known wihtin the function. Hope that helps.
0
 

Author Comment

by:Randall-B
ID: 17789823
davidateuropol,
   Yes, it works when I put the value before the include function.  Thanks.

I have also looked at the simple Orca threaded discussion that you recommended, at: http://www.greywyvern.com/code/php/orcaforum_4.3c.zip , and I would help making it usable on all pages, with their individual pageID .  Currently, it seems that the script is usable only on one page.  
   Also:  I like the expand all/collapse all feature, but how would I modify it so "expand all" would not only show all the topics, but would also show *all the comments* under all the topics (so it's more like experts-exchange where you can see all comments)?
0
 
LVL 2

Expert Comment

by:davidateuropol
ID: 17789967
You're right..., at the moment this script is only usable on one page..., I haven't looked to it into detail but I think that it should be possible to adapt it to your needs, without to much investment (at least a lot less then developing it yourself).

I would start by checking the fields in the table and try to add a pageID field..., afterwards, you would need to change the select-statement and the insert-statements first and see what that gives.

0
 

Author Comment

by:Randall-B
ID: 17791676
davidateuropol:
    OK, if I decide to try to adapt the Orca script, I'll probably post another question asking for help doing that. Thanks.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question