Top

Review & Score Editor


PHP Code
+ 0 likes
Please Register to submit score.
Bookmark and Share
Average Score  10.0 (of 5 scores)
Date Added  Aug 24, 2005
Last Updated  Mar 24, 2007
Tags  editor  mysql  php  review  score 

Introduction

This is a stand alone example of a review system. This is the administration page for entering and editing reviews. It'll create your table for you automatically, so all you need to do is change the database settings. You may extract the database code and put it into a file called db.php. You can also extract the get_reviews() function and put it into a file called reviews.php. Just make sure you include() both files for this to work. This does not support front end displaying, but it can easily be modified to do so if you split the files up. Give it a try as-is on your local or hosted PHP account.

Screenshot


Grab the Code

<?php
 
/********************************************************************************/
// DB connection and table.  This can be moved to a separate file. If you do that
// You'll need to add this line:
//
// include('db.php'); 
//
 
/* db.php */
 
$mysql_server = 'localhost';
$mysql_user = 'root';
$mysql_pass = '';
$mysql_db = 'test';
 
$dbcon = mysql_connect($mysql_server, $mysql_user, $mysql_pass);
 
if(!$dbcon)
{
   print 'Could not connect to mysql';
   exit;
}
 
if(!mysql_select_db($mysql_db, $dbcon))
{
   print 'Could not select database';
   exit;
}
 
