วันอังคารที่ 7 ธันวาคม พ.ศ. 2553

รายชื่อ Zend Framework Components

Zend_Acl


เป็น access controll list ( ACL ) แบบขนาดย่อมแต่มีความยืดหยุ่นสูง สำหรับการกำหนดสิทธิ.
โดยทั่วไป โปรแกรมจะใช้ประโยชน์จาก ACL เพื่อป้องกัน protected objects จาก requesting objects.
ในเอกสารของ ZF จะให้
resource - แทน object ที่จะถูกควบคุมการเข้าถึง
role - จะหมายถึง object ซึ่งจะร้องขอการเข้าถึง resource

Zend_Application


ช่วยให้บรรจุ configuration เริ่มต้นของโปรแกรมได้สะดวก จัดหาทรัพยากรที่นำกลับมาใช้ได้ใหม่
คลาสที่เรียบง่ายและมีความเป็นโมดูล และช่วยในการตรวจสอบ dependency. มันเอาใจใส่กับการสร้าง
environment ของ PHP และตั้งค่าช่วยในการทำให้โปรแกรม autoloding โดย default

Zend_Amf


ช่วยในการเชื่อมต่อกับ Adobe's Action Message Format (AMF)
, เพื่อให้ Flash Player ติดต่อกับ PHP ได้.
มันจัดสรรส่วนการทำงานส่วน gateway server สำหรับควบคุมการ request จาก Flash Player ไปยัง server
และเทียบเคียง request เหล่านั้นไปยัง object และ method ของ class และ callback function ที่ผู้เขียนโปรแกรมสร้างขึ้นมาเอง
การใช้งาน AMFPHP กับ ActionScript 2.0

AMF3 sepecification มีให้อ่านได้ฟรี, ซึ่งช่วยอธิบายให้ทราบว่ามีชนิดของ message อะไรซึ่งสามารถจะส่งไปมาระหว่าง
Flash Player และ server

Zend_Captcha


CAPTCHA เป็นคำย่อของ "Completely Automated Public Turing test to tell Computers and Humans Apart";
มันถูกใช้เป็นเป็นตัวตรวจสอบความสิทธิรูปแบบหนึ่ง เพื่อให้แน่ใจว่าผู้ที่ป้อนข้อมูลเข้ามาแต่ละครั้งเป็นมนุษย์ไม่ใช้โปรแกรมส่งข้อมูลอัตโนมัติ.
โดยทั่วไป, captcha จะถูกใช้พร้อม form กรอกข้อมูลซึ่งอนุญาตให้ใช้งานโดยไม่จำเป็นต้อง login เข้าสู่ระบบสมาชิก แต่คุณต้องการป้องการการส่งข้อมูลจาก spam

โค้ดตัวอย่าง captcha ที่เป็น text ธรรมดาๆ ครับ :)
จริงๆ ควรจะใช้ร่วมกับ Zend_Form นะครับ แต่เนื่องจากผมอยากให้มันดูธรรมดาๆ อันไหนเขียนแบบธรรมดาๆ ได้ก็เขียนแบบธรรมดาๆ ไปเลย

<?php
ini_set('include_path','.'.PATH_SEPARATOR.'c:\mydoc\testweb\zend\library');
require_once 'Zend/View.php';
require_once 'Zend/Captcha/Figlet.php';
require_once 'Zend/Session.php';
$view = new Zend_View();
$captcha = new Zend_Captcha_Figlet(array(
'name' => 'code',
'wordLen' => 6,
'timeout' => 30000
));
if (isset($_POST['captcha'])){
if ($captcha->isValid($_POST['captcha'])) {
echo 'correct!';
}else{
$msg=$captcha->getMessages();
echo current($msg);
}
}
$id = $captcha->generate();
echo '<span style="font-family:fixedsys">';
echo $captcha->render($view);
echo '</span>';
?>
<form action="?" method="post">
<input type="hidden" name="captcha[id]" value="<?php echo $id;?>" id="captcha-id" />
<input type="text" name="captcha[input]" id="captcha-input" value="" />
<input type="submit" value="test">
</form>


Zend_Pdf



