PDA

View Full Version : Adding smilies to my shoutbox! Please help.


Element
08-17-2004, 07:34 AM
Okay I have a MySQL based shoutbox and i made one version but now people say I should add smilies, and in my PHP book it doens't cover smilies, and I need help

how would I go about having a selection of smilies that they click on to add in the text area in a binary format such as : - ) would turn into a smilie or by clicking the smilie it would add that.

raf
08-17-2004, 09:27 AM
That would be a clientside action, using clientside scripting like Javascript.

What you could do with PHP is replace the ': - )' by an imagetag of your smiley.

you couls build 2 arrays (one with the text, one with the imagetags) and then do a str_replace(). Like so

$arr_smiley_text = array(':)', ':(', ':o');
$arr_smiley_image = array(
'<img src="./images/smile_happy.gif" alt="happy" id="smileyhappy" name="smileyhappy" width="20" height="20" />',
'<img src="./images/smile_sad.gif" alt="sad" id="smileysad" name="smileysad" width="20" height="20" />',
'<img src="./images/smile_oooh.gif" alt="oooh" id="smileyoooh" name="smileyoooh" width="20" height="20" />');

$newphrase = str_replace($arr_smiley_text, $arr_smiley_image, $comment);

$comment is then the posted text where you wanna add smileys.

Jero
08-17-2004, 12:31 PM
This is my code:

$smiliesDir = "images/smilies/";
$smilies = array(':-)' => "happy.gif",
':-D' => "bigsmile.gif",
':-(' => "sad.gif",
':\'(' => "crying.gif",
'(A)' => "angle.gif",
'-_-' => "doh.gif",
':-O' => "oh.gif",
';-)' => "wink.gif");

foreach ($smilies AS $find => $character)
$message = str_replace(htmlentities($find), "<img src=\"". $smiliesDir . $character ."\" alt=\"\" />", $message);

Javascript for adding it into your message box:

<script type="text/javaScript">
<!--
function storeCaret (textEl) {
if (textEl.createTextRange)
textEl.caretPos = document.selection.createRange().duplicate();
}
function insertAtCaret (textEl, text) {
if (textEl.createTextRange && textEl.caretPos) {
var caretPos = textEl.caretPos;
caretPos.text =
caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?
text + ' ' : text;
}
else
textEl.value = textEl.value + ' ' + text;
}
-->
</script>

And add this atribute at every smilie you can click on:

onclick="insertAtCaret(document.FORMNAME.MESSAGEBOXNAME, ':-)')"

You have to replace FORMNAME with the name of your form and MESSAGEBOXNAME with the name of your messagebox. And replace :-).

Element
08-18-2004, 01:02 AM
thanks guys

doesn't work


http://www.akiyume.com/acc/shoutbox.php <-- its MySQL is that why?

As5a5sIn5
08-18-2004, 01:09 AM
This is my code:

$smiliesDir = "images/smilies/";
$smilies = array(':-)' => "happy.gif",
':-D' => "bigsmile.gif",
':-(' => "sad.gif",
':\'(' => "crying.gif",
'(A)' => "angle.gif",
'-_-' => "doh.gif",
':-O' => "oh.gif",
';-)' => "wink.gif");

foreach ($smilies AS $find => $character)
$message = str_replace(htmlentities($find), "<img src=\"". $smiliesDir . $character ."\" alt=\"\" />", $message);

Lookin at this code wouldn't you change $smiliesDir . $character to $smiliesDir[$character] :D

Element
08-18-2004, 01:40 AM
still don't work even with the other code.

As5a5sIn5
08-18-2004, 04:24 AM
sorry i c its $smileysDir . $smilies[$character] :D
maybe other errors but thats cleared up :p

raf
08-18-2004, 07:39 AM
still don't work even with the other code.
Do you mean that my code doesn't work? I don't see what couldn't work.

It maybe looks like more typing, but it's just a bit mlore copy pasting.
+ it'll probably be executed faster + it a least generates valid xhtml with alternative texts

Element
08-19-2004, 04:10 AM
Okay here is the shoutbox code...everything is pretty much in one file besides config....but yeah please don't like chop my head off if the code is crap...this is my first script besides little strings.

<html>
<head>

<script type="text/javaScript">
<!--
function storeCaret (textEl) {
if (textEl.createTextRange)
textEl.caretPos = document.selection.createRange().duplicate();
}
function insertAtCaret (textEl, text) {
if (textEl.createTextRange && textEl.caretPos) {
var caretPos = textEl.caretPos;
caretPos.text =
caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?
text + ' ' : text;
}
else
textEl.value = textEl.value + ' ' + text;
}
-->
</script>

<?php include 'config.php';

