Hjælp til kommentarsystem
Hej alle sammenJeg har fundet et kommentarsystem i PHP, så de besøgende kan skrive kommentarer.
Der står, at man kan "show all or only confirmed comments", og at det understøtter forskellige kategorier. Udover det står der ikke mere.
Er der nogen, der kan se, hvordan man får disse to funktioner til at virke? Jeg vil gerne have at alle kommentarer skal godkendes, og at den kun viser og skriver kommentarer i kategorien "eksperten".
På forhånd tak og god jul :)
---------- MySql setup ----------
CREATE TABLE `guestbook` (
`id` int(11) NOT NULL auto_increment,
`category` int(11) NOT NULL default '1',
`confirmed` tinyint(1) NOT NULL default '0',
`stamp` datetime default NULL,
`who` varchar(32) NOT NULL,
`email` varchar(128) NOT NULL,
`msg` text NOT NULL,
PRIMARY KEY (`id`),
KEY `category` (`category`),
KEY `confirmed` (`confirmed`)
) ENGINE=MyISAM;
---------- class.guestbook.php ----------
<?
/***************************************
* Script: guestbook
* Author: Indrek Päri @ indrek.eu
* Website: www.indrek.eu
***************************************/
class guestbook{
var $category;
var $confirmed;
var $db;
function guestbook($category=0,$confirmed=0){
$this->category = (int)$category;
$this->confirmed = (int)$confirmed;
// mysql connection
$this->db = mysql_connect('localhost', 'username', 'password') or die('Could not connect: ' . mysql_error());
mysql_select_db('indrek',$this->db) or die('Could not select database');
}
function gimmi_rows(){
if($this->confirmed==1) $sql_where = " AND confirmed = ".$this->confirmed;
// select entrys last in first order
$sql = "select * from guestbook where category = ".$this->category.$sql_where." order by id desc";
$result = mysql_query($sql,$this->db);
$numrows = mysql_num_rows($result);
while($row = mysql_fetch_object($result)){
$back .= $this->html_row($row->who,$row->stamp,$row->msg);
}
return $back;
}
function html_row($who, $stamp, $msg){
return '
<div style="margin-top:20px;"><b>'.$who.'</b> @ '.$stamp.'</div>
<div style="margin-top:5px;">'.$msg.'</div>
';
}
function make_insert($who, $email, $msg){
$sql = sprintf("INSERT INTO `guestbook` (`id`,`category`,`confirmed`,`stamp`,`who`,`email`,`msg`) VALUES ('','%d','0',NOW(),'%s','%s','%s')",
$this->category,
mysql_escape_string($who),
mysql_escape_string($email),
mysql_escape_string($msg)
);
mysql_query($sql,$this->db);
return;
}
function html_add(){
if(is_array($_POST['uform'])){
if($_POST['uform']['name'] AND$_POST['uform']['text']){
$this->make_insert($_POST['uform']['name'],$_POST['uform']['email'],$_POST['uform']['text']);
}
return '<strong>Thank you for taking the time to send us your comments!</strong>';
}else{
return '
<form method="post">
<div style="margin-top:20px;"><b>Add new entry</b></div>
<table>
<tr>
<td>*Your name</td>
<td><input size="25" type="text" name="uform[name]"></td>
</tr>
<tr>
<td>Your email</td>
<td><input size="35" type="text" name="uform[email]"></td>
</tr>
<tr>
<td>*Text</td>
<td><textarea cols="40" rows="5" name="uform[text]"></textarea></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="submit"></td>
</tr>
</table>
</form>
';
}
}
}
?>
---------- Usage ----------
<?php
/***************************************
* Script: guestbook
* Author: Indrek Päri @ indrek.eu
* Website: www.indrek.eu
***************************************/
include('class.guestbook.php');
// category;
// show all or only confirmed comments
$gb = new guestbook(0,0);
echo $gb->html_add();
echo $gb->gimmi_rows();
?>