<?php
header("Content-type: application/pdf");
ini_set('include_path','.'.PATH_SEPARATOR.'c:\mydoc\testweb\zend\library');
require_once 'Zend/Pdf.php';

$pdf1 = new Zend_Pdf();
$pdfPage = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
$pdfPage->setFont(Zend_Pdf_Font::fontWithPath('2005_iannnnnMTV.ttf'), 36);
$pdfPage->drawText('สวัสดีครับท่าน', 50, 842-50, 'UTF-8');
$pdf1->pages[]=$pdfPage;
echo $pdf1->render();


Zend_Soap_Client


อันนี้เป็นตัวอย่างการใช้งาน Zend_Soap_Client คนที่เคยใช้ dotnet xml จะทราบว่า xml ของ dotnet มันไม่ค่อย standard เท่าไหร่
คือ ' หรือ " มันจะไม่ถูกแปลงข้อมูลไปเป็น &quot; อะไรพวกเนี้ยลืมแระ และคิดว่า soap ของ dotnet ก็คงจะไม่ standard อีกเช่นเคย
Zend Framework ก็เลยสร้าง class Zend_Soap_Client_DotNet เสริมขึ้นมาจาก Zend_Soap_Client ธรรมดาเพื่อจัดการ xml format ของ dotnet โดยเฉพาะ ครับ :)
โปรแกรมนี้เรียกใช้ webservice จากเว็บ http://www.webservicex.com ครับ โดยอันนี้จะเป็น service แปลงอัตราแลกเปลี่ยนค่าเงินบาทกับเงินยูเอสดอลล่าครับ

<?php
ini_set('include_path','.'.PATH_SEPARATOR.'c:\mydoc\testweb\zend\library');
$wsdl = 'http://www.webservicex.com/CurrencyConvertor.asmx?wsdl';
require_once 'Zend/Soap/Client/DotNet.php';
$client = new Zend_Soap_Client_DotNet($wsdl);
$result = $client->__call('ConversionRate',array(
'FromCurrency'=>'USD',
'ToCurrency'=>'THB'
));
var_dump($result);
?>

โครงสร้าง MVC ของ Zend Framework

MVC


M (Model) คือเนื้อหาที่อยู่ใน application เช่น data, web service, rss feed
V (View) คือส่วนแสดงผลที่แสดงต่อผู้ใช้
C (Controller) คือส่วนจัดการกับการ request และควบคุมการทำงาน

Controller สามารถบอกให้ View แสดงผลต่างๆ ในแบบต่างๆ
View สามารถส่งข้อมูลไปให้กับ Controller

Controller สามารถดึงเอาข้อมูลจาก Model มาใช้ในการตัดสินใจต่างๆ
และสามารถส่งข้อมูลไปให้ Model เพื่อให้บันทึกข้อมูล

View สามารถเข้าถึง Model เพื่อดึงเอาข้อมูลมาใช้ แต่ไม่สามารถส่งข้อมูลเพื่อให้ Model บันทึกข้อมูล


Front Controller


จัดการกับการ requests
ส่งต่อการควบคุมให้กับ Action Controller
คืนค่า response

Zend_Controller


เกี่ยวกับ Controller
ในการสร้าง Action Controller เราจะสร้าง class ซึ่ง extends Zend_Controller_Action
ในการตั้งชื่อ class เราจะตั้งชื่อให้ลงท้ายว่า Controller ยกตัวอย่างเช่น IndexController
ตัวอักษร _ ในชื่อ class จะแทน subdirectory อย่างเช่น Test_MyController จะหมายถึง Test/MyController.php
URL ของ DoraemonNobitaController คือ /doraemon-nobita หรือ /doraemon.nobita หรือ /doraemonIndex

เกี่ยวกับ Action
คือ method ที่ controller นำมาใช้งาน
โดยจะต้องเป็น public method ที่มีชื่อลงท้ายว่า Action เช่น indexAction(), viewAction()
URL ของ magicItemAction() คือ /doraemon-nobita/magic-item หรือ /doraemon-nobita/magic.item หรือ /doraemon-nobita/magic_item

