How can I post smileys in a comment form? php/mySQL comments

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Denver40
Forum Newbie
Posts: 18
Joined: Tue Dec 02, 2008 9:58 am

How can I post smileys in a comment form? php/mySQL comments

Post by Denver40 »

Its a php mySQL comment system. Im a bit of a php noob too so go easy on me. :D
I want to post smileys like this :) or :D and have them show up as smiley gifs, how would I make the php recognize the anscii smiley and then post its image? I think there needs to be some altering to the mySQL table since flag varchar(20) NOT NULL default '', and rating tinyint(1) NOT NULL default '0', sound like they may have something to do with the flag.png files and the Rating.png files. Heres a link to an example of how the comment system looks and works http://www.plentyoftorrents.com/flsh/an ... c.php#post (scroll up to see comments)

Thanks for any help.

Here is the mySQL table thing I had to import into phpmyadmin when installing the script.

Code: Select all

#
# Table structure for table `page_comments`
#
 
CREATE TABLE IF NOT EXISTS page_comments (
  id mediumint(11) NOT NULL auto_increment,
  name varchar(40) NOT NULL default '',
  location varchar(40) NOT NULL default '',
  flag varchar(20) NOT NULL default '',
  comments text NOT NULL,
  ip varchar(20) NOT NULL default '',
  page_id varchar(40) NOT NULL default '0',
  dated datetime NOT NULL default '0000-00-00 00:00:00',
  rating tinyint(1) NOT NULL default '0',
  admin_comment text NOT NULL default '',
  is_approved tinyint(1) NOT NULL default '1',
  PRIMARY KEY  (id)
) TYPE=MyISAM AUTO_INCREMENT=1 ;
Heres the comments show php page that contains the rating and flag images, so Im assuming thats where any smileys will have to go.

Code: Select all

<?php
 
 
/***********************************************************************************
There is no need to edit ANYTHING below this comment.  Almost EVERYTHING you need
to edit is contained in the comments-config.php file and comments-lang.php file. 
PLEASE READ THE DOCUMENTATION FILE!
************************************************************************************/
 
include("includes/comments-config.php"); // configuration parameters package
include("includes/comments-lang.php"); // language/words package
 
$comm_page = is_numeric($_GET['comm_page']) ? $_GET['comm_page'] : 1;
if ($comm_page<1) {
    $comm_page = 1;
}
 
// Figure out the limit for the query based on the current page number. 
$from = $comm_page * $comment_limit - $comment_limit;
 
include("includes/db_conn.php"); // connect to host and select db
mysql_connect($db_host, $db_user, $db_pass) or die("Connection Error: ". mysql_error());
mysql_select_db($db_name);
 
// construct page query to find out how many matches
$result=mysql_db_query($db_name,"select count(*) from $db_table WHERE page_id='$page_id' AND is_approved = '1'");
$count=mysql_result($result,0,"count(*)");
$total_pages = ceil($count / $comment_limit);
 
// and the average rating is ...
$query = "SELECT AVG(rating) from $db_table WHERE page_id='$page_id' AND is_approved = '1' AND rating>'0'";
$result = mysql_query($query) or die("error ". mysql_error(). " with query ".$query);
$row = mysql_fetch_array($result);
$av_rating = number_format($row['AVG(rating)'],2);
 
// construct page query to find out how many matches
$query = "SELECT * from $db_table WHERE page_id = '$page_id' AND is_approved = '1' ORDER by dated DESC LIMIT $from, $comment_limit";// what matches THIS page?
$result = mysql_query($query) or die("Error: ". mysql_error(). " with query ". $query); 
 
