Jump to content

Php comments on news


Blaze

Recommended Posts

Alright, i'll try to explain myself the best i can. I'm spanish, my english isn't great. So please... bear with me. Thanks in advance to anyone that tries to help.I was learning php, and trying to add a "Add a comment" for the news section on my site. I managed to make the add a comment page, and the view comments page. Now, here is the issue.For example, i have two news on the frontpage, both with different id (on the mysql table). But whenever i click the view comments page, it shows the comments for BOTH. That's because I know i should use a WHERE clause on the mysql command for loading the comments, based on news id. My issue here is,I don't know how to make the "add a comment" page have the news id added dinamically so that it can be read later only if the same id is on the comments. Im not sure if i make myself clear...http://www.dfblaze.net/index2.phpGo there, and try to add a comment to the FIRST news post. Then view the comments for the second one, and you'll see the same comments. I hope i made myself clear now. :)Thanks to anyone, again. I've been struggling with this all day.

  • Like 1
Link to comment
Share on other sites

You will need a column in your comments table to tell which news story it goes with. So, your comments table needs a field like "news_id". Your links to add a comment will have to include the ID of the news story on the link, like this:add_comment.php?id=7Or whatever the ID is, when you are displaying information about the news from the database, include the ID in your link. Then, when they click on that link to add the comment, you know which news story they are commenting on. You can get the news ID on the add_comment page using this:$news_id = $_GET['id'];And you will probably want to add it as a hidden input field on your comment form:<input type="hidden" name="news_id" value="<?php echo $news_id; ?>">Then, when they submit their comment, get the news_id field from the $_POST data, and include that in your SQL query to insert the comment. You will need to do the same thing on the view_comments page, like this:view_comments.php?id=7And then:

$news_id = intval($_GET['id']);$result = mysql_query("SELECT * FROM comments WHERE news_id={$news_id}");

That way you only get the comments for the news story they clicked on.Does that make sense?

Link to comment
Share on other sites

You will need a column in your comments table to tell which news story it goes with. So, your comments table needs a field like "news_id". Your links to add a comment will have to include the ID of the news story on the link, like this:add_comment.php?id=7
I know that I need the link to show up like that, the thing is, I don't want to do it by opening each page and giving it an id.I was thinking along the lines of when clicked on the "add a comment" part, it would somehow read the comment's id and then have the id as a variable on the typing part, so that i can use that and add it to the comment's mysql table as a news_id or something similar. The thing is i dont know how to do that...
Or whatever the ID is, when you are displaying information about the news from the database, include the ID in your link. Then, when they click on that link to add the comment, you know which news story they are commenting on. You can get the news ID on the add_comment page using this:$news_id = $_GET['id'];
This gives me an idea. I'm adding the "Add a comment-view comments" button via php, right after the news are posted. So i assume i can make the link using POST variables? like:
$id=$_POST(id);echo '<a href='addcomments.php?[b]id=$id[/b]'>Add a comment</a> - <a href='viewcomments.php?[b]id=$id[/b]>View Comments</a>';

But its the bold part what im not sure of (the id=$id).

And you will probably want to add it as a hidden input field on your comment form:<input type="hidden" name="news_id" value="<?php echo $news_id; ?>">
Wait a second, what did you just did there? Make an invisible input with the value of the old id, what for? I lost myself there.
$news_id = intval($_GET['id']);$result = mysql_query("SELECT * FROM comments WHERE news_id={$news_id}");

Lastly, what does that intval mean? interval or is it one of mysql data types?Thanks a lot for your help.

Link to comment
Share on other sites

I know that I need the link to show up like that, the thing is, I don't want to do it by opening each page and giving it an id.
You don't have to do it manually. You are connecting to the database to get all of the news stories and display them, right? The news stories need an id field, so when you are looping through the result from MySQL, you just include the id onto the link.
$result = mysql_query("SELECT * FROM news"); // or whateverwhile ($row = mysql_fetch_array($result)){  //display news item  ...  echo "<a href=\"add_comment.php?id={$row['id']}\">Add comment</a> - <a href=\"view_comments.php?id={$row['id']}\">View comments</a>";}

Wait a second, what did you just did there? Make an invisible input with the value of the old id, what for? I lost myself there.
<input type="hidden"> makes an input field that doesn't show in the form. You can use it to pass information through $_POST that you don't want the user to see or change (they can still see it in the code though). I had it write the ID it got from $_GET into a hidden element, so that when they submit the Add Comment form you will have the ID of the news story ready to add to the database.
Lastly, what does that intval mean? interval or is it one of mysql data types?
The intval function gets the integer value, so it converts to a number. It's just a security thing, you don't want someone typing some SQL code into the id variable:add_comment.php?id=DELETE%20FROM%20 ...and then you put the value into your SQL statement and execute their code. So, intval converts it to a number so you can be sure that you are only giving a number for the ID.
Link to comment
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
×
×
  • Create New...