เกี่ยวกับ Modules
คือกลุ่มของ action controllers, models และ views ที่สัมพันธ์กัน
โครงสร้าง directory จะสัมพันธ์กับโครงสร้าง application directory
ชื่อคลาสของ Controller ควรจะนำหน้าด้วยชื่อ Module
  ยกตัวอย่างเช่น Cartoon_DoraemonController.php ซึ่งจะมีโครงสร้าง directory เป็น Cartoon/controller/DoraemonController.php
ชื่อของ Module จะต้องเป็น camel case เช่นเดียวกับชื่อของ Controller

Request Object  จะบรรจุข้อมูลเกี่ยวกับการ request เอาไว้ทั้งหมด
Router จะแยกข้อมูลจากการ request เช่น url ออกเป็น token ที่แสดงถึง action, controller และ module ของการ request
Dispatcher นำเอา token ที่ได้จากการ routing มาเป็นข้อมูลในการเลือก คลาส metod ของ action controller, จากนั้นจึงสั่งให้มันทำงาน
Response Object จะบรรจุข้อมูล response ที่สมบูรณ์ และมีความสามารถในการส่งข้อมูลกลับไปให้ user

Dispatch Loop

  1. Zend_Controller_Front::getInstance()->dispatch(); จัดการกับ request ที่เข้ามา

  2. สร้าง ออปเจค request และ ออปเจค response ถ้ายังไม่ได้ถูกสร้างขึ้น

  3. ทำการ route request

  4. เข้าสู่ dispatch loop

    • ในการ dispatch action ประกอบด้วยการสร้างออปเจค controller และการเรียก method ของ controller

    • ซึ่งจะทำการ dispatch ไปเรื่อยๆ จนกว่าออปเจค request จะรายงานกลับมาว่าไม่มี action ที่จะต้อง dispatch อีก



  5. คืนค่า response



Routing
default routing ได้แก่
/controller/action/key1/value1/key2/value2
/module/controller/action/key1/value1/key2/value2

การแก้ไข routing เรียกว่า Rewrite Router
Zend_Controller_Router_Rewrite คือส่วนการทำงานของ router ปกติ
อนุญาตให้เพิ่ม named route ได้มากเท่าที่ต้องการ
named route จะช่วยให้สามารถทำงานเกี่ยวกับการ route ได้ในภายหลัง การรวมกันเป็น URL
และการวิเคราะห์หาว่ามีข้อมูลอะไรบ้างที่อยู่ใน URL
การ Route ถูกประมวลผลแบบ LIFO (route ที่นิยามทีหลังสุดจะถูกประมวลผลเป็นอันดับแรก)
Route interface ช่วยให้คุณสามารถสร้างชนิดของการ route ที่จะใช้กับ application ของคุณได้เอง


ชนิดของ Route ที่ได้เตรียมไว้ให้
static: เทียงเคียงอย่างตรงไปตรงมา, ส่งต่อการทำงานสอดคล้องกับการ default route
standard: เทียงเคียงกับส่วนประกอบใน named URL, ยืดหยุ่นและอ่านได้ง่าย แต่ว่าแต่ะละส่วนประกอบจะต้องใช้ regexp ในการเทียบเคียงจึงทำงานค่อนข้างช้า
regex: เทียบเคียงโดยใช้ PCRE, มีความยืดหยุ่นสูงและเร็วกว่าแบบ standard แต่สร้างยากกว่าแบบ standard

ลองดูตัวอย่างการ route นะครับ อันนี้เป็น standard route
เมื่อเพิ่มโค้ดนี้ลงใน c:/www/quick/application/bootstrap.php นะครับ
(ก่อนคำสั่ง unset($frontController, $view, $configuration, $dbAdapter, $registry);)
ก็จะทำให้ http://localhost/quick/public/homepage ชี้ไปที่ controller index, action index ครับผม
หรือ http://localhost/quick/public/homepage ชี้ไปที่เดียวกับ http://localhost/quick/public ครับ

// Create a router
$router = $frontController->getRouter(); // returns a rewrite router by default
$router->addRoute(
'myRouteName',
new Zend_Controller_Router_Route('homepage',
array('controller' => 'index',
'action' => 'index'))
);

Action Controllers