// skip output if no comments exist
if (!$count) {
    echo "<p style='". $num_style. "'>". $no_comments. "</p>";
} else {
    echo "<p style='". $num_style. "'>". $comments_to_date. $count. $this_is_page. $comm_page. $page_of_page. $total_pages. ". ";
    if (($av_rating>0) && ($art_rating==1)) {
       $stars = 5 * round($av_rating/0.5);
       echo $average_rating. "<img src='comments/images/stars/stars_". $stars. ".png' alt=''/>";
    }
    echo "</p>";
    // output comments
    echo "<table cellpadding='0' cellspacing='0' width='100%' border='0' align='center'>";
    while ($myrow = mysql_fetch_array($result)) // loop through all results
    { 
        $style = $style == $ro1 ? $ro2 : $ro1; 
        echo "<tr bgcolor='". $style. "'>";
        echo "<td><p style='". $comm_style. "'><b>";
        if ($show_flags == 1) {
            $flag_image = "comments/images/flags/". $myrow['flag']. ".png";
            if (file_exists($flag_image)) {
                $size = getimagesize($flag_image);
                echo "&nbsp;<img src='". $flag_image. "' ". $size[3]. " alt=''/>&nbsp;";
            }
        if (!$myrow['name']) {
            echo $unknown_poster;
        } else {
            echo $myrow['name'];
        }
        
        
        
        }
        echo "</b></p></td>";
        echo "<td align='right'><p style='". $commdate_style. "'>";
        niceday($myrow['dated']);
        echo "<br/>";
        if (($art_rating==1) && ($myrow['rating']>0) && ($visitor_rating==1)) {
           $star_img = "comments/images/stars/stars_". 10*$myrow['rating']. ".png";
           $size = getimagesize($star_img);
           echo "&nbsp;<img src='". $star_img. "' ". $size[3]. " alt=''/>";
        }
 
        echo "</p></td></tr>";
        echo "<tr bgcolor='". $style. "'>";
        echo "<td colspan='2' style='border-bottom:1px dotted ". $space_color. ";'><p style='". $comm_style. "'>";
        $comments = stripslashes($myrow['comments']);
        if (strlen($comments)>$maxshow_comments) {
           $comments = substr($comments,0,$maxshow_comments). "... <a href='comments/showmore.php?id=". $myrow['id']. "'>". $show_more. "</a>&nbsp;<strong>&raquo;</strong>";
        }
        echo nl2br($comments);
        if ($myrow['admin_comment']) {
            echo "<br/><br/>". $admin_comment. "&nbsp;<span style='color:#c30;'><em>". $myrow['admin_comment']. "</span></em>";
        } 
        echo "</p></td></tr>\n";
    }
    // loop done
    echo "</table>\n";
}
// Pagination magic (of sorts)
if ($total_pages>1) {
            echo "<p><br/>Page:&nbsp;";
            for ($z=-5; $z<6;$z++) {
                $dapage = $comm_page+$z;
                if (($dapage>0) && ($dapage<=$total_pages)) {
                    if ($dapage==$comm_page) {
                        echo "-". $dapage. "-";
                    } else {
                        echo "<a class='pagelink' href='". $_SERVER['PHP_SELF']. "?comm_page=". $dapage. "'>&nbsp;". $dapage. "&nbsp;</a>";
                    }
                    echo "&nbsp;&nbsp;";
                }           
            }
            echo "</p>";
}
            
 
?>
Cirdan
Forum Contributor
Posts: 144
Joined: Sat Nov 01, 2008 3:20 pm

Re: How can I post smileys in a comment form? php/mySQL comments

Post by Cirdan »

If I understand your question correctly, I would search and replace

str_replace(":D", "<img src='myimage.gif' alt=':D' />", $mytext);
Denver40
Forum Newbie
Posts: 18
Joined: Tue Dec 02, 2008 9:58 am

Re: How can I post smileys in a comment form? php/mySQL comments

Post by Denver40 »

Cirdan wrote:If I understand your question correctly, I would search and replace

str_replace(":D", "<img src='myimage.gif' alt=':D' />", $mytext);
So that would turn any :D in the form into Image?
Could you show me where I could enter it into the comments show php without breaking the code on that page?
Denver40
Forum Newbie
Posts: 18
Joined: Tue Dec 02, 2008 9:58 am

Re: How can I post smileys in a comment form? php/mySQL comments

Post by Denver40 »

Also doesnt there need to be an echo to be able to print it on the comment section?

Like this does for the stars.

Code: Select all

if (($art_rating==1) && ($myrow['rating']>0) && ($visitor_rating==1)) {
           $star_img = "comments/images/stars/stars_". 10*$myrow['rating']. ".png";
           $size = getimagesize($star_img);
           echo "&nbsp;<img src='". $star_img. "' ". $size[3]. " alt=''/>";
        }
