making a page to authenticate comments

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Wilbo
Forum Newbie
Posts: 22
Joined: Fri Jul 25, 2008 5:45 am

making a page to authenticate comments

Post by Wilbo »

Hi there,

I have a site where users can add comments about news articles.
The comments can't be seen until they have been authenticated by the administrator.

So I need to make a page where the administrator can autheniticate the comments.
I want to have all the un-authenticated comments listed with a check box next to each one.
The administrator checks the box for all the comments that are ok and then clicks on the submit button at the bottom of the list and the authenticated field of the 'comments' table is changed to 'Y' (it's an ENUM).

Anyone know of any articles which tell the best way to do this?
Or could someone tell me how to do it?

Thanks in advance.
User avatar
ghurtado
Forum Contributor
Posts: 334
Joined: Wed Jul 23, 2008 12:19 pm

Re: making a page to authenticate comments

Post by ghurtado »

I think the best way to do it is to collect all the values from the form (the checkboxes that represent the comments to be approved), then run an update SQL statement which changes the column from N to Y.

Give it a try.
User avatar
idevlin
Forum Commoner
Posts: 78
Joined: Tue Jun 26, 2007 1:10 pm
Location: Cambridge, UK

Re: making a page to authenticate comments

Post by idevlin »

If you already have the code that allows people to enter comments etc. then you're almost there. All you need to do is what ghurtado has said. Load up the comment details for each comment, with a checkbox next to it, and if the checkbox is clicked then when the "admin" clicks an update button, the "authenticated" field value is changed to "Y" and thus is displayed on whatever view comments page you have.

You might also want to add the ability to delete comments.
Wilbo
Forum Newbie
Posts: 22
Joined: Fri Jul 25, 2008 5:45 am

Re: making a page to authenticate comments

Post by Wilbo »

Yes but how do I pass the ids for the selected comments to the query?
I guess I need to put them into an array but I'm not sure how to do it.

Any help would be appreciated!
Could anyone post some example code?
Wilbo
Forum Newbie
Posts: 22
Joined: Fri Jul 25, 2008 5:45 am

Re: making a page to authenticate comments

Post by Wilbo »

a checkbox has name and value fields right?
Should I put the comment id into one of these?

And what would the UPDATE query look like?
User avatar
ghurtado
Forum Contributor
Posts: 334
Joined: Wed Jul 23, 2008 12:19 pm

Re: making a page to authenticate comments

Post by ghurtado »

Name your checkbox something like:

Code: Select all

<input type="text" name="options[]">
...and PHP will automatically turn it into an array with:

Code: Select all

$_POST['options']
Wilbo
Forum Newbie
Posts: 22
Joined: Fri Jul 25, 2008 5:45 am

Re: making a page to authenticate comments

Post by Wilbo »

OK, I worked it out. Here's the solution:

The first thing you should do is run a query to get the records you want to display. Use a while loop to display the records.
For each record you want to add a checkbox:

<input type="checkbox" name="ckbox[]" value="'.$row['comments_id'].'" />

ok so by putting an array as the name, it allows you to have the same name for all the checkboxes but have a different value for each one, the value being stored in the array.

OK so thats all good.

Now, in the 'if submitted' section you want to put the following:

if (isset($_POST['ckbox'])) {
$ckbox = $_POST['ckbox'];
} else {
$ckbox = array();
}

why do we need this? Well, if none of the checkboxes are checked, then the array won't be created and we'll get an error when we try to run the next bit of code, so if we have no checkboxes checked we need to create the array, which will just be empty.

So far so good.

So, all we have to do now is to run an update query for each item stored in the ckbox array.
We do this with a foreach loop:

foreach ($ckbox as $commentId) {

$query = "UPDATE comments SET authenicated='Y' WHERE comments_id=$commentId";

$result = mysql_query($query);

if (!$result) {
echo '<p>Error authorising comment</p>';
}

}

And there you have it.

You can add another checkbox called something else and do the same for deleting comments.

Peace
Post Reply