คือคลาสที่ extends จาก Zend_Controller_Action
ใช้กำหนด public action (method ที่ลงท้ายด้วย Action) ที่ต้องการให้ controller ควบคุม
สร้าง public method ถ้าต้องการจะนำกลับมาใช้ใหม่ หรือหากต้องการให้ทดสอบโปรแกรมได้สะดวก

ใน Action Controller เราสามารถสร้าง method เพื่อ listen และแทรกคำสั่งใน event ต่อไปนี้
init(): method ที่จะถูกเรียกเมื่อ object ของ Action Controller ถูกสร้างขึ้น
preDispatch(): method ที่จะถูกเรียกก่อนการ dispatch action
postDispatch(): method ที่จะถูกเรียกหลังการ dispatch action

Utility Methods
_forward : ใช้เพื่อย้ายไปยัง action อื่น
_redirect : ใช้เพื่อย้ายไปยัง url อื่น
render : ใช้เพื่อเปลี่ยนไปใช้ view อื่นแทน default view
__call : สามารถ override method นี้เพื่อควบคุมการทำงาน undefined action ว่าจะให้ forward ไปยัง action ไหน

ViewRenderer


เป็น action helper ตัวนึงที่ ZF จะสร้าง object ขึ้นมาให้โดยอัตโนมัติ
property view ($this->view) ของ controller คือออปเจค view
เราสามารถกำหนดค่าตัวแปรให้ view โดยใช้คำสั่ง
$this->view->varname = $varname;

View Script จะถูกนำมาแสดงผลโดยอัตโนมัติหลังจาก event postDispatch()
view script จะมีชื่อตาม controller และ action
ตัวอย่างเช่น DoraemonController::showItemsAction จะมีไฟล์ view script คือ /view/scripts/doraemon/show-items.phtml
เราสามารถใช้คำสั่ง setNoRender() เพื่อบอกให้ controler ไม่นำ view script มาแสดงผลโดยอัตโนมัติ

คำสั่งเกี่ยวกับ ViewRenderer
setView
setViewSuffix
setView(Base|Script)PathSpec()
setResponseSegment()


Plugins


plugin จะถูกเรียกใน event ของ front controller
มีการแทรก event ในทุกๆ ขบวนการหลักของ front controller
ช่วยให้สามารถสร้าง action แบบ global (แทรกคำสั่งเพิ่มเข้าไปในทุกๆ action)

event
routeStartup(): ทำงานก่อนการ route
routeShutdown(): ทำงานหลังการ route
dispatchLoopStartup(): ทำงานก่อน รอบแรกของ dispatch loop
preDispatch(): ทำงานก่อนการ dispatch action
postDispatch(): ทำงานหลัง dispatch action
dispatchLoopShutdown(): ทำงานหลังจากที่ dispatch loop วนซ้ำจบ

การสร้าง plugins
ให้สร้างคลาส extends Zend_Controller_Plugin_Abstract
เขียน override event ลงใน คลาสดังกล่าว โดยอาจจะเขียนเพียง event เดียวหรือหลาย event ก็ได้


Action Helpers


Action Helper คือคลาสที่รวมเอา function ที่นำกลับมาใช้ได้ใหม่ในหลายๆ controller

การสร้าง Action Helper
สร้างคลาส extends Zend_Controller_Action_Helper_Abstract
ส่วนสุดท้ายของชื่อคลาสคือชื่อของ helper
ตัวอย่างเช่น My_Helper_SuperMan 'SuperMan' คือชื่อของ helper
สร้าง method direct() เพื่อให้ helper ถูกเรียกราวกับว่าเป็น mehod ของ helper broker

การใช้ Action Helper เป็น Action Controller Event Listener

init(): เมื่อ action controller ถูกสร้างขึ้น
preDispatch(): ทำงานหลังจาก preDispatch() ของ front controller plugins แต่ทำงานก่อน preDispatch() ของ action controller
postDispatch(): ทำงานหลังจาก postDispatch() ของ action controller แต่ทำงานก่อน postDispatch() ของ front controller plugins

โน๊ต : event listener ของ helper จะถูก trigger ก็ต่อเมื่อได้ทำการลงทะเบียนกับ broker