$query = "
CREATE TABLE IF NOT EXISTS `reviews` (
  `review_id` int(11) NOT NULL auto_increment,
  `datetime` datetime NOT NULL default '0000-00-00 00:00:00',
  `timestamp` timestamp(14) NOT NULL,
  `review_title` varchar(250) NOT NULL default '',
  `review_body` text NOT NULL,
  `review_score` int(11) NOT NULL default '0',
  PRIMARY KEY  (`review_id`),
  KEY `review_id` (`review_id`),
  FULLTEXT KEY `review` (`review_title`,`review_body`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;
";
 
mysql_query($query);
 
/* db.php end */
 
/********************************************************************************/
// Gets a review from the db based on it's review_id
// This function should be put into it's own file called reviews.php
//
// include('reviews.php');
//
 
function get_reviews($review_id = '0')
{
	global $dbcon;
 
	if($review_id)
	{
		$where = "where review_id = '$review_id'";
	}
 
	$query = "
		select
			*
		from 
			reviews
		$where	
		order by
			datetime desc
	"; 
 
	$result = mysql_query($query, $dbcon);
	$rows = mysql_num_rows($result);
 
	$reviews = array();
	if($rows > 0)
	{
		while($row = mysql_fetch_assoc($result))
		{
			if($review_id)
			{		
				return $row;
			}	
			else
			{
				$reviews[$row[review_id]] = $row;
			}
		}	
	}
 
	return $reviews;
}
 
/********************************************************************************/
// Collect variables from the URL and POST variables
 
$review_id = $_POST[review_id];
if(!$review_id) 
{
	$review_id = $_GET[review_id];
}
 
$op = $_POST[op];
 
/********************************************************************************/
// Saving the new or edited review to the database.
 
switch($op)
{
	case 'save':
		$review_title = addslashes(stripslashes($_POST[review_title]));
		$review_score = addslashes(stripslashes($_POST[review_score]));
		$review_body = addslashes(stripslashes($_POST[review_body]));
 
		$set = "
			timestamp = now(),
			review_title = "$review_title",
			review_score = "$review_score",
			review_body = "$review_body"
		";	
 
		if($review_id)
		{
			$query = "update reviews set $set where review_id = '$review_id'";
		}
		else
		{
			$query = "insert into reviews set datetime = now(), $set";
		}
 
		mysql_query($query);
}
 
/********************************************************************************/
// Include or print your header here
 
?>
<html>
<head>
<title>Article Editor</title>
 
<style type="text/css">
<!--
 
td, body { font-family: Arial; font-size: 14px; color: #000000; text-decoration: none; font-weight: none}
.white { font-family: Arial; font-size: 14px; color: #FFFFFF; text-decoration: none; font-weight: bold}
a.header:link {  font-family: Verdana, Arial, Helvetica, Sans-serif; font-size: 13px; color: #FFFFFF; text-decoration: none; font-weight: bold}
a.header:visited {  font-family: Verdana, Arial, Helvetica, Sans-serif; font-size: 13px; color: #FFFFFF; text-decoration: none; font-weight: bolder}
a.header:hover {  font-family: Verdana, Arial, Helvetica, Sans-serif; font-size: 13px; color: #FFFFFF; text-decoration: underline; font-weight: bolder}
 
a:link {  font-family: Verdana, Arial, Helvetica, Sans-serif; font-size: 13px; color: #000088; text-decoration: none; font-weight: bolder}
a:visited {  font-family: Verdana, Arial, Helvetica, Sans-serif; font-size: 13px; color: #000088; text-decoration: none; font-weight: bolder}
a:hover {  font-family: Verdana, Arial, Helvetica, Sans-serif; font-size: 13px; color: #000000; text-decoration: none; font-weight: bolder}
 
-->
</style>
</head>
 
<body bgcolor="#FFFFFF">
<h3><a href='?'>Write new Review</a></h3>
 
<?php
 
/********************************************************************************/
// If we have a review_id, only show that particular review and it's edit form.
// If there is no specific review_id, show all of the reviews.
 
$reviews = get_reviews();
 
if(count($reviews) > 0)
{
	foreach($reviews as $r_id => $review)
	{
		$review_title = htmlentities($review[review_title], ENT_QUOTES);
		$review_score = $review[review_score];
		$review_body = $review[review_body];
		$datetime = $review[datetime];
 
		list($date, $time) = split(" ", $datetime);
		list($year, $month, $day) = split("-", $date);
        $date = date('M d, Y', mktime(0, 0, 0, $month, $day, $year));
?>
 
<table width=600 cellpadding=5 cellspacing=0 border=0>
<tr bgcolor="#FFFFFF">
	<td align=left valign=top width=100><?php print $date; ?></td>
	<td align=left valign=top><a href='<?php print "$_SELF?review_id=$r_id" ?>'><?php print $review_title; ?></a></td>
</tr>	
</table>
 
<?php
	}
}	
 
if($review_id)
{
	$review = get_reviews($review_id);
	$review_title = htmlentities($review[review_title], ENT_QUOTES);
	$review_score = $review[review_score];
	$review_body = $review[review_body];
?>
 
<!-- This is the html you use to display a review -->
<br><br>
<table width=600 bgcolor='#000000' cellpadding=8 cellspacing=1 border=0>
<tr bgcolor="#FFFFFF">
	<td align=left>
	<b><?php print $date; ?></b><br>
	<?php print $review_title; ?><br><br>
	<?php print $review_body; ?><br><br>
	</td>
</tr>
</table>
 
<?php
}	
else
{
	unset($review_title);
	unset($review_score);
	unset($review_body);
}
 
 
?>
 
<!-- This is the html you use to edit a review -->
<br><br>
<form method=post action='<?php print $_SELF; ?>'>
<input type='hidden' name='op' value='save'>
<input type='hidden' name='review_id' value='<?php print $review_id; ?>'>
<table width=600 cellpadding=0 cellspacing=1 bgcolor=#000000>
<tr>
	<td align=left><table width=100% cellpadding=8 cellspacing=0 border=0>
	<tr bgcolor="#FFFFFF">
		<td align=right valign=top width=120>Review Score</td>
		<td align=left valign=top><input type=text size=5 maxlength=25 name='review_score' value='<?php print $review_score; ?>'></td>
	</tr>	
	<tr bgcolor="#FFFFFF">
		<td align=right valign=top>Review Title</td>
		<td align=left valign=top><input type=text size=30 maxlength=250 name='review_title' value='<?php print $review_title; ?>'></td>
	</tr>	
	<tr bgcolor="#FFFFFF">
		<td align=right valign=top>Review Body</td>
		<td align=left valign=top><textarea cols=60 rows=15 name='review_body'><?php print $review_body; ?></textarea></td>
	</tr>	
	<tr bgcolor="#FFFFFF">
		<td align=right valign=top colspan=2><input type=submit name=submit value='Save'></td>
	</tr>	
	</table></td>
</tr>
</table>
</form>
 
<?php
 
mysql_close($dbcon);
 
?>
 
</HTML>
 

Comments

  (7)  RSS
Lindrian
Comments: 761
 
PHP Snippet:  Review & Score Editor
Posted on Mar 24, 2007 5:49 pm
What about adding a function so your not able to input a number higher then 20? (Havent tried it, so i dont know if u already got that in there)
Hawkee
Comments: 1,039
 
PHP Snippet:  Review & Score Editor
Posted on Mar 24, 2007 5:53 pm
The limit is up to you. I didn't impose any sort of restrictions as this is just a bare bones template for a review system.
Lindrian
Comments: 761
 
PHP Snippet:  Review & Score Editor
Posted on Mar 24, 2007 7:40 pm
alright, i was just wondering, excellent work.
Abbas
Comments: 15
 
PHP Snippet:  Review & Score Editor
Posted on Sep 1, 2007 6:08 am
good work
mountaindew
Comments: 1,826
 
PHP Snippet:  Review & Score Editor
Posted on Sep 7, 2007 7:05 pm
ive never scripted php, but it looks good :P

did you code hawkee.com or did u get it from somewhere else?
mountaindew
Comments: 1,826
 
PHP Snippet:  Review & Score Editor
Posted on Sep 7, 2007 7:06 pm
like adding and reviewing snippets
Hawkee
Comments: 1,039
 
PHP Snippet:  Review & Score Editor
Posted on Sep 11, 2007 2:21 am
Coded everything except the forum myself.

Commenting Options

Register or Login to Hawkee.com or use your Facebook or Twitter account by clicking the corresponding button below.

  
Bottom