Cirdan
Forum Contributor
Posts: 144
Joined: Sat Nov 01, 2008 3:20 pm

Re: How can I post smileys in a comment form? php/mySQL comments

Post by Cirdan »

Line 86:

Code: Select all

 
        if (strlen($comments)>$maxshow_comments) {
           $comments = substr($comments,0,$maxshow_comments). "... <a href='comments/showmore.php?id=". $myrow['id']. "'>". $show_more. "</a>&nbsp;<strong>&raquo;</strong>";
        }
        echo nl2br($comments);
        if ($myrow['admin_comment']) {
            echo "<br/><br/>". $admin_comment. "&nbsp;<span style='color:#c30;'><em>". $myrow['admin_comment']. "</span></em>";
 
In your code there are two variables, one named $comments, and another named $admin_comment. You would need to perform the str_replace on the variables that contain the comments, or would contain smilies.
Denver40
Forum Newbie
Posts: 18
Joined: Tue Dec 02, 2008 9:58 am

Re: How can I post smileys in a comment form? php/mySQL comments

Post by Denver40 »

Cirdan wrote:Line 86:

Code: Select all

 
        if (strlen($comments)>$maxshow_comments) {
           $comments = substr($comments,0,$maxshow_comments). "... <a href='comments/showmore.php?id=". $myrow['id']. "'>". $show_more. "</a>&nbsp;<strong>&raquo;</strong>";
        }
        echo nl2br($comments);
        if ($myrow['admin_comment']) {
            echo "<br/><br/>". $admin_comment. "&nbsp;<span style='color:#c30;'><em>". $myrow['admin_comment']. "</span></em>";
 
In your code there are two variables, one named $comments, and another named $admin_comment. You would need to perform the str_replace on the variables that contain the comments, or would contain smilies.
So on line 3 here before $comments I should put the str_replace(":D", "<img src='myimage.gif' alt=':D' />", $mytext); ?

admin comments is in the admin panel and allows the admin to add text to someones post.
Cirdan
Forum Contributor
Posts: 144
Joined: Sat Nov 01, 2008 3:20 pm

Re: How can I post smileys in a comment form? php/mySQL comments

Post by Cirdan »

Denver40 wrote: So on line 3 here before $comments I should put the str_replace(":D", "<img src='myimage.gif' alt=':D' />", $mytext); ?

admin comments is in the admin panel and allows the admin to add text to someones post.
Right, but replace the $mytext with $comments
Denver40
Forum Newbie
Posts: 18
Joined: Tue Dec 02, 2008 9:58 am

Re: How can I post smileys in a comment form? php/mySQL comments

Post by Denver40 »

Cirdan wrote:
Denver40 wrote: So on line 3 here before $comments I should put the str_replace(":D", "<img src='myimage.gif' alt=':D' />", $mytext); ?

admin comments is in the admin panel and allows the admin to add text to someones post.
Right, but replace the $mytext with $comments
I tried that but nothings happening. Isnt there supposed to be an echo?

Heres what I did.

Code: Select all

$comments = stripslashes($myrow['comments']);
        if (strlen($comments)>$maxshow_comments) {
         str_replace(":D", "<img src='http://www.plentyoftorrents.com/flsh/smileys/hai_gais.gif' alt=':D' />", $comments);  $comments = substr($comments,0,$maxshow_comments). "... <a href='comments/showmore.php?id=". $myrow['id']. "'>". $show_more. "</a>&nbsp;<strong>&raquo;</strong>";
        }
        echo nl2br($comments);
        if ($myrow['admin_comment']) {
            echo "<br/><br/>". $admin_comment. "&nbsp;<span style='color:#c30;'><em>". $myrow['admin_comment']. "</span></em>";
        } 
        echo "</p></td></tr>\n";
    }
    // loop done
Heres the page where Im testing it.
http://www.plentyoftorrents.com/flsh/an ... princess-1
Cirdan
Forum Contributor
Posts: 144
Joined: Sat Nov 01, 2008 3:20 pm

Re: How can I post smileys in a comment form? php/mySQL comments

Post by Cirdan »

Yea, it gets echo'd out here at line 5 of your example: echo nl2br($comments);

I'm not sure why it isn't working, I can't access that site because it's blocked.
Denver40
Forum Newbie
Posts: 18
Joined: Tue Dec 02, 2008 9:58 am

Re: How can I post smileys in a comment form? php/mySQL comments

Post by Denver40 »

Cirdan wrote:Yea, it gets echo'd out here at line 5 of your example: echo nl2br($comments);

I'm not sure why it isn't working, I can't access that site because it's blocked.
Ya its still not working, thanks anyway. Whos blocking my site? 8O
Cirdan
Forum Contributor
Posts: 144
Joined: Sat Nov 01, 2008 3:20 pm

Re: How can I post smileys in a comment form? php/mySQL comments

Post by Cirdan »

Denver40 wrote:
Cirdan wrote:Yea, it gets echo'd out here at line 5 of your example: echo nl2br($comments);

I'm not sure why it isn't working, I can't access that site because it's blocked.
Ya its still not working, thanks anyway. Whos blocking my site? 8O
My dad, it's blocked because of "Proxy Avoidance"
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: How can I post smileys in a comment form? php/mySQL comments

Post by papa »

"This function returns a string or an array with all occurrences of search in subject replaced with the given replace value. "

so try $comment = str_replace()
Denver40
Forum Newbie
Posts: 18
Joined: Tue Dec 02, 2008 9:58 am

Re: How can I post smileys in a comment form? php/mySQL comments

Post by Denver40 »

papa wrote:"This function returns a string or an array with all occurrences of search in subject replaced with the given replace value. "

so try $comment = str_replace()
so like this $comment = str_replace(":D", "<img src='myimage.gif' alt=':D' />"); ?
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: How can I post smileys in a comment form? php/mySQL comments

Post by papa »

Try and find out. :)
Denver40
Forum Newbie
Posts: 18
Joined: Tue Dec 02, 2008 9:58 am

