<?xml version="1.0" encoding="utf-8"?>
<tutorial>

<description>Make a Comment Sheet similar to the one on Spoono.</description>
<keywords>php, mysql, form, comment, post</keywords>

<title>Comment Board</title>
<slug>This is a three-part tutorial that shows you hwo to make a comment/discussion board like those on this site using PHP and mySQL. there are three files: comments.php, which displays the comments, commentadd.php, which processes the comment, and commentform.html which is simply a form that can be placed in any page manually or per SSI (server side includes). I realize that it is possible to have all three components in one file, but we are using this multifile method for tutorial functionality</slug>

<text>

<p><b>The Database</b><br />
First, you need to create a database called "comments"<br />
Then, use this code to create the table:
</p>

<![CDATA[
<pre>
CREATE TABLE `comtbl` (
`postID` INT NOT NULL AUTO_INCREMENT ,
`postTITLE` TEXT NOT NULL ,
`posterNAME` TEXT NOT NULL ,
`posterEMAIL` TEXT NOT NULL ,
`postTIME` TIMESTAMP NOT NULL ,
`postTXT` TEXT NOT NULL ,
PRIMARY KEY ( `postID` ) 
);
</pre>
]]>

<p><b>The Comment Viewing Page COMMENTS.PHP</b><br />
First we need to connect to the database/table, where username/password correspond to your username and password:
</p>

