Thursday, July 9, 2009

make Emoticon in PHP

Hullo halo, udah old likely caw nulis this Come on ane will write the simple article, gimana the way of fetching/parsing/ngambil emoticon-emoticon kept in database, continue to change the selected symbols in text with file image emoticon. Okeh direct.
* First, which udah surely, made the tables by the name of emoticon, his field merely two: "id" with " alt". Practical to so that execute aja direct code below/under in phpMyAdmin.
CREATE TABLE `db_ente`.`emoticon` (
`id` INT NOT NULL AUTO_INCREMENT ,
`alt` VARCHAR( 100 ) NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE = MYISAM
Don'T forget to change ` db_ente` by the name of the database in computer ente. If there is no could dimembuat formerly with code below/under.
CREATE DATABASE `nama_db` ;


* Database all right, now remained masukkin his data, karna merely for trial and error, masukkin aja one record formerly.
INSERT INTO `db_ente`.`emoticons` (
`id` ,
`alt`
)
VALUES (
NULL , ':lol:'
);
With query above us masukkin alt emoticon ": lol:" ( lol = laughing out loud)
. * To image animation emoticon representing the symbol " lol", bikin/copy image wanted to directory emoticon/lol.gif. Become in the directory [is] equal to especial file, make the directory emoticon, trus copy of file image by the name of " lol.gif" ( haduh ribet gw ngomongnya). Ane [by] xself for this example [of] make animation from Plurk this one.
. * Now, function in PHP to fetching emoticon:
function fetch_emo($strnya) {
$hsl = $strnya;
$qE = mysql_query("SELECT * FROM emoticon") or die(mysql_error());
while ($aE = mysql_fetch_array($qE)) {
$offst = substr($aE["alt"], 1);
$img = "";
$hsl = str_replace($aE["alt"], $img, $hsl);
}
return $hsl;
}

* Last, implementation.
?
mysql_connect("localhost", "user", "password") or die(mysql_error()); //ganti sesuai MySQL ente
mysql_select_db("db_ente") or die(mysql_error());
?
html
head
title>Test Emoticon head
body
?
.$str_test = "Test kacang euy :lol:";
.$sesudah = .fetch_emo(.$str_test);
echo "Sebelum: $str_test

";
echo "Sesudah: $ssd";
?
body
html

Hasilnya:
Sebelum: Test kacang euy :lol:
Sesudah: Test kacang euy
Now, ane jelasin few [of] his [job/activity] way.
In first step and both, we draw up the database mo dipake to nyimpan string selected which mo di-replace with emoticon.
Karna kept in database, we can easily ngapus or nambah emoticon-emoticon newly.
In function fetch_emo() which we make in step fourth, we do query to database, and nge-parse one string emoticon ( in above example [of] ": lol:"), trus changed with image in directory emoticon/lol.gif. So, possibly [at] the questioning, lol.gif dapet where from ?? In this function, so that we is caw require to busy define path image, ane make path imagenya taken away from string among dots of two (":") added with ekstensi ". gif", so that kalo string emoticon-nya ": lol:", mean path image-nya is " lol.gif".

Artikel yang Berkaitan

0 komentar:

Post a Comment