Jump to content
xisto Community
Sign in to follow this  
hippiman

Php Comment Page how to make your own comment page

Recommended Posts

I have just uploaded a working example of this put to use, at "hippi.trap17.com/comments/comments.php"


This is my second tutorial on PHP. It's not as long as the last one, and I'm going to try and explain this one better to make sure you know exactly what's going on.
I just figured this out today, so if it's missing any functionality, or there is anything wrong with it, let me know. It worked for me.
This comment page isn't supposed to look good, it's just supposed to work, but you can change the HTML however you want to make it look better and change the overall layout.

First of all, you need to make your config.php in the same directory as your comments page.
$pre = 'com_';    //use any prefix you want, as long as nothing else could have it.(you don't really need this, it's just if you have 
// something else with a table named 'comments.')

?> linenums:0'><?$dbserver="localhost";$user="root";$passwd=""; //Enter your password here$connect=mysql_connect($dbserver,$user, $passwd);$db = 'forum'; //just a database I picked, use one you already have, or make another one if you really want to.$pre = 'com_'; //use any prefix you want, as long as nothing else could have it.(you don't really need this, it's just if you have // something else with a table named 'comments.')?>You might want to make it echo something, using "echo 'something';", after the connection to make sure it connected. You could also use the "or die('something')" on the line where it connects, but I've never really gotten that to work.

Now, you need to have your comments.php. The HTML for it should have a form with the method as POST and a text input, a textArea, and a submit button. Make sure you name them 'name', and 'comment'. If you name them anything else, you'll have to change all of your code later to get it to work, so it's just easier to leave the names alone.
<form method='post'><h3>Name:</h3><br><input type='text' name='name'><br><h3>Comment:</h3><textarea name='comment'rows="10" cols="50" wrap='hard'></textarea><input type='submit' value='submit'></form>
I like "wrap='hard'" better than 'soft' and 'off'. It depends on what you want to do. There are HTML tutorials elsewhere that you could look at to see the difference.

Now, you need to start making your functions.
The first one you need should be a submitter function.

  mysql_query($sql, $connect);  //Makes the table if it doesn't exist.  The "." is the concatenation character for PHP 

$sql = 'INSERT INTO ' . $pre . 'comments (name, comment) VALUES ("' . $name . '", "' . $com . '");';
mysql_query($sql, $connect); //inserts your comment into the table.
} linenums:0'>function submitComment($db, $pre, $name, $com, $connect) { //database, prefix, name, comment, connection mysql_select_db($db); $sql = 'CREATE TABLE IF NOT EXISTS ' . $pre . 'comments (id int auto_increment, name text, comment text, primary Key(id));'; mysql_query($sql, $connect); //Makes the table if it doesn't exist. The "." is the concatenation character for PHP $sql = 'INSERT INTO ' . $pre . 'comments (name, comment) VALUES ("' . $name . '", "' . $com . '");'; mysql_query($sql, $connect); //inserts your comment into the table.}The "exiter" function is something I made up to make it so there are no errors when you use quotes, apostrophes, or backslashes. I'm not sure if there are any other characters you would need it for, though. You will use it when you call the submitComment function later.

      $beg = substr($str, 0, $i);
$end = substr($str, $i+1, strlen($str));
$str = $beg . chr(92) . $sub . $end; //chr(92) is the backslash
$i++;
}
}
return $str;
} linenums:0'>function exiter($str) { //makes it so you can insert quotes for($i=0;$i<strlen($str);$i++) { //loops through every character $sub = substr($str, $i, 1); // character at the $i position if($sub=='"' || $sub=="'" || $sub==chr(92)) { //if it has a " or ' or \, it will add a \ before it so it won't think of it as that character. $beg = substr($str, 0, $i); $end = substr($str, $i+1, strlen($str)); $str = $beg . chr(92) . $sub . $end; //chr(92) is the backslash $i++; } } return $str;}
Now that you have that, you can call your submitter function.
if ($_POST['name'] && $_POST['comment']) submitComment($db, $pre, $_POST['name'], exiter($_POST['comment']), $connect);  //if there is a name and a comment, it will put them in the database.

