MySQL
  Home arrow MySQL arrow Page 2 - Quick and Dirty Emoticons
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MYSQL

Quick and Dirty Emoticons
By: Havard Lindset
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 172
    2002-07-04

    Table of Contents:
  • Quick and Dirty Emoticons
  • Quick and Dirty Emoticons
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More MySQL Articles
    More By Havard Lindset


     

    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 ...






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT