<?php

class Comment {

	function addcomment($arr)
		{
			if (count($arr) > 0) 
				{
					$sql = sprintf("insert into comment 
									(loc_id, user_id, text, created_date)
									values ('%s', '%s', '%s', '%s')",
									$arr[0], $arr[1], $arr[2], $arr[3]);
					if (mysql_query($sql))
						return true;
					else
						return "Error: " . mysql_error();
						 
				}
			else
				{
					return "Error: Please enter the required field values.";
				}
		}
		
	function showcomment($loc_id)
		{
			$sql = sprintf("SELECT comment.*, CONCAT(user.first_name,' ',user.last_name) as user_name 
							FROM comment, user 
							where comment.user_id = user.user_id and comment.loc_id = '%s'
							order by comment.comment_id desc", 
							$loc_id);
							//print $sql;exit;
			$res = mysql_query($sql);
			//return mysql_fetch_array($res);
			return $res;
		}		
		
	function getcommentbyloc_id($loc_id)
		{
			$com='';
		//	$sql = sprintf("select * from comment where loc_id= '%s' limit 10", $loc_id);
			$sql = sprintf("SELECT comment.*, CONCAT(user.first_name,' ',user.last_name) as user_name 
							FROM comment, user 
							where comment.user_id = user.user_id and comment.loc_id = '%s' 
							order by comment.comment_id desc limit 10", 
							$loc_id);

			$res = mysql_query($sql);
			$com = '';

			while($row=mysql_fetch_array($res))
			{
				if ($com != '') $com .= "<tr><td colspan=2>&nbsp;</td></tr>";
				$com .= "<tr bgcolor=#f5f5f5>";
					$com .= "<td><b>Author: </b>".$row['user_name']."</td>";
					$com .= "<td align=right><b>Posted on: </b>".$row['created_date']."</td>";
				$com .= "</tr>";
				$com .= "<tr>";
					$com .= "<td colspan=2><b>Message: </b>".nl2br($row['text'])."</td>";
				$com .= "</tr>";
			}
			$com = ($com != '') ? $com : "<tr><td><font color=#ff0000>No Comments Found.</font></td></tr>";
			$com = "<table cellpadding=2 cellspacing=0 width=100%>".$com."</table>";
			return htmlspecialchars($com);
		}			
}
	?>