Zend_View


สร้างคลาสจาก Zend_View_Interface เพื่อสร้าง template engine ของคุณเอง
default คลาส Zend_View ใช้ ภาษา PHP เป็น template language
สามารถกำหนดค่าตัวแปร view ราวกับว่ามันเป็นสมาชิกของ object อย่างเช่น $view->varname = $varname;
ใช้งานตัวแปร view ใน view script ได้จากออปเจค $this ตัวอย่างเช่น <?= $this->varname ?>


View Helper


เป็นคลาสซึ่ง ขยายความสามารถให้กับ Zend_View
ตัวอย่างเช่นช่วย นำเข้า rss feed มายังเว็บเพจ
จัดรูปแบบ text ให้อยู่ในรูปแบบ xhtml
ช่วยแสดงผลตามเงื่อนไขที่ตั้งไว้อย่างเช่นแสดงผลปุ่ม login ถ้ายังไม่ได้ login
ส่วนแสดงผลที่สามารถนำกลับมาใช้หลายๆ ครั้ง เช่นกล่อง serach

การใช้ View Helper
เรียกใช้งาน Vew Helper ราวกับว่ามันเป็น method ของออปเจค View
<?= $this->formText('username') ?>

การสร้างและใช้งาน View Helper
ชื่อคลาส My_View_Helper_Coffee จะมีชื่อ helper เป็น coffee
ลงทะเบียน helper paths กับออปเจค Zend_View

จะต้องมีชื่อ method ชื่อเดียวกับ helper ดังนี้

<?php
class My_View_Helper_Coffee
{
  function coffee(){
  }
}
?>


View Filter


ช่วยในการกรอง content ก่อนจะนำไปแสดงผลจริงๆ
เหมือนกับ View Helper คือจะต้องสร้างคลาสซึ่งมี 1 method ต่อ 1 class
ประโยชน์ของ Filter เช่นแปลง HTML เป็น PDF, JSON, ลบแท็ก script ออกไปจาก content

Zend_Model


ตัวอย่างของ Model ได้แก่ Database, Web Service, RSS Feeds, FileSystem, Images

Zend Framework ระบุถึง Model ได้อย่างไร
ZF ยังไม่ได้สร้าง Model ที่เป็นแบบทั่วไปขึ้นมา
แต่ ZF สนับสนุน Model ที่มีความเฉพาะเจาะจงตัวอย่างเช่น Zend_Db_Table, Zend_Service, Zend_Feed


reference
Ophinney_Matthew_2007-ZendCon-MVC.pdf

ปฎิทินกิจกรรม php calendar


<?php
function echoDate($y,$m,$d,$acts){
$ymd = sprintf("%04d-%02d-%02d",$y,$m,$d);
echo array_key_exists($ymd,$acts)
?'<a href="act.php?date='.$ymd.'" onclick="alert(\''.$acts[$ymd].'\');return false;">'.$d.'</a>'
:$d;
}
$y = empty($_GET['year'])?date('Y'):(int)$_GET['year'];
//สมมุติว่า $acts มาจาก database
$acts = array(
'2009-08-12'=>'activity1',
'2009-12-05'=>'activity2',
);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>calendar of year <?php echo $y;?></title>
<style type="text/css">
.w-s {color:red;}
.w-m {color:yellow;}
.w-tu {color:pink;}
.w-w {color:green;}
.w-th {color:orange;}
.w-f {color:blue;}
.w-st {color:violet;}
</style>
</head>
<body>
<h1 align="center">calendar of year <?php echo $y;?></h1>
<table cellpadding="10" align="center">
<?php
for($m=1;$m<=12;$m++):
$num = cal_days_in_month(CAL_GREGORIAN, $m, $y);
$fd = date('w',mktime(1, 1, 1, $m, 1, $y));
$d = 0;
?>
<?php if ($m%4 == 1):?>
<tr valign="top">
<?php endif;?>
<td>

