แสดงบทความที่มีป้ายกำกับ Smarty แสดงบทความทั้งหมด
แสดงบทความที่มีป้ายกำกับ Smarty แสดงบทความทั้งหมด

29 สิงหาคม 2554

Smary template แสดงข้อมูล จาก Database

หลังจากได้อ่านบทความต่าง ๆ ของ Smarty template ไปบ้างเล็ก ๆ น้อย ๆ วันนี้จะเป็นการนำข้อมูลจากฐานข้อมูลมาแสดง โดยการส่งข้อมูลออกมาเป็น Array

ซึ่งตัวอย่างในการใช้งานดังต่อไปนี้ :

- สร้างไฟล์ test_2.php ขึ้นมาโดยเขียนโค้ด ด้านล่างลงไป

<?php
require'libs/Smarty.class.php';

$hostname = 'localhost';
$database = 'phithan_appointment';
$user = 'root';
$pass = '';
$connection = mysql_connect($hostname, $user, $pass) or trigger_error(mysql_error(),E_USER_ERROR); //+ เชื่อมต่อฐานข้อมูล
mysql_select_db($database,$connection);
mysql_query("SET NAMES UTF8");

$sql = 'SELECT * FROM tbl_appointment LIMIT 20'; //+ เลือกมาแค่ 20 แถว
$rs_appointment = mysql_query($sql);

while($row_appointment = mysql_fetch_array($rs_appointment)){
$appointment[$row_appointment['app_id']] = $row_appointment['app_customer']; //+ ทำเป็น Array
}

$smarty = new Smarty();
$smarty->assign('appointment', $appointment);
$smarty->display('test_2.tpl'); //+ ส่งข้อมูลออกไปที่ test_2.tpl เืพื่อทำการแสดงข้อมูล
?>

- สร้างไฟล์ test_2.tpl ไว้ภายในโฟลเดอร์ templates แล้วเขียนโ้ค้ดด้านล่างลงไป

<html>
<head>
<title>Test Smary</title>
</head>
<body>
<table>
{foreach from=$appointment key=k item=i}
<tr><td>{$k}</td><td>{$i}</td></tr>
{/foreach}
</table>
</body>
</html>

* key เปรียบได้เป็น index ของ Array และ item คือ Value นั่นเอง


หรือจะเป็นการส่งข้อมูลจากฐานข้อมูลแบบหลาย ๆ คอร์ลัม ดังตัวอย่างด้านล่าง

- ไฟล์ test_2.php

<?php
require'libs/Smarty.class.php';

$hostname = 'localhost';
$database = 'phithan_appointment';
$user = 'root';
$pass = '';
$connection = mysql_connect($hostname, $user, $pass) or trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db($database,$connection);
mysql_query("SET NAMES UTF8");

$sql = 'SELECT * FROM tbl_appointment LIMIT 20';
$rs_appointment = mysql_query($sql);

while($row_appointment = mysql_fetch_array($rs_appointment)){
$appointment[$row_appointment['app_id']] ['app_customer'] = $row_appointment['app_customer'];
$appointment[$row_appointment['app_id']] ['app_register'] = $row_appointment['app_register'];
$appointment[$row_appointment['app_id']] ['app_check'] = $row_appointment['app_check'];
}

$smarty = new Smarty();
$smarty->assign('appointment', $appointment);
$smarty->display('test_2.tpl');
?>

- ไฟล์ test_2.tpl

<html>
<head>
<title>Test Smary</title>
</head>
<body>
<table>
{foreach from=$appointment key=k item=i}
<tr><td>{$k}</td><td>{$i.app_register}</td><td>{$i.app_customer}</td><td>{$i.app_check}</td></tr>
{/foreach}
</table>
</body>
</html>


เท่านี้ก็เป็นตัวอย่างเบื้องต้นสำหรับการนำข้อมูลจากฐานข้อมูลออกมาแสดงด้วย Smarty template ครับ !

26 สิงหาคม 2554

เริ่มต้นการใช้งาน Smary template !

ก่อนที่จะเริ่มต้นการใช้งาน ต้องไปทำการดาวน์โหลด Class มาใช้งานกันก่อนที่ http://www.smarty.net/ (Smarty-3.0.8)
หลังจากดาวน์โหลดมาแล้วทำการแตกไฟล์ออก ซึ่งภายในจะประกอบไปด้วย
- demo
- libs
- README
- SMARTY2_BC_NOTES
- COPYING.lib
- change_log.txt

โฟลเดอร์ที่สำคัญคือ libs ซึ่งภายในจะมี ไฟล์ Smarty.class.php ซึ่งจำเป็นต่อการใช้งาน ต่อไปก็เริ่มทำการทดสอบการใช้งานลองดูเลย

- ก็อบปี้ โฟลเดอร์ Smarty-3.0.8 ที่ได้หลังจากการแตกไฟล์ (ซึ่งผมตัดชื่อให้เหลือแค่ Smarty)ไปไว้ที่ เซิฟเวอร์จำลอง โฟลเดอร์ WWW
- สร้างไฟล์ test.php ไว้ภายในโฟลเดอร์ Smarty แล้วก็เขียนคำสั่งนี้ลงไป

<?php
require'libs/Smarty.class.php'; // แทรก Class Smarty ลงไป

class Contacts{
var $id = 1;
var $name = 'Chaiwit';
var $email = 'lionblue_wit_99 at hotmail dot com';
var $phone = '083-130-43xx';
}

$contacts = new Contacts;

$smarty = new Smarty();

$smarty->assign('title', 'Contact Detail'); // ตัวแปร, ค่าที่ส่งไป

$smarty->assign('contact',$contacts); // ตัวแปร, ค่าที่ส่งไป

$smarty->display('test.tpl'); // อ้างอิงการเรียกใช้งาน Template
?>

- สร้างโฟลเดอร์ templates เพื่ออ้างอิง Template ที่เราเรียกใช้จากไฟล์ test.php โดยในไฟล์ test.php เราเรียกใช้งานไฟล์ชื่อ test.tpl ฉะนั้นเราก็ต้องสร้างไฟล์ test.tpl เก็บไว้ในโฟลเดอร์ templates แล้วก็เขียนโค้ดนี้ลงไป

<html>
<head>
<title>{$title}</title>
</head>
<body>
{$contact->id}<br>
{$contact->name}<br>
{$contact->email}<br>
{$contact->phone}<br>
</body>
</html>

หมายเหตุ คำสั่ง $contact->id คือ คำสั่งที่ใช้แสดง property ค่าของ Class ที่เราส่งมา

หลังจากนั้นก็ทดลองรับ สคริป นี้ที่ http://localhost/smarty/test.php ก็จะได้ผลลัพย์ดังรูปด้านล่าง



* Smarty template case sensitive เรื่องการส่งตัวแปร ตัวอักษรเล็ก ตัวอักษรใหญ่

อ่านเนื้อหาเพิ่มเติมได้ที่ : http://www.phpeveryday.com/articles/Smarty-Template-Engine-Tutorial-P849.html