<![CDATA[r 
<pre>
$dbcnx = mysql_connect("localhost", "username", "password"); 
mysql_select_db("comments");
</pre>
]]>
<p>Next, we need to query the table, and sort it by ID Descending:
</p>
<![CDATA[
<pre>
$result = mysql_query("SELECT * FROM comtbl ORDER BY postID DESC"); 
    if (!$result) { 
      echo("&lt;b>Error performing query: " . mysql_error() . "&lt;/b>"); 
      exit(); 
    } 
</pre>
]]>
<p>Now we have fields to send into variables; and because we want it to show posts, we need a while loop (which repeats the query until the table completely read):
</p>
<![CDATA[
<pre>
while ($row = mysql_fetch_array($result) ) { 
	$msgTxt = $row["postTXT"];
	$msgId = $row["postID"]; 
	$SigName = $row["posterNAME"];
	$SigDate = $row["postTIME"];
	$msgTitle = $row["postTITLE"];
	$url = $row["posterEMAIL"];
</pre>
]]>
<p>
Ok. now here's the hard part: because i decided to use MySQL's TIMESTAMP fuction (which automatically enters the time of an addition to a table), i need to split up the postTIME string using the php substr() function (where $yr will give the year, $mo the month, etc):
</p>
<![CDATA[
<pre>
	$yr = substr($SigDate, 2, 2);
	$mo = substr($SigDate, 4, 2);
	$da = substr($SigDate, 6, 2);
	$hr = substr($SigDate, 8, 2);
	$min = substr($SigDate, 10, 2);
</pre>
]]>
<p>
Now since not everyone is familiar with military time, we have to replace hours like 15 and 22 with more user-friendly noes like 3 and 10, but at the same time add PM if its past noon, and add AM if its not:
</p>
<![CDATA[
<pre>
if ($hr > "11") {
	$x = "12";
	$timetype = "PM";
	$hr = $hr - 12;
	}else{
	$timetype = "AM";
	}
</pre>
]]>
<p>
Heres the last bit before we actually display the data. If the user decides to leave the Email field blank, we'll insert a '#' into the link on his name. If he filled it out, then we'll insert the 'mailto:' in front of the email address:
</p>
<![CDATA[
<pre>
if (!$url) {
	$url = "#";
	}else{
	$stat = $url;
	$url = "mailto:" . $url . "";
	}
</pre>
]]>
<p>
FINALLY, we get to display the row of data, and close out the loop, thus finishing the php. Here the code is followed by an example of how it will be printed (please realize that you can change how it looks at any time just by moving the variables within the echo statement)
</p>
<![CDATA[
<pre>
echo("&lt;p>&lt;b>$msgTitle&lt;/b> $msgTxt&lt;br>&lt;div align=right>$hr:$min $timetype | $mo/$da/$yr | $msgId, &lt;a 
href='$url'>$SigName&lt;/a>&lt;/div>&lt;/p>");
    } 
</pre>
]]>
<![CDATA[
&lt;p>&lt;b>Message Title&lt;/b> Text within the message, blah blah&lt;br>&lt;div align=right>Hour:Minute AM/PM | Month/Day/Year | Message ID, &lt;a 
href='mailto:test@test.com'>Name with email link&lt;/a>&lt;/div>&lt;/p>
]]>
<p><b>Form Processing: COMMENTADD.PHP</b><br />
Whew! Ok this file does the actual processing (adding) of the comments. First we'll set the variables passed through HTTP Post into a variable, then insert it into the database. Please remember to change the username and password.
</p>
<![CDATA[
<pre>
$assume = $_POST['assume'];
$posterEMAIL = $_POST['postemail'];
$postTXT = $_POST['posttxt'];
$posterNAME = $_POST['poster'];
$postTITLE = $_POST['posttitle'];

if ($assume == "true") {

$dbcnx = mysql_connect("localhost", "username", "password"); 
mysql_select_db("comments");

    
     $sql = "INSERT INTO comtbl SET posterNAME='$posterNAME', posterEMAIL='$posterEMAIL', 
postTXT='$postTXT', postTITLE='$postTITLE'"; 

      if (mysql_query($sql)) { 	        
	echo("&lt;P>Your comment has been added&lt;/P>"); 
      } else { 
        echo("&lt;P>Error adding entry: " .  mysql_error() . "&lt;/P>"); 
      } 
    } 
</pre>
]]>
<p>
OK, thats it for PHP, now all we need is some javascript to make it go right back to the comments page or to any other page you want (remember to close ur php tags!):
</p>
<![CDATA[
<pre>
&lt;script language=javascript> 
&lt;!-- 
location.href="comments.php"; 
//--> 
&lt;/script> 
</pre>
]]>
<p><b>OK! Heres for the form! COMMENTFORM.HTML</b>
This is basically just a form that sends all the data into commentadd.php to be processed.
</p>
<![CDATA[
<pre>
&lt;form action="commentadd.php" method=post>
&lt;input type="text" name="poster" size="23" value="name">&lt;br />
&lt;input type="text" name="posttitle" size="23" value="name">&lt;br />
&lt;input type="text" name="postemail" size="23" value="user@email.com">&lt;br />
&lt;textarea cols=44 rows=6 name="posttxt" size=24 wrap="VIRTUAL">message</textarea>&lt;br />
&lt;input type=hidden name=assume value=true>
&lt;input type="submit" value="submit"></form>

</script> 
</pre>
]]>

<p><b>Oh, so you want the full files?</b>
you already got commentform.html, so here is comments.php:
</p>
<![CDATA[
<pre>
&lt;?
$dbcnx = mysql_connect("localhost", "username", "password"); 
mysql_select_db("comments");

      
    $result = @mysql_query("SELECT * FROM comtbl ORDER BY postID DESC"); 
    if (!$result) { 
      echo("&lt;b>Error performing query: " . mysql_error() . "&lt;/b>"); 
      exit(); 
    } 
    
    while ($row = mysql_fetch_array($result) ) { 
	$msgTxt = $row["postTXT"];
	$msgId = $row["postID"]; 
	$SigName = $row["posterNAME"];
	$SigDate = $row["postTIME"];
	$msgTitle = $row["postTITLE"];
	$url = $row["posterEMAIL"];
	$yr = substr($SigDate, 2, 2);
	$mo = substr($SigDate, 4, 2);
	$da = substr($SigDate, 6, 2);
	$hr = substr($SigDate, 8, 2);
	$min = substr($SigDate, 10, 2);
	if ($hr > "11") {
	$x = "12";
	$timetype = "PM";
	$hr = $hr - 12;
	}else{
	$timetype = "AM";
	}
	if (!$url) {
	$url = "#";
	}else{
	$stat = $url;
	$url = "mailto:" . $url . "";
	}

echo("&lt;p>&lt;b>$msgTitle&lt;/b> $msgTxt&lt;br>&lt;div align=right>
   $hr:$min $timetype | $mo/$da/$yr | $msgId, &lt;a href='$url'>$SigName&lt;/a>&lt;/div>&lt;/p>");
    } 


?>
</pre>
]]>
<p>and here's commentadd.php:</p>
<![CDATA[
<pre>
&lt;?
$assume = $_POST['assume'];
$posterEMAIL = $_POST['postemail'];
$postTXT = $_POST['posttxt'];
$posterNAME = $_POST['poster'];
$postTITLE = $_POST['posttitle'];

if ($assume == "true") {

$dbcnx = mysql_connect("localhost", "username", "password"); 
mysql_select_db("comments");

    
     $sql = "INSERT INTO comtbl SET posterNAME='$posterNAME', posterEMAIL='$posterEMAIL', 
postTXT='$postTXT', postTITLE='$postTITLE'"; 
      if (mysql_query($sql)) { 
        echo("<P>Your comment has been added</P>"); 
      } else { 
        echo("<P>Error adding entry: " .  mysql_error() . "</P>"); 
      } 
    } 

?>
&lt;script language=javascript> 
&lt;!-- 
location.href="comments.php"; 
//--> 
&lt;/script> 
</pre>
]]>


</text>
</tutorial>