appendからassignにSmartyの呼び出し方法を変更しましたが、結果はそれほど変わらず、Smartyの場合、Smarty文法の方が優先されるようだ。
ちょっと改造した方法
require_once( 'MySmarty.class.php');
$objSmarty =& new MySmarty;
if (!($cn = mysql_connect("localhost", "hoge", "hoge"))) {
die;
}
if (!(mysql_select_db("test"))) {
die;
}
$sql = "select * from address";
if (!($rs = mysql_query($sql))) {
die;
}
$i=0;
while ($item = mysql_fetch_array($rs)) {
$arg[]=array(
'id'=>$item['id'],
'name'=>$item['name'],
'cell'=>$item['tel'],
'email'=>$item['email'],
);
}
mysql_close($cn);
$objSmarty->assign('contacts', $arg);
$objSmarty->display('hoge1.tmpl');
Smarty MySQLとの連携方法1
Smarty MySQLとの連携方法2
Showing posts with label Smarty. Show all posts
Showing posts with label Smarty. Show all posts
Thursday, May 10, 2007
Smarty MySQLとの連携方法2
前回紹介した方法より若干速度が速い。ただしプログラム側にデータベースのカラムを追加する必要がある。
array配列を使用したSmaryのassignのサンプル sectionの場合
---php---
if (!($cn = mysql_connect("localhost", "hoge", "hoge"))) {
die;
}
if (!(mysql_select_db("test"))) {
die;
}
$sql = "select * from address";
if (!($rs = mysql_query($sql))) {
die;
}
$id= array();
$name= array();
$tel= array();
$email= array();
while ($item = mysql_fetch_array($rs)) {
array_push($id,$item['id']);
array_push($name,$item['name']);
array_push($tel,$item['tel']);
array_push($email,$item['email']);
}
$objSmarty->assign('id',$id);
$objSmarty->assign('name',$name);
$objSmarty->assign('tel',$tel);
$objSmarty->assign('email',$email);
mysql_close($cn);
$objSmarty->display('html.tmpl');
---html----
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF8">
<title>テンプレート</title>
</head>
<body>
<hr />
{section name=customer loop=$id}
<p>
name: {$name[customer]}<br />
id: {$id[customer]}<br />
cell: {$tel[customer]}<br />
e-mail: {$mail[customer]}
</p>
{/section}
</body>
</html>
array配列を使用したSmaryのassignのサンプル sectionの場合
---php---
if (!($cn = mysql_connect("localhost", "hoge", "hoge"))) {
die;
}
if (!(mysql_select_db("test"))) {
die;
}
$sql = "select * from address";
if (!($rs = mysql_query($sql))) {
die;
}
$id= array();
$name= array();
$tel= array();
$email= array();
while ($item = mysql_fetch_array($rs)) {
array_push($id,$item['id']);
array_push($name,$item['name']);
array_push($tel,$item['tel']);
array_push($email,$item['email']);
}
$objSmarty->assign('id',$id);
$objSmarty->assign('name',$name);
$objSmarty->assign('tel',$tel);
$objSmarty->assign('email',$email);
mysql_close($cn);
$objSmarty->display('html.tmpl');
---html----
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF8">
<title>テンプレート</title>
</head>
<body>
<hr />
{section name=customer loop=$id}
<p>
name: {$name[customer]}<br />
id: {$id[customer]}<br />
cell: {$tel[customer]}<br />
e-mail: {$mail[customer]}
</p>
{/section}
</body>
</html>
Smarty MySQLとの連携方法1
この方法のメリットはデータベースのカラム名はテンプレートのみに記述するのでプログラム管理が簡単になる。ただし、後から紹介する方法の方が速度は若干速い。
--PHP部分--
require_once( 'MySmarty.class.php');
$objSmarty =& new MySmarty;
if (!($cn = mysql_connect("localhost", "hoge", "hoge"))) {
die;
}
if (!(mysql_select_db("test"))) {
die;
}
$sql = "select * from address";
if (!($rs = mysql_query($sql))) {
die;
}
while ($item = mysql_fetch_array($rs)) {
$objSmarty->append('contacts',$item);
}
mysql_close($cn);
$objSmarty->display('hoge1.tmpl');
---hoge1.tmpl---
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF8">
<title>テンプレート</title>
</head>
<body>
<hr />
{section name=customer loop=$contacts}
<p>
name: {$contacts[customer].name}
id: {$contacts[customer].id}
cell: {$contacts[customer].tel}
e-mail: {$contacts[customer].email}
</p>
{/section}
</body>
</html>
--PHP部分--
require_once( 'MySmarty.class.php');
$objSmarty =& new MySmarty;
if (!($cn = mysql_connect("localhost", "hoge", "hoge"))) {
die;
}
if (!(mysql_select_db("test"))) {
die;
}
$sql = "select * from address";
if (!($rs = mysql_query($sql))) {
die;
}
while ($item = mysql_fetch_array($rs)) {
$objSmarty->append('contacts',$item);
}
mysql_close($cn);
$objSmarty->display('hoge1.tmpl');
---hoge1.tmpl---
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF8">
<title>テンプレート</title>
</head>
<body>
<hr />
{section name=customer loop=$contacts}
<p>
name: {$contacts[customer].name}
id: {$contacts[customer].id}
cell: {$contacts[customer].tel}
e-mail: {$contacts[customer].email}
</p>
{/section}
</body>
</html>
Smarty 独自ファイルクラス化
PHP5.0/7.Smartyを使う
HTMLファイルの変更がすぐに反映されないので以下を追加
<:?php
require('Smarty/Smarty.class.php');
class MySmarty extends Smarty {
function MySmarty () {
$this->Smarty();
$this->compile_check = true; // << ここを追加
$mydir = dirname(__FILE__);
$this->template_dir = "$mydir/templates/";
$this->compile_dir = "$mydir/templates_c/";
$this->config_dir = "$mydir/configs/";
$this->cache_dir = "$mydir/cache/";
$this->caching = 0;
}
}
?>
HTMLファイルの変更がすぐに反映されないので以下を追加
<:?php
require('Smarty/Smarty.class.php');
class MySmarty extends Smarty {
function MySmarty () {
$this->Smarty();
$this->compile_check = true; // << ここを追加
$mydir = dirname(__FILE__);
$this->template_dir = "$mydir/templates/";
$this->compile_dir = "$mydir/templates_c/";
$this->config_dir = "$mydir/configs/";
$this->cache_dir = "$mydir/cache/";
$this->caching = 0;
}
}
?>
Subscribe to:
Posts (Atom)