
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` ) );The Comment Viewing Page COMMENTS.PHP
$dbcnx = mysql_connect("localhost", "username", "password");
mysql_select_db("comments");
Next, we need to query the table, and sort it by ID Descending:
$result = mysql_query("SELECT * FROM comtbl ORDER BY postID DESC");
if (!$result) {
echo("<b>Error performing query: " . mysql_error() . "</b>");
exit();
}
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):
while ($row = mysql_fetch_array($result) ) {
$msgTxt = $row["postTXT"];
$msgId = $row["postID"];
$SigName = $row["posterNAME"];
$SigDate = $row["postTIME"];
$msgTitle = $row["postTITLE"];
$url = $row["posterEMAIL"];
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):
$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);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:
if ($hr > "11") {
$x = "12";
$timetype = "PM";
$hr = $hr - 12;
}else{
$timetype = "AM";
}
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:
if (!$url) {
$url = "#";
}else{
$stat = $url;
$url = "mailto:" . $url . "";
}
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)
echo("<p><b>$msgTitle</b> $msgTxt<br><div align=right>$hr:$min $timetype | $mo/$da/$yr | $msgId, <a
href='$url'>$SigName</a></div></p>");
}
<p><b>Message Title</b> Text within the message, blah blah<br><div align=right>Hour:Minute AM/PM | Month/Day/Year | Message ID, <a
href='mailto:test@test.com'>Name with email link</a></div></p>
Form Processing: COMMENTADD.PHP
$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>");
}
}
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!):
<script language=javascript> <!-- location.href="comments.php"; //--> </script>OK! Heres for the form! COMMENTFORM.HTML This is basically just a form that sends all the data into commentadd.php to be processed.
<form action="commentadd.php" method=post> <input type="text" name="poster" size="23" value="name"><br /> <input type="text" name="posttitle" size="23" value="name"><br /> <input type="text" name="postemail" size="23" value="user@email.com"><br /> <textarea cols=44 rows=6 name="posttxt" size=24 wrap="VIRTUAL">message<br /> <input type=hidden name=assume value=true> <input type="submit" value="submit">Oh, so you want the full files? you already got commentform.html, so here is comments.php:
<?
$dbcnx = mysql_connect("localhost", "username", "password");
mysql_select_db("comments");
$result = @mysql_query("SELECT * FROM comtbl ORDER BY postID DESC");
if (!$result) {
echo("<b>Error performing query: " . mysql_error() . "</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("<p><b>$msgTitle</b> $msgTxt<br><div align=right>
$hr:$min $timetype | $mo/$da/$yr | $msgId, <a href='$url'>$SigName</a></div></p>");
}
?>
and here's commentadd.php:
<?
$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("Your comment has been added
");
} else {
echo("Error adding entry: " . mysql_error() . "
");
}
}
?>
<script language=javascript>
<!--
location.href="comments.php";
//-->
</script>
Copyright © 2000-2010 Spoono, LLC. All rights reserved.
Network: Reseller Web Hosting by Spoono Host | Spoonloads | Absolute Cross
Terms of Service | Privacy Policy.