Archive for May 2551

ภาษาไทยกับ PHP และ MySQL

UTF-8 เป็นคำตอบสุดท้่าย เพื่อให้การใช้ภาษาไทยกับ PHP และ MySQL ใช้งานได้อย่างสมบูรณ์

การสร้างฐานข้อมูลใน MySQL

ให้ตั้งค่าดังต่อไปนี้

  • Default CHARSET = utf8
  • COLLATE = utf8_unicode_ci

ตัวอย่างเช่น

CREATE TABLE brand (
pk_brand int(5) NOT NULL auto_increment,
brand varchar(250) collate utf8_unicode_ci NOT NULL,
brand_logo varchar(200) collate utf8_unicode_ci NOT NULL,
website varchar(200) collate utf8_unicode_ci default NULL,
PRIMARY KEY  (pk_brand)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

แล้วทำไมจึงไม่ใช้ utf8_general_ci?  อ่านได้ที่นี่

การเชื่อมต่อจาก PHP ไปยัง MySQL

หลังจากที่เชื่อมต่อกับฐานข้อมูลแล้วให้ส่ง query ต่อไปนี้ด้วย (ทำแค่ครั้งเดียว)

  • set NAMES utf8

ขอให้สังเกตว่า ใช้ utf8  ไม่ใช่  utf-8  ตัวอย่างเช่น

<?php
// Database connection for abstract
$dsn = 'mysql://username:password@localhost/database';
$mdb2 =& MDB2::factory($dsn);
if (PEAR::isError($mdb2)) {
    die ($mdb2->getMessage());
}
// set fetchmode
$mdb2->setFetchMode(MDB2_FETCHMODE_ASSOC);
$mdb2->query('set NAMES utf8');
?>

การเขียน META TAG ใน HTML Code

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>hello!</title>
</head>

:-)

Popularity: 65% [?]

วิธีการเรียก Smarty library แบบง่าย

ผมบังเิอิญไปเจอวิธีการที่จะเรียก Smarty library ที่น่าสนใจ  สะดวกมากเวลาที่กำลังพัฒนาโปรแกรม เพราะไม่ต้องระบุ path เลย แต่เมื่อพัฒนาโปรแกรมเสร็จแล้วก็ควรแก้ไข path ให้ถูกต้องครับ

<?php
// SMARTY Section
define('SMARTY_DIR',str_replace("\\","/",getcwd()).'/Smarty-2.6.19/libs/');
require_once(SMARTY_DIR . 'Smarty.class.php');
$smarty = new Smarty();
$smarty->template_dir = str_replace("\\","/",getcwd()).'/templates/';
$smarty->compile_dir  = str_replace("\\","/",getcwd()).'/templates_c/';
$smarty->config_dir   = str_replace("\\","/",getcwd()).'/configs/';
$smarty->cache_dir    = str_replace("\\","/",getcwd()).'/cache/';
//** un-comment the following line to show the debug console
//$smarty->debugging = true;
?>

Popularity: 12% [?]

CSS and Preformatted

ผมลองค้นหา CSS ที่ใช้ร่วมกับ <pre> tag เพื่อนำมาใช้แสดงโค้ดของโปรแกรม ก็บังเอิญได้พบ CSS ที่ใช้แสดงผลดังนี้

.phpcode, pre {
  overflow: auto;
  padding-left: 15px;
  padding-right: 15px;
  font-size: 11px;
  line-height: 15px;
  margin-top: 10px;
  width: 93%;
  display: block;
  background-color: #eeeeee;
  color: #000000;
  max-height: 300px;
}

เราสามารถกำหนด white-space: pre; ให้กับแต่ละ element ได้เช่นกัน ดังตัวอย่างต่อไปนี้

.preElement{
  white-space: pre;
  font-weight: bold;
  color: navy;
  font-family: arial;
}

และสามารถกำหนดฟอนต์ให้กับ <pre> แท็กดังนี้

pre{
  font-weight: bold;
  color: navy;
  font-family: arial;
}

Popularity: 14% [?]

PEAR::Auth package

PEAR  มีแพ็คเกจที่ชื่อว่า  Auth  ซึ่งช่วยอำนวยความสะดวกในการสร้างระบบตรวจสอบสิทธิ์ผู้ใช้ (authentication) โดยช่วยอำนวยความสะดวกตั้งแต่ การเพิ่มและลบผู้ใช้ การล็อกอิน การนำข้อมูลผู้ใช้มาแสดง การกำหนดระยะเวลาการล็อกอินและ Idle timeout และการตรวจสอบสิทธิ์ผู้ใช้งาน  ทำให้ช่วยลดเวลาในการพัฒนาโปรแกรมลงได้มาก 

สิ่งที่สำคัญคือ Auth รองรับระบบจัดเก็บข้อมูลของผู้ใช้หลากหลายประเภท ได้แก่

  • All databases supported by the PEAR database layer
  • All databases supported by the MDB database layer
  • All databases supported by the MDB2 database layer
  • Plaintext files
  • LDAP servers
  • POP3 servers
  • IMAP servers
  • vpopmail accounts (Using either PECL vpopmail or PEAR Net_Vpopmaild)
  • RADIUS
  • SAMBA password files
  • SOAP (Using either PEAR SOAP package or PHP5 SOAP extension)
  • PEAR website
  • Kerberos V servers
  • SAP servers
Read more »

Popularity: 22% [?]