<table border="1">
<caption><?php echo $m;?></caption>
<!-- first week -->
<tr><td class="w-s">s</td><td class="w-m">m</td><td class="w-tu">t</td>
<td class="w-w">w</td><td class="w-th">t</td><td class="w-f">f</td><td class="w-st">s</td>
<tr>
<?php if ($fd>0):?>
<td colspan="<?php echo $fd;?>">&nbsp;</td>
<?php endif;?>
<?php while($fd + $d++ < 7): ?>
<td><?php echoDate($y,$m,$d,$acts);?></td>
<?php endwhile; $d--; ?>
</tr>
<!-- second to fifth week -->
<?php for($x=0;$x<31;$x++): $xx=$x%7; ?>
<?php if ($xx==0):?><tr><?php endif;?>
<td><?php $d++; echoDate($y,$m,$d,$acts);?></td>
<?php if ($xx==6 || $d==$num):?>
<?php if ($xx!=6):?>
<td colspan="<?php echo 6-$xx;?>">&nbsp;</td>
<?php endif;?>
</tr>
<?php if ($d==$num){break;} ?>
<?php endif;?>
<?php endfor;?>
</table>

</td>
<?php if ($m%4 == 0):?>
</tr>
<?php endif;?>
<?php endfor;?>
</table>

<div align="center">
<?php foreach(range(2000,2021) as $year):?>
<a href="?year=<?php echo $year;?>"><?php echo $year;?></a>
<?php endforeach;?>
</div>
</body>
</html>

วันอาทิตย์ที่ 5 ธันวาคม พ.ศ. 2553

class DbExpress


<?php
/*
คลาส DbExpress จะนำข้อมูล $_POST มาสร้าง คำสั่ง query ให้โดยอัตโนมัติครับ
ทำให้เขียนโค้ดสะดวกขึ้นครับ ตอนนี้สร้างขึ้นเฉพาะคำสั่ง insert /update / delete
ส่วนคำสั่ง select จะเขียนในเวอร์ชั่นถัดไปครับ
มีคำสั่งช่วย escape string ป้องกัน sql injection
คลาสนี้ควรจะใช้ในสภาพแวดล้อมที่ magic quotes gpc off ครับ
*/

/**
*
* Description of DbExpress
*
* @author num ( http://web-programming-bookmark.blogspot.com )
* @version 0.5a
*/
class DbExpress
{
protected static $_debug;
public static function Debug($debug=true){
self::$_debug = $debug;
}
protected static $_con;
public static function Connect($host,$user,$password,$db){
if (empty(self::$_con)){
self::$_con = mysql_connect($host,$user,$password);
mysql_query('SET NAMES UTF8',self::$_con);
mysql_select_db($db,self::$_con);
}
return self::$_con;
}
public static function Close(){
if (is_resource(self::$_con))
mysql_close(self::$_con);
}
protected $_act;
protected $_cols;
protected $_table;
protected $_where;
protected function __construct($act,$table){
$this->_act = $act;
$this->_cols = array();
$this->_table = $table;
}
protected function Where(){
$args = func_get_args();
$w =& $this->_where;
if (func_num_args()==1){
$w = 'id = '.intval($_GET['id']);
}
if (func_num_args()==2){
$w = 'id = '.intval($args[1]);
}
if (func_num_args()==3){
if (is_scalar($args[2])){
$w = $args[1].' = '.self::qstr($args[2]);
} else {
$params = array_map(array($this,'qstr'),$args[2]);
array_unshift($params, str_replace('?','%s',$args[1]));
$w = call_user_func_array('sprintf', $params);
}
}
}
public static function Insert($table){
return new DbExpress('insert',$table);
}
public static function Update($table){
$o = new DbExpress('update',$table);
$params = func_get_args();
call_user_func_array(array($o,'Where'), $params);
return $o;
}
public static function Delete($table){
$o = new DbExpress('delete',$table);
$params = func_get_args();
call_user_func_array(array($o,'Where'), $params);
return $o;
}

public static function qstr($s){
return "'".mysql_real_escape_string($s,self::$_con)."'";
}
public function Query(){
if ($this->_act === 'insert'){
$names = implode(',',array_keys($this->_cols));
$values = implode(',',array_map(array($this,'qstr'),$this->_cols));
$q = sprintf('INSERT INTO %s(%s) VALUES(%s)', $this->_table, $names, $values);
} else {
//action
if ($this->_act === 'update'){
$q = '';
foreach($this->_cols as $name=>$val)
$q.= ','.$name.'='.self::qstr($val);
$q = 'UPDATE '.$this->_table.' SET '.substr($q,1).' WHERE '.$this->_where;
}
if ($this->_act === 'delete'){
$q = 'DELETE FROM '.$this->_table.' WHERE '.$this->_where;
}
}
return $q;
}
public function Save(){
$q = $this->Query();
if (self::$_debug)
echo '<br','>',htmlspecialchars($q);
mysql_query($q,self::$_con) or die(htmlspecialchars(mysql_error()));
}
protected function __get($name){
$this->_cols[$name] = empty($_POST[$name]) ? '' : $_POST[$name];
return $this;
}

}