Re: How can I post smileys in a comment form? php/mySQL comments

Post by Denver40 »

papa wrote:Try and find out. :)
I tried but it wont print out.
heres where I put it on line 99.

Code: Select all

<?php
 
/************************************************************************************
Page Comments - Written by Andy Bowers | http://www.halfadot.com | andy@halfadot.com
Version 6.0 - release date 2008-02-28 [Original Version 1 release 2003-11-28]
 
You are free to use this script as long as the credits and notes are not removed.  If
you intend to distribute this script, make sure to include all of the files from the
original zip package.  Your use of this script is subject to the terms of the license
included in the zipped package as license.txt, and constitutes acceptance thereto.
 
Please e-mail me with any questions or comments - I may even respond :)
*/
 
/***********************************************************************************
There is no need to edit ANYTHING below this comment.  Almost EVERYTHING you need
to edit is contained in the comments-config.php file and comments-lang.php file. 
PLEASE READ THE DOCUMENTATION FILE!
************************************************************************************/
 
include("includes/comments-config.php"); // configuration parameters package
include("includes/comments-lang.php"); // language/words package
 
$comm_page = is_numeric($_GET['comm_page']) ? $_GET['comm_page'] : 1;
if ($comm_page<1) {
    $comm_page = 1;
}
 
// Figure out the limit for the query based on the current page number. 
$from = $comm_page * $comment_limit - $comment_limit;
 
include("includes/db_conn.php"); // connect to host and select db
mysql_connect($db_host, $db_user, $db_pass) or die("Connection Error: ". mysql_error());
mysql_select_db($db_name);
 
// construct page query to find out how many matches
$result=mysql_db_query($db_name,"select count(*) from $db_table WHERE page_id='$page_id' AND is_approved = '1'");
$count=mysql_result($result,0,"count(*)");
$total_pages = ceil($count / $comment_limit);
 
// and the average rating is ...
$query = "SELECT AVG(rating) from $db_table WHERE page_id='$page_id' AND is_approved = '1' AND rating>'0'";
$result = mysql_query($query) or die("error ". mysql_error(). " with query ".$query);
$row = mysql_fetch_array($result);
$av_rating = number_format($row['AVG(rating)'],2);
 
