Home arrow MySQL arrow Page 2 - Quick and Dirty Emoticons
MYSQL

Quick and Dirty Emoticons


Ever wondered how forums like VBulletin display emoticons? In this article Havard shows us a quick and dirty way to create and use our own emoticons with PHP and MySQL.

Author Info:
By: Havard Lindset
Rating: 4 stars4 stars4 stars4 stars4 stars / 185
July 04, 2002
TABLE OF CONTENTS:
  1. · Quick and Dirty Emoticons
  2. · Quick and Dirty Emoticons
  3. · Conclusion

print this article
SEARCH DEVARTICLES

Quick and Dirty Emoticons - Quick and Dirty Emoticons
(Page 2 of 3 )

Before we get to the coding, I'll explain how to create the MySQL table, and also how we can go about inserting the emoticons into the database.

Run the following queries in the MySQL console application:

CREATE DATABASE mydatabase;
USE mydatabase;

CREATE TABLE emoticons (
emote VARCHAR(10) NOT NULL,
image VARCHAR(30) NOT NULL
);


The emote-column will hold the text, while the image column will contain the filename of the image that will replace every instance of emote in a string of text.

Now that we've created the MySQL table, it's time to insert some rows into it.

Inserting Emoticons Into The Database
The following query is just an example. You may insert as many emoticons as you'd like:

INSERT INTO emoticons (emote, image)
VALUES (":)", "icon_smile.gif");

INSERT INTO emoticons (emote, image)
VALUES (";)", "icon_wink.gif");

INSERT INTO emoticons (emote, image)
VALUES (":(", "icon_sad.gif");


I have provided a few emoticons below. Right click on the images, and select "Save Picture As". Save them in a folder named "images", "emoticons" or something similar.

icon_smile.gif icon_smile.gif
icon_wink.gif icon_wink.gif
icon_sad.gif icon_sad.gif

You may of course create more emoticons, and insert them in your database if you'd like to.

Putting It All Together
Just having an emoticon table won't do us any good. This is where PHP comes into play. Place the following code in a .php file:

<?PHP

// Configuration part
$dbhost = "localhost"; // Database host
$dbname = "mydatabase"; // Database name
$dbuser = "dbuser"; // Database username
$dbpass = "dbpass"; // Database password
$path = "images"; // Path to the directory where the emoticons are

// Connect to database
$db = mysql_connect($dbhost, $dbuser, $dbpass) or die("Error: Couldn't connect to database");
mysql_select_db($dbname, $db) or die("Error: Couldn't select database.");

// Query the database, and assign the result-set to $result
$query = "SELECT emote, image FROM emoticons";
$result = mysql_query($query);

// Loop through the results, and place the results in two arrays
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$emotes[] = $row['emote'];
$images[] = "<img src='" . $path . "/" . $row['image'] . "'>";
}

$text = "<strong>Emoticons</strong><br>
:) ;) :( :P<br><br>
Neat? :)";

// The line below replaces the emotes with the images
echo str_replace($emotes, $images, $text);

?>


Let's look at some parts of the code that you may be unfamiliar with:

// Loop through the results, and place the results in two arrays
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$emotes[] = $row['emote'];
$images[] = "<img src='" . $path . "/" . $row['image'] . "'>";
}


The while loop iterates through the MySQL result attained from the query and places it into two different arrays: one array for each of the selected columns.

PHP will automatically increment the index of the array when you don't put anything between the brackets of the array you're assigning the value to. 

// The line below replaces the emotes with the images
echo str_replace($emotes, $images, $text);


Let me explain how the str_replace() function works in the code above.

If you feed the first two attributes of the function with arrays, then it will iterate through the values of the array. Take a look at the example below:

$search = array(0 => a, 1 => b, 2 => c);
$replace = array(0 => c, 1 => b, 2 => a);
str_replace($search, $replace, "a b c");


$search[0] would be replaced by $replace[0], $search[1] would be replaced by $replace[1], and so on.. After processing the final value would be "b c a". This function is useful in situations where you have two related arrays that require swapping or replacing of common data.
blog comments powered by Disqus
MYSQL ARTICLES

- MySQL and BLOBs
- Two Lessons in ASP and MySQL
- Lord Of The Strings Part 2
- Lord Of The Strings Part 1
- Importing Data into MySQL with Navicat
- Building a Sustainable Web Site
- Creating An Online Photo Album with PHP and ...
- Creating An Online Photo Album with PHP and ...
- PhpED 3.2 More Features Than You Can Poke ...
- Creating An Online Photo Album with PHP and ...
- Creating An Online Photo Album with PHP and ...
- Security and Sessions in PHP
- Setup Your Personal Reminder System Using PHP
- Create a IP-Country Database Using PERL and ...
- Developing a Dynamic Document Search in PHP ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials