Jump to content

Create a Dynamic Comment box for a web page


kamal213

Recommended Posts

Hi guyz,

 

I am looking to create a dymanic comment box for my website where already logged in users can comment on webpages.

 

It is quite a challenge getting to dynamically display only the comments a particular user posted on a web page.

 

What I mean is I would like to be able to click on a page, see only the comments logged users posted on that page, and then post my own comment on that page. At the moment I created a comment box for my webpages but the issue is my posted comments on one particular page is displaying on all web pages with is comment system..This is the problem and it is very difficult to find helpful material hopefully you guyz can come to the rescue.

 

Please guys any help or suggestions are more than welcome.

 

Thanks

 

Kdiamonds

Link to comment
Share on other sites

I created a comment code 2 years ago ... it's a very easy to use code. Heres what i did.

 

comment.php

<?php

/**
* @author Drisate
* @copyright 2009
*/

// Delete comment 
if ($_GET[action]=="del"){
    $add = mysql_query("DELETE FROM comment WHERE id='$_GET[cid]'") or die(mysql_error());
    
print ('<table id="delete" class="tborder" id="post1501" cellspacing="0" cellpadding="6" width="100%" align="center" border="0">
<tr valign="top">
	<td class="alt2" align="middle" width="100%" bgcolor="#1D1D1D">
	<p><b><font color="#FFFFFF">Your comment has been delete!</font></b></p>
	</td>
</tr>
</table><br>');
}

// Add comment
if ($_POST[comment]){
    
$_POST[title] = htmlentities($_POST[title], ENT_QUOTES);
$_POST[message] = htmlentities($_POST[message], ENT_QUOTES);
$date = date("d-m-Y H:i:s");
    
if (!$_POST[title]){$cmessage .= "Please provide a title.<br>";}
if (!$_POST[message]){$cmessage .= "Please provide a message.<br>";}
/*
echo "<pre>";
print_r($_POST);
echo "</pre>";
*/
if (!$cmessage){
    
    if ($_POST[comment]=="Add"){
    $add = mysql_query("INSERT INTO comment (id, placeid, catid, userid, date, title, message) VALUES ('', '$placeid', '$catid', '$bbuserinfo[userid]', '$date', '$_POST[title]', '$_POST[message]')") or die(mysql_error());
    $al = "Your comment was added!";
    unset($_POST);
    unset($_GET[action]);
    }
    
    if ($_POST[comment]=="Modify"){
    $add = mysql_query("UPDATE comment SET title='$_POST[title]', message='$_POST[message]' WHERE id='$_GET[cid]'") or die(mysql_error());
    $al = "Your comment was modifyed!";
    unset($_POST);
    unset($_GET[action]);
    }
    
print ('<table class="tborder" id="post1501" cellspacing="0" cellpadding="6" width="100%" align="center" border="0">
<tr valign="top">
	<td class="alt2" align="middle" width="100%" bgcolor="#1D1D1D">
	<p><b><font color="#FFFFFF">'.$al.'</font></b></p>
	</td>
</tr>
</table><br>');
    
}

}  

// See comments
$count = @current(@mysql_fetch_assoc(@mysql_query("SELECT count(id) FROM comment where placeid='$placeid' and catid='$catid'")));

if ($count){
print ('<table class="tborder" cellspacing="0" cellpadding="6" width="100%" align="center" border="0">
<thead>
	<tr>
		<td class="tcat" align="left" width="100%">Posted comments</td>
	</tr>');
        
$select = mysql_query("SELECT * FROM comment where placeid='$placeid' and catid='$catid'") or die(mysql_error());
while ($thread = mysql_fetch_array($select)) {

unset($tuser); $tuser = @mysql_fetch_assoc(@mysql_query("SELECT * FROM vb_user where userid='$thread[userid]'"));
unset($groupe); $groupe = @mysql_fetch_assoc(@mysql_query("SELECT * FROM vb_usergroup where usergroupid='$thread[usergroupid]'"));
unset($avatar); $avatar = @mysql_fetch_assoc(@mysql_query("SELECT * FROM vb_customavatar where userid='$thread[userid]'"));
       
print ('<tr>
		<td id="comment_'.$thread[id].'" class="thead" align="left" width="100%">
		<table cellspacing="10" cellpadding="10" width="100%" border="0">
			<tr>
					<td style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 12px; font-family: verdana, geneva, lucida, \'lucida grande\', arial, helvetica, sans-serif" width="134" align="left" valign="top" rowspan="2">
					<div id="edit1500" style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 6px; PADDING-TOP: 0px">
						<table class="tborder" id="post1500" cellspacing="0" cellpadding="6" width="100%" align="center" border="0">
							<tr valign="top">
								<td class="alt2" align="middle" width="175">
								<div id="postmenu_1500" state="false" unselectable="true">
									<a class="bigusername" href="http://dramis.info/board/member.php?u='.$thread[userid].'">'.$groupe[opentag].$tuser[username].$groupe[closetag].'</a>
								</div>
								<div class="smallfont">'.$groupe[opentag].$tuser[usertitle].$groupe[closetag].'</b></div>');
                                    
if ($avatar[filename]){                             
print ('
								<div class="smallfont">
 <br>
									<a style="color: #c2bfae" href="http://dramis.info/board/member.php?u='.$thread[userid].'">
									<img title="'.$tuser[username].'\'s Avatar" height="78" alt="'.$tuser[username].'\'s Avatar" src="http://dramis.info/board/image.php?u='.$thread[userid].'&dateline='.$avatar[dateline].'" width="80" border="0" style="border-width: 0px"></a>
								</div>');
}               
                                    
								print ('</td>
							</tr>
						</table>
					</div>
					</td>
					<td style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 12px; font-family: verdana, geneva, lucida, \'lucida grande\', arial, helvetica, sans-serif" align="left" valign="top">
					<font color="#C2BFAE" size="2"><b><u>'.html_entity_decode($thread[title]).'</u><br>
					<br>
					</b></font><font color="#FFFFFF" size="2">'.nl2br(html_entity_decode($thread[message])).'</font></td>
			</tr>');
                
                if ($thread[userid]==$bbuserinfo['userid']){
                
			print ('
			<tr>
					<td style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 12px; font-family: verdana, geneva, lucida, \'lucida grande\', arial, helvetica, sans-serif" align="right" valign="bottom" height="10">
					<b><font color="#C2BFAE"><a href="'.$pagepath.'&action=mod&cid='.$thread[id].'#edit">[Edit]</a> <a href="'.$pagepath.'&action=del&cid='.$thread[id].'#delete">[Delete]</a></font></b></td>
			</tr>');
                
                }
                
		print ('</table>
		</td>
	</tr>
	<tr>
		<td align="left" width="100%" height="1" bgcolor="#3B3B3B"></td>
	</tr>');
    
}

print ('</thead>
</table><br>');
}

// Post comment
if ($bbuserinfo['userid']!=0){
    
if ($cmessage){
    print ('<table class="tborder" id="post1501" cellspacing="0" cellpadding="6" width="100%" align="center" border="0">
<tr valign="top">
	<td class="alt2" align="middle" width="100%" bgcolor="#1D1D1D">
	<p align="left"><font color="#FF0000">OUPS!<br>
'.$cmessage.'</font></p>
	</td>
</tr>
</table><br>');
}

if ($_GET[action]=="mod"){
        $_POST = @mysql_fetch_assoc(@mysql_query("SELECT * FROM comment where id='$_GET[cid]' "));
}else{
    if (!$_POST[title]){
        $_POST[title] = $dtitle;
    }
}
    
print ('<table class="tborder" cellspacing="0" cellpadding="6" width="100%" align="center" border="0">
<thead>
	<tr>
		<td id="edit" class="tcat" align="left" width="100%">'); if ($_GET[action]=="add" or $_GET[action]==""){echo "Post your comment!";}else{echo "Modify your comment!";} print('</td>
	</tr>
	<tr>
		<td class="thead" align="left" width="100%">
		<table cellspacing="10" cellpadding="10" width="100%" border="0">
			<tr>
				<form method="POST" action="'.$pagepath.'&cid='.$_GET[cid].'#comment_'.$_GET[cid].'">
					<td style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 12px; font-family: verdana, geneva, lucida, \'lucida grande\', arial, helvetica, sans-serif">
					<b><font color="#FFFFFF">Subject:<br>
					<input type="text" name="title" style="width:50%" value="'.html_entity_decode($_POST[title]).'"></font></b></td>
			</tr>
		</table>
		<table cellspacing="10" cellpadding="10" width="100%" border="0">
			<tr>
					<td style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 12px; font-family: verdana, geneva, lucida, \'lucida grande\', arial, helvetica, sans-serif">
					<textarea rows="5" name="message" style="width:100%;" cols="20">'.html_entity_decode($_POST[message]).'</textarea><br>
					<br>
					<input type="submit" value="'); if ($_GET[action]=="mod"){echo "Modify";}else{echo "Add";} print('" name="comment">
					<input type="reset" value="Reset" name="B4"> </td>
				</form>
			</tr>
		</table>
		</td>
	</tr>
</thead>
</table>');
    
}else{
    print ('<table class="tborder" id="post1501" cellspacing="0" cellpadding="6" width="100%" align="center" border="0">
<tr valign="top">
	<td class="alt2" align="middle" width="100%" bgcolor="#1D1D1D">
	<p align="left"><font color="#FF0000">You must loggin to post/reply a comment! If you do not have an account you can create one even if your not part of the Dramis Consortium. Fallow this <a href="board/register.php">link</a>.</font></p>
	</td>
</tr>
</table>');
}

?>

 

SQL

CREATE TABLE IF NOT EXISTS `comment` (
  `id` int(11) NOT NULL auto_increment,
  `placeid` varchar(255) NOT NULL,
  `catid` int(11) NOT NULL,
  `userid` int(11) NOT NULL,
  `date` varchar(255) NOT NULL,
  `title` varchar(255) NOT NULL,
  `message` longtext NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 ;

 

and you call the comment systeme where ever you need it like this:

$placeid = "23";
$catid = "news";
$pagepath = "index.php";
$dtitle = "RE: Hello world";
include ("comment.php");

 

$placeid is the ID or the name of the category Ex: News, Download, Article [...] Reason for this is if you use the code in a lot of difrent places you might end up with the same ID for a download and a news so this is just a way to make sure ID 23 for the download is not merged with ID 23 for the news

$catid is the ID that tranks where the comment is posted ... Ex: the id of the article or the ID of the download or what ever

$pagepath is the current page URL. Make sure you insert all the URL variables if there is any. Ex: $pagepath = "index.php?mod=tuto&id=$_GET[id]";

$dtitle is the title the comment systeme is gona log with. You can use the title of the news, download or what ever this comment is abbout. Ex: $dtitle = "RE: ".$cpage[titre];

 

This code is made to meet my requirements so i guess you will need to modify it a little. Have fun ;-)

Link to comment
Share on other sites

I am not spiderwell but you welcome :-)

 

BTW, im my rush of posting i got those to vars mixed up

$placeid = "news";
$catid = "23";

 

Oh and the script may sound unsecure but i have a array_map('escape_deep', $value) adding slashes and other counter mesures on _GET and _POST to protect my self aganst SQL injections so you might wana protect your self as well...

Link to comment
Share on other sites

Thanks drisate,

 

This is great stuff bro, I am definately way below your level of coding! lol!

 

My version of php is php5 so had to make few twirks to the code and it works thanks bro.

 

Though the code works perfectly its not exactly what am looking for, nevertheless I will use this as a guide. Its gonna help alot!

 

Thanks for your help bro!

 

Link to comment
Share on other sites

I spend all weekend analyzing your script and I finally figure how to get it to work..mostly had to set some variables and write new queries to suite my need am it worked like a house on fire!!

 

So thanks for your help bro..You is a STAR  :thumb-up:

 

Kamal

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.