// construct page query to find out how many matches
$query = "SELECT * from $db_table WHERE page_id = '$page_id' AND is_approved = '1' ORDER by dated DESC LIMIT $from, $comment_limit";// what matches THIS page?
$result = mysql_query($query) or die("Error: ". mysql_error(). " with query ". $query); 
 
// skip output if no comments exist
if (!$count) {
    echo "<center><p style='". $num_style. "'>". $no_comments. "</p></center>";
} else {
    echo "<center><p style='". $num_style. "'>". $comments_to_date. $count. $this_is_page. $comm_page. $page_of_page. $total_pages. ". ";
    if (($av_rating>0) && ($art_rating==1)) {
       $stars = 5 * round($av_rating/0.5);
       echo $average_rating. "<img src='comments/images/stars/stars_". $stars. ".png' alt=''/></center>";
    }
    echo "</p>";
    
    // output comments
    echo "<table cellpadding='0' cellspacing='0' width='100%' border='0' align='center'>";
    while ($myrow = mysql_fetch_array($result)) // loop through all results
    { 
        $style = $style == $ro1 ? $ro2 : $ro1; 
        echo "<tr bgcolor='". $style. "'>";
        echo "<td><p style='". $comm_style. "'><b>";
        if ($show_flags == 1) {
            $flag_image = "comments/images/flags/". $myrow['flag']. ".png";
            if (file_exists($flag_image)) {
                $size = getimagesize($flag_image);
                echo "&nbsp;<img src='". $flag_image. "' ". $size[3]. " alt=''/>&nbsp;";
            }
        if (!$myrow['name']) {
            echo $unknown_poster;
        } else {
            echo $myrow['name'];
        }
        
        
        
        }
        echo "</b></p></td>";
        echo "<td align='right'><p style='". $commdate_style. "'>";
        niceday($myrow['dated']);
        echo "<br/>";
        if (($art_rating==1) && ($myrow['rating']>0) && ($visitor_rating==1)) {
           $star_img = "comments/images/stars/stars_". 10*$myrow['rating']. ".png";
           $size = getimagesize($star_img);
           echo "&nbsp;<img src='". $star_img. "' ". $size[3]. " alt=''/>";
        }
 
        echo "</p></td></tr>";
        echo "<tr bgcolor='". $style. "'>";
        echo "<td colspan='2' style='border-bottom:1px dotted ". $space_color. ";'>&nbsp;<p style='". $comm_style. "'>&nbsp;&nbsp;&nbsp;&nbsp;";
        $comments = stripslashes($myrow['comments']);
        if (strlen($comments)>$maxshow_comments) {
        $comment = str_replace(":D", "<img src='http://www.plentyoftorrents.com/flsh/smileys/hai_gais.gif' alt=':D' />");
           $comments = substr($comments,0,$maxshow_comments). "... <a href='comments/showmore.php?id=". $myrow['id']. "'>". $show_more. "</a> &nbsp;<strong>&raquo;</strong>";
        
        }
        echo nl2br($comments);
        if ($myrow['admin_comment']) {
            echo "<br/><br/>". $admin_comment. "&nbsp;<span style='color:#c30;'><em>". $myrow['admin_comment']. "</span></em>";
        } 
        echo "</p></td></tr>\n";
    }
    // loop done
    echo "</table>\n";
}
// Pagination magic (of sorts)
if ($total_pages>1) {
            echo "<p><br/>Page:&nbsp;";
            for ($z=-5; $z<6;$z++) {
                $dapage = $comm_page+$z;
                if (($dapage>0) && ($dapage<=$total_pages)) {
                    if ($dapage==$comm_page) {
                        echo "-". $dapage. "-";
                    } else {
                        echo "<a class='pagelink' href='". $_SERVER['PHP_SELF']. "?comm_page=". $dapage. "'>&nbsp;". $dapage. "&nbsp;</a>";
                    }
                    echo "&nbsp;&nbsp;";
                }           
            }
            echo "</p>";
}
            
 
?>
Post Reply