//ตั้งค่า timezone เป็นเวลาประเทศไทย
date_default_timezone_set('Asia/Bangkok');

//จำลองข้อมูลจาก form
$_POST['title'] = 't1';
$_POST['name'] = 'n1\'';
$_POST['detail'] = 'd1';
$_POST['create_at'] = date('Y-m-d H:i:s');
$_GET['id'] = 1;


//ตัวอย่างการใช้งาน

//ต่อ database mysql
DbExpress::Connect('localhost', 'root', '12345', 'dbname');
DbExpress::Debug(true);

//ทำการ update ข้อมูลตาราง tbcontact เฉพาะ record ที่ฟิลด์ id=$_GET['id']
DbExpress::Update('tbcontact')
->title
->name
->detail
->create_at
->Save();

//ทำการ update ข้อมูลตาราง tbcontact เฉพาะ record ที่ฟิลด์ title='test'
DbExpress::Update('tbcontact','title','test\'')
->title
->name
->detail
->create_at
->Save();

//ทำการ update ข้อมูลตาราง tbcontact ตามเงื่อนไข
DbExpress::Update('tbcontact','id between ? and ?',array(18,20))
->title
->name
->detail
->create_at
->Save();

//ทำการ insert ข้อมูลตาราง tbcontact
DbExpress::Insert('tbcontact')
->title
->name
->detail
->create_at
->Save();

//ทำการ ลบ ข้อมูลตาราง tbcontact เฉพาะ record ที่ฟิลด์ title='ffffffff'
DbExpress::Delete('tbcontact','title','ffffffff')->Save();

DbExpress::Close();
?>

วันศุกร์ที่ 3 ธันวาคม พ.ศ. 2553

run mysql_close automatically

โดยปกติ mysql connection จะถูก close ให้อัตโนมัติ
แต่ในบาง server หรือบางครั้ง php จะไม่ปิด mysql connection ให้ในทันที
อาจจะต้องรอให้หลายวินาทีกว่าจะมีการปิดให้ ดังงนั้นเราจึงควรทำการปิด mysql_close เสมอ
อย่างไรก็ตามอาจจะไม่สะดวกถ้าเขียนคำสั่ง mysql_close ในทุกหน้าเราสามารถเขียนในไฟล์ include แทนได้ดังนี้


<?php
function always_run(){
mysql_close($GLOBALS['con']);
echo 'end of request. the connection is close automatically';
}
register_shutdown_function('always_run');

//connect to mysql
$con = mysql_connect('localhost','root','12345');

//มีการเรียก always_run อัตโนมัติ ที่จุดที่ script หยุดทำงาน เพื่อทำการปิด connection ให้

วันพฤหัสบดีที่ 2 ธันวาคม พ.ศ. 2553

ตั้งค่า ให้ login sql server ได้ทั้ง windows และ sql authentication

ที่ object explorer คลิ้กขวาที่ชื่อของ server และ properties และเลือก tab security เลือก server authentication เลือก sql server and window authentication mode

บางครั้ง user อาจจะ disable ทำให้ login ไม่ได้ให้คลิ้กที่ชื่อ user
ใน security > login จากนั้นแก้ให้ enable

วันพุธที่ 1 ธันวาคม พ.ศ. 2553

xp folder default view

http://www.tech-recipes.com/rx/807/xp-change-the-default-folder-view-make-all-folder-views-the-same/