if ( $refresh == "on" ) {
echo "<meta http-equiv=\"refresh\" content=\"$refreshin\">\n";} ?>
<style type="text/css">
<!--
body,td,th {
font-family: Verdana;
font-size: 9px;
color: #333333;
}
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
background-color: #DADBDD;
}

a:link {
color: #333333;
text-decoration: none;
}
a:visited {
color: #333333;
text-decoration: none;
}
a:hover {
color: #FFFFFF;
text-decoration: none;
}
a:active {
color: #333333;
text-decoration: none;
}

input, select {

font-size: small;
color: #333333;
font-size: 9px;
background-color: #D5D6DA;
border-color: #61656C;
border-width: 1px;
border-style: solid;

}
textarea {
background: #D5D6DA;
font-size: 9px;
color: #333333;
border-color: #61656C;
border-width: 1px;
border-style: solid;
}
-->
</style>

</head>
<body>

<?php

$smiliesDir = "smilies/";
$smilies = array(':-)' => "smiley01.gif",
':-D' => "bigsmile.gif",
':-(' => "sad.gif",
':\'(' => "crying.gif",
'(A)' => "angle.gif",
'-_-' => "doh.gif",
':-O' => "oh.gif",
';-)' => "wink.gif");

foreach ($smilies AS $find => $character)
$r[message] = str_replace(htmlentities($find), "<img src=\"". $smileysDir . $smilies[$character] ."\" alt=\"\" />", $r[message]);
?>

<?php

{ echo("<center>$sname</center>"); }

if($_POST['submit']) {

if(!$_POST['author']) {
echo 'Error ! : No name entered';
die;
}
if(!$_POST['email']) {
echo 'Error ! : No email entered';
die;
}
if(!$_POST['message']) {
echo 'Error ! : No message entered';
die;
}

$message = strip_tags($_POST['message'], '');
$email = strip_tags($_POST['email'], '');
$author = strip_tags($_POST['author'], '');
$website = strip_tags($_POST['website'], '');

$message_length = strlen($message);
$author_length = strlen($author);
if($message_length > 300) {
echo "Error ! : Your message was too long, messages must be less than 150 chars";
die;
}
if($author_length > 150) {
echo "Error ! : Your name was too long, names must be less than 150 chars";
die;
}

mysql_connect($db_host,$db_user,$db_password) or die(mysql_error());

mysql_select_db($db_name) or die(mysql_error());

$date = date("h:i A dS M");

$query = "INSERT INTO shoutbox (message, author, email, website, date, ip)
VALUES ('$message','$author','$email','$website','$date','$_SERVER[REMOTE_ADDR]')";
mysql_query($query);
mysql_close();

echo "Your post has been added.<BR>";
echo "<A HREF=\"shoutbox.php\">Go back</A>";

} else {

mysql_connect($db_host,$db_user,$db_password) or die(mysql_error());

mysql_select_db($db_name) or die(mysql_error());

$query = "SELECT message, author, email, website, date, ip
FROM shoutbox order by id DESC LIMIT 10";
$result = mysql_query($query);
echo "<TABLE width='100%' cellspacing='1' cellpadding='2' bgcolor='#EEEEEE'>";
while($r=mysql_fetch_array($result))
{
// Shouts body
echo "<TR>
<TD bgcolor='#94979C'><font size='1'><a href=\"mailto:$r[email]\"><img src='mailme.gif' border='0'></a> <a href=\"$r[website]\" target=\"_blank\">$r[author]</a>:</TD>
</TR>
<TR>
<TD bgcolor='#D5D6DA'><font size='1'>$r[message]</font></TD>
</TR>
";

}
echo "</TABLE>";
// The form body
?>
<FORM METHOD=POST ACTION="shoutbox.php" name="fields">
<TABLE cellspacing="0" cellpadding="2" width="100%">
<TR>
<TD><INPUT TYPE="text" NAME="author" size="20" value="name"></TD>
</TR>
<TR>
<TD><INPUT TYPE="text" NAME="email" size="20" value="email"></TD>
</TR>
<TR>
<TD><INPUT TYPE="text" NAME="website" size="20" value="website"></TD>
</TR>
<TR>
<TD><img src="smilies/smiley01.gif" onclick="insertAtCaret(document.fields.message, ':-)')"></TD>
</TR>
<TR>
<TD><INPUT TYPE="text" NAME="message" size="20" value="message"></TD>
</TR>
<TR>
<TD><INPUT TYPE="submit" name="submit" value="post"></TD>
</TR>
</TABLE>
</FORM> <br><br><center>YumeBox Version <?php { echo"$version"; } ?><br>By: <a href="http://www.akiyume.com" target="_blank">Element</a></center>
<?php
}

?>
</body>
</html>