Jump to content

Recommended Posts

Does any one know where a good tutorial is on commenting systems. I cant seem to find a good tutorial for what i want to do. Like i want people to be able to comment on each individual post i make on my blog or i may just make a new single page for people to comment but only people that have register on the web site.

 

Thank you,

Dominic

Link to comment
https://forums.phpfreaks.com/topic/244467-php-commenting-system/
Share on other sites

yeah I mostly understand, or at least I think I do.

or like if I follow a tutorial on making a comments page, is there a way I can impliment my login script into it so you must be logged in to post?

 

Sorry if im not making sense when I ask questions. I'm fairly new and really dont know what all everything does, as you could probably tell from all my threads. lol.

is there a way I can impliment my login script into it so you must be logged in to post?

 

Of course, if you understand how both systems work.

 

What I'm trying to get at is how to think like a programmer instead of spoon feeding yourself through tutorials.

 

Allot of people doing tutorials are in a rush to get things working so they tend to skim past the explinations of why they are typing the crap they are typing. Even if people do pay attention and read this stuff allot of tutorials may not be particularly well written and not explain allot of stuff anyway.

 

Do yourself a favor and try to break the task down yourself. Once you have the task broken down, you simply need to go through each step and create the code. SOme steps may need to be broken down even further along the way.

 

As an example, your comment system. What does it need to do?

 

[pre]

1) If a user is logged in, display a button at the bottom of the each blog post.

2) Once button is clicked, open a text area for the user to post there comment in.

3) Submit the form to the database and add a new record.

[/pre]

 

Obviously, this isnt enough information to start coding (for some, it would be as the missing pieces are pretty clear already).

 

So, let's go through the list again and ask ourselves some questions and/or jot down idea..

 

[pre]

1) If a user is logged in, display a button at the bottom of the each blog post.

  *) How exactly do we check a user is logged in?

  *) We can check the $_SESSION array for the flag we added to it when we logged a user in.

 

2) Once button is clicked, open a text area for the user to post there comment in.

  *) We can likely take the user to a new page to do this.

  *) Or, get a little bit fancy and implement it using JavaScript to slide open an already existing text area.

 

3) Submit the form to the database and add a new record.

  *) How can we relate this comment to the article?

  *) Each article has an id in the database. When we create the article (and the optional comment button) we can place this id in a hidden form element. This way it will get sent along with the comment.

 

4) Database structure:

  *) Now that we know we will be relating comments to an article via the articles id we can design a simple database.

  *) What fields to store?

  *) A comment id, and article id and maybe the users id so we can put there name on the comment.

  *) How do I get the users id?

  *) It should likely be in that $_SESSION array I am already using to see if the user is logged in.

[/pre]

 

Now we are getting much closer to something that can actually be turned into code.

 

From here, you might go through it again and add in a few more ideas / thoughts. You might need to go and add some stuff to your login system to account for the data your going to need.

 

With this kind of plan in place you don't really need any specific tutorials. If you get stuck, you can come to a forum and say "Hey, Iv'e got this login script, how do I add a user id to the $_SESSION array?"

 

Better still, if you've gone through a similar process to create your login script, you likely won't need to ask those types of simple questions because you will understand completely how this system works, why and how it does what it does.

 

Hope this helps.

I'm not usually trying to be nasty when I say things like "go break your problem down into smaller pieces and come back when you have a more specific problem", I'm trying to get people to think for themselves.

   

Most things aren't as difficult as they seem once they are broken down into smaller pieces.

   

The company I work for is in the process of putting in a tender for a Government contracted application. We got a 'functional requirement outline' on Monday which is basically the same sort of thing. Ours (unfortunately) however for this particular project is 257 A4 pages long.

yeah i get what your saying, i like being able to figure stuff out for my self lol

 

but yeah i got the commenting part to where it displays the messages and everything now i have to figure out how to make it to where you have to be loged in to access it lol

okay so i got all this, just by looking at my other php files and i figured this is what it should look like to require the loggin to access it. But it does not work. Do you see what im missing or doing wrong?

 

 

<?php require("top.php"); ?>
<div id='comments'>
<?php

$getid = $_GET['id'];
if(!$getid)
$getid = "1";

require("scripts/connect.php");
$query = mysql_query("SELECT * FROM users WHERE id='$getid'");
$numrows = mysql_num_rows($query);

if($numrows == 1){
require ("scripts/comment_connect.php");
$getdata = mysql_query ("SELECT * FROM comments ORDER BY date AND time ASC");
while($row = mysql_fetch_assoc($getdata)){
$id = $row['id'];
$name = $row['name'];
$message = $row['message'];
$date = $row['date'];
$time = $row['time'];

$message = nl2br($message);

echo"
<table>
	<tr>
	<td><b>$name on $date at $time</b></td>
	</tr>
	<tr>
	<td>$message</td>
	</tr>
</table>
<br />
<hr />";
}

$button = $_POST['button'];

if($button){
$name = $_POST['name'];
$message = $_POST['message'];
if ($name && $message){
	$dayofweek = date("l");
	$month = date("F");
	$dayofmonth = date("d");
	$year = date("Y");

	$date = "$dayofweek $month $dayofmonth, $year";

	$hours = date("g");
	$min = date("i");
	$sec = date("s");
	$amorpm = date("a");
	$timezone = date("T");
	$time = "$hours:$min:$sec $amorpm $timezone";


	require ("scripts/comment_connect.php");
	$queryget = mysql_query("SELECT * FROM comments WHERE name='$name' AND message='$message'");
	$numrows = mysql_num_rows($queryget);
	if($numrows != 1){
	$query = mysql_query("INSERT INTO comments VALUES ('', '$name', '$message', '$date', '$time')");
	echo "Your message has been sent!"; 
	}	
	else
		echo "You can NOT resubmit the same message!";


}

else 	
	echo "You did not fill in all fields.";
}
echo "
<form action='comments.php' method='POST'>
<table width='400'>
<tr>
<td width='100'>Name:</td>
<td><input type='text' name='name'></td>
</tr>
<tr>
<td width='100'>Message:</td>
<td><textarea name='message' cols='50' rows='3'></textarea></td>
</tr>
<tr>
<td width='100'></td>
<td><input type='submit' name='button' value='Submit'</td>
</tr>

</table></form>

";

}
else
	echo "This needs to be changed";
?>
</div>
</body>
</html>

You can check if that variable is set before inserting the comment into the DB:

session_start(); //Make sure you do this at the very top of your script

//Later on in the script
if(!isset($_SESSION['username'])) {
   echo "You must login before commenting";
} else {
  //Comment script or whatever you want protected.
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

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.