Page 1 of 1
making a page to authenticate comments
Posted: Wed Aug 20, 2008 8:16 am
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.
Re: making a page to authenticate comments
Posted: Wed Aug 20, 2008 8:40 am
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.
Re: making a page to authenticate comments
Posted: Wed Aug 20, 2008 8:48 am
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.
Re: making a page to authenticate comments
Posted: Fri Aug 22, 2008 6:03 am
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?
Re: making a page to authenticate comments
Posted: Fri Aug 22, 2008 6:21 am
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?
Re: making a page to authenticate comments
Posted: Fri Aug 22, 2008 7:56 am
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:
Re: making a page to authenticate comments
Posted: Fri Aug 22, 2008 11:04 am
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