Smartyアプリケーション

下記の場所に配置することにします。
─┬─htdocs/ ─┬─index.html
└─SampleDb/ ───index.php
└─Smarty/ ─── SampleDb/─┬─ templates/──index.tpl
├─templates_c/
├─configs/
└─cache/


データベース読込み
PEAR::DBを使ってデータベース情報をselectして表示するプログラムです。
プログラムを作成します(下記を入力したファイルindex.phpを作成)。
<?php
require_once('Smarty.class.php');
require_once('DB.php');

//エラーが発生したときに実行される関数
function errorHandler($error){
 echo "<p>エラー発生 {$error->getMessage()}</p>";
 exit;
}
//errorHandlerを使うように設定
PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'errorHandler');

$smarty = new Smarty;

// smarty config
$smarty->template_dir= '../../Smarty/SampleDb/templates/';
$smarty->compile_dir = '../../Smarty/SampleDb/templates_c/';
$smarty->config_dir = '../../Smarty/SampleDb/configs/';

// assign some content.
$smarty->assign('title', 'SampleDb-DB');
$smarty->assign('name', 'SampleDb-DB');

// データベースライブラリで接続
$db=DB::connect('mysql://root:PASWD@localhost/personal?charset=ujis');

// 文字化け対策
$db->query("set names ujis");

// 全件の取り出し
$query ='SELECT * FROM nametable';
$array =& $db->getAll($query);

// 開放する
if($db) $db->disconnect() ;

// Smartyにデータを渡す
$smarty->assign('count', count($array));
$smarty->assign('data', $array);

// display it
$smarty->display('index.tpl');
?>

(下記を入力したテンプレート・ファイルindex.tplを作成)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-JP">
<title>{$title}</title>
</head>
<body>
 名前:{$name}
 日付:{$smarty.now|date_format:"%Y年 %m月 %d日 "}
 データ数:{$count}

<table border="1">
<tr style="color:#ffffff" bgcolor="#a0a0a0">
<td>ID</td>
<td>Yomi</td>
<td>Name</td>
<td>Phone</td>
<td>Fax</td>
<td>Zip</td>
<td>Address</td>
<td>Rem</td>
</tr>
{foreach from=$data item=dt}
<tr>
<td>{$dt[0]}</td>
<td>{$dt[1]}</td>
<td>{$dt[2]}</td>
<td>{$dt[3]}</td>
<td>{$dt[4]}</td>
<td>{$dt[5]}</td>
<td>{$dt[6]}</td>
<td>{$dt[7]}</td>
</tr>
{/foreach}
</table>

</body>
</html>

アプリケーションの実行
ブラウザで、http://localhost/SampleDb/index.phpへアクセスすると以下のように表示されます。
 
表示内容はデータベースに登録されている情報が表示されます。