Jump to content


Photo

How to Make Smilies Work


  • Please log in to reply
5 replies to this topic

#1 RTS

RTS
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts

Posted 12 October 2006 - 05:00 AM

i just downloaded a pack of snmilies for messages on my site, and i was wondering, how do i make it so that when someone types : ) it will come up as a smiley?

#2 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 12 October 2006 - 05:32 AM

Put them in an array like this then replace them:

$smilies = array( // code => image
		':)' => 'smile.gif',
		':D' => "smile2.gif',
		':(' => 'sad.gif',
	);

$codes = array_keys($smilies);

$the_text = str_replace($codes,$smilies,$the_text);


#3 RTS

RTS
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts

Posted 12 October 2006 - 04:28 PM

not quite sure how to put that in my code, but atleast I tried. it gave me
Parse error: parse error in /Library/WebServer/Documents/read.php on line 7

Heres my script
read.php

<?php
$con = mysql_connect("localhost","ZackBabtkis","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("test", $con);
$result = mysql_query("SELECT * FROM messages WHERE ID=".$_GET['id']);
while($row = mysql_fetch_array($result))
  {
$the_text = echo "<table border=10 width=400 cellpadding=0 cellspacing=3><tr><td background=table.jpg><h1 align=center>Message:</h1>" . $row['Message'];
$smilies = array( // code => image
':)' => '<img src=smilies/smile.gif>',
);

$codes = array_keys($smilies);

$the_text = str_replace($codes,$smilies,$the_text);

  }
echo "</td></tr></table>";

mysql_close($con);
?>



#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 12 October 2006 - 05:26 PM

You had a few badf syntax errors. Using echo within  variable is not allowed and thus why you got the parse error. I have corrected this issue and tidy'd up your code a little too:
<?php

$con = mysql_connect("localhost","ZackBabtkis","") or die('Could not connect: ' . mysql_error());
mysql_select_db("test", $con);

$result = mysql_query("SELECT * FROM messages WHERE ID=" . $_GET['id']);

while($row = mysql_fetch_array($result))
{
    $the_text = '<table border="10" width="400" cellpadding="0" cellspacing="3">
    <tr>
        <td background="table.jpg">
            <h1 align="center">Message:</h1>' . $row['Message'];

    $smilies = array( // code => image
        'Smiley' => '<img src=smilies/smile.gif>',
    );

    $codes = array_keys($smilies);

    $the_text = str_replace($codes,$smilies,$the_text);

    echo $the_text;
}

echo "      </td>
    </tr>
</table>";

mysql_close($con);

?>


#5 RTS

RTS
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts

Posted 12 October 2006 - 06:24 PM

hmmmm, that didnt give me an error, but just showd up as the text ": )" do I need to have it in the message being sent? how would I put it in this code?
send.php:
<?php
session_start();
$con = mysql_connect("localhost","ZackBabtkis","");
$subject = mysql_real_escape_string($_POST[subject]);
$message = mysql_real_escape_string($_POST[message]);
$user = $_COOKIE["user"];
$sender = $_SESSION['username'];
$date = date("F j, Y, g:i a");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("test", $con);
$sql = "INSERT INTO messages (Username, Sender, Subject, Message, Date) VALUES ('$user','$sender','$subject','$message', '$date')";
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "Your message has been sent to $user";
mysql_close($con)
?>


#6 RTS

RTS
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts

Posted 12 October 2006 - 11:07 PM

anyone? im still somewhat new to php, so this kind of thing is hard for me.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users