Now, you need to display your comments from your table. The my showComments function does the trick. Later, though, you might want to change the appearance of the table. If you know anything about HTML, you should be able to do this easily.
  $sql = "select * from " . $pre . "comments order by id desc";  //Makes it so it shows the comments in reverse order, so it shows
$result = mysql_query($sql, $connect); //the most recent at the top.
$numrows = mysql_numrows($result);
echo "<table>"; //starts the table, change it's properties however you want.
for ($y=0;$y<$numrows;$y++) { //loops through every row
echo "<tr>";
for ($x=0;$x<$numcols;$x++) { //loops through every column
if($x) echo "<td>" . mysql_result($result, $y, $colnames[$x]) . "</td>"; //The "if" makes it so it doesn't show the row's ID
}
echo "</tr>";
}
echo "</table>";
} linenums:0'>function showComments($db, $pre, $connect) { mysql_select_db($db); $numcols = 3; //id, name, comment $colnames=array(0=>'id',1=>'name',2=>'comment'); //creates an array to use later to read from the database. $sql = "select * from " . $pre . "comments order by id desc"; //Makes it so it shows the comments in reverse order, so it shows $result = mysql_query($sql, $connect); //the most recent at the top. $numrows = mysql_numrows($result); echo "<table>"; //starts the table, change it's properties however you want. for ($y=0;$y<$numrows;$y++) { //loops through every row echo "<tr>"; for ($x=0;$x<$numcols;$x++) { //loops through every column if($x) echo "<td>" . mysql_result($result, $y, $colnames[$x]) . "</td>"; //The "if" makes it so it doesn't show the row's ID } echo "</tr>"; } echo "</table>";}Don't worry, if you don't already have the table, it won't show anything. If it does for you, let me know.

Now, you only need to call the function.
showComments($db, $pre, $connect);
You don't need an if statement for this, because it should show the previous comments no matter what.

Here is the entire code: (not counting config.php)
<form method='post'><h3>Name:</h3><br><input type='text' name='name'><br><h3>Comment:</h3><textarea name='comment'rows="10" cols="50" wrap='hard'></textarea><input type='submit' value='submit'></form><? require_once('config.php');function exiter($str) {               //makes it so you can insert quotes  for($i=0;$i<strlen($str);$i++) {    $sub = substr($str, $i, 1);    if($sub=='"' || $sub=="'" || $sub==chr(92)) {      $beg = substr($str, 0, $i);      $end = substr($str, $i+1, strlen($str));      $str = $beg . chr(92) . $sub . $end;  //chr(92) is the backslash      $i++;    }  }  return $str;}function submitComment($db, $pre, $name, $com, $connect) {  mysql_select_db($db);  $sql = 'CREATE TABLE IF NOT EXISTS ' . $pre . 'comments (id int auto_increment, name text, comment text, primary Key(id));';  mysql_query($sql, $connect);  $sql = 'INSERT INTO ' . $pre . 'comments (name, comment) VALUES ("' . $name . '", "' . $com . '");';  mysql_query($sql, $connect);}function showComments($db, $pre, $connect) {  mysql_select_db($db);  $numcols = 3;  //id, name, comment  $colnames=array(0=>'id',1=>'name',2=>'comment');  $sql = "select * from " . $pre . "comments order by id desc";  $result = mysql_query($sql, $connect);  $numrows = mysql_numrows($result);  echo "<table>";  for ($y=0;$y<$numrows;$y++) {    echo "<tr>";    for ($x=0;$x<$numcols;$x++) {      if($x) echo "<td>" . mysql_result($result, $y, $colnames[$x]) . "</td>";    }    echo "</tr>";  }  echo "</table>";}if ($_POST['name'] && $_POST['comment']) submitComment($db, $pre, $_POST['name'], exiter($_POST['comment']), $connect);showComments($db, $pre, $connect); //Whenever you want to delete the comments, just do a drop table on your PHPmyAdmin.//I'll probably figure out how to delete them depending on when they were submitted.?>

If this script doesn't work, you might need to change the first "<?" to "<?php". I've configured my PHP to work either way.
If you have any suggestions, or know a way to make it delete old comments, please let me know.

Don't read past this if you don't care how I learned this(it's kind of boring, and I blabber(<--funny word :( ) a lot)...

I'm new to PHP, HTML, and CSS, so if anyone knows of any good tutorials, please help.
It took me around two hours to get this to work because I didn't know exactly what I was doing. I learned most of it as I made the page, like I just found out what an exitor was, so it took like 20 minutes to figure out why it wouldn't insert anything with quotes or anything; so when I found that out, I made up a function to get it to work anyway. Trying to get my "exiter" function to work is when I learned about PHP string handling.
I also tried to make a version of this that works with text files, so I learned a lot about file handling, too; but I found out that it wasn't efficient enough that way, and you can end up with a lot of errors if there is more than one person at a time, and it's really slow.
Sorry if I'm starting to annoy you. I'll just shut up. :unsure:
Edited by hippiman (see edit history)

Share this post


Link to post
Share on other sites

Perhaps you could add a sample page to the Tutorial posting so others can see how it works???You have covered some good ground here. Lots of functions and programming design tips there. I'll test it out later.

Share this post


Link to post
Share on other sites

Yes, A sample page would be great. But I can see that your tutorial must of taken ages. Well, well done it looks good so far. I might try it out later. Again, well done.

Share this post


Link to post
Share on other sites

Perhaps you could add a sample page to the Tutorial posting so others can see how it works???
You have covered some good ground here. Lots of functions and programming design tips there. I'll test it out later.

Yes, A sample page would be great. But I can see that your tutorial must of taken ages. Well, well done it looks good so far. I might try it out later. Again, well done.

Share this post


Link to post
Share on other sites

My "exiter" function that I had before was kind of wrong.I've just found out that "chr(13)" is the new line character for the textarea, so I edited my old function, so now it looks like this:

function exiter($str) {               //makes it so you can insert quotes  for($i=0;$i<strlen($str);$i++) {    $sub = substr($str, $i, 1);    if($sub=='"' || $sub=="'" || $sub==chr(92) || $sub==chr(13)) {      $beg = substr($str, 0, $i);      $end = substr($str, $i+1, strlen($str));      if($sub==chr(13)) $str = $beg . '<br>' . $end;        //changes it to an HTML break      else $str = $beg . chr(92) . $sub . $end;  //chr(92) is the backslash      $i++;    }  }  return $str;}

Also, you might have problems depending on if you use the $sub=="'" because it depends on what character you were using as quotes in your INSERT statement. If it adds extra slashes to your comments, just take one of those statements out.EX: if($sub==' " ' || $sub==chr(92) || $sub==chr(13)) { //took out the one that said " ' "(other kind of quote)I'm also planning on making a tutorial on making a login page, but if there's another one that says everything I would, I'll just leave it alone, and if I don't ever get around to it, sorry(I'm probably not that good at explaining this anyway).

Share this post


Link to post
Share on other sites

Darn. Somebody is always using MySQL for comments, and lot's of other stuff. Well i'll write a Flat File script for those things so you guys will see..Flat file rules :unsure: j/k

Share this post


Link to post
Share on other sites

Darn. Somebody is always using MySQL for comments, and lot's of other stuff. Well i'll write a Flat File script for those things so you guys will see..Flat file rules :unsure: j/k



I once made a javascript/php chat in just a text file. After a while as I was adding more stuff like edit, and timestamps, the file started to mess up. Whenever somebody would edit, the chat logs php would have to escape out all the quotes and backslashes. One day i looked at the logs and it was something like:

Alex: heh and then I said \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"people are cool\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"Will: what do you mean by that? isn\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'t that from a movie somewhere?

by the way, i just made up those two lines, I never said such thing! But thats not the point, anyway I had alot more control when every message was in sql. In a text file, i had to parse the file for every line every time I checked for stuff like bb code etc...

Also, if a user changed his or her username, in a text file it wouldn't update, unless you kept their id in the chat text and replaced it on getting the data, which would be another big hassle.

All in all, SQL RULES!

Share this post


Link to post
Share on other sites

hey can we create some database program in javascript ?bcoz our teacher gives us the assignment on that ??and my answer was simple there that no ... is that possible a database in Javascript ?

Share this post


Link to post
Share on other sites

I don't think so. Pure Javascript can't connect to database (i might be wrong), but instead Ajax is used to do that. Asynchronous Javascript and Xml technology, that has advantage like xhttp request or something. You can check both on

 

w3schools(Javascript, Ajax), or Wikipedia links(Javascript, Ajax)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.