[PHP] SmartyでDBのデータを一覧表示

○Smarty
 PHPで使用するテンプレートエンジン
 基本的な使い方は↓
 https://www.javadrive.jp/smarty/

ここで、Smartyでも、関数やif/for構文等、
使えることを最近知り、

DBのテーブルを簡単に一覧表示できるんじゃないかと
思ったので作ってみます。

以下のように記述
 index.php : メインのプログラム
 TestDB.php : DBアクセス用のクラス
 test.tpl : テンプレート

======== index.php ================
<?php
require_once ("[Smartyのパス]/Smarty.class.php");
require_once ("TestDB.php");

$testdb = new TestDB();
$keys = $testdb->get_keys();
$list = $testdb->get_list();
$values = [
       'title' => 'test',
       'head' => 'テスト',
       'keys' => $keys,
       'list' => $list
];

$s = new Smarty();
$s->assign ('values', $values);
$s->display('test.tpl');
======== TestDB.php ================

<?php

class TestDB {
       private $pdo;
       const DSN = 'mysql:dbname=test; host=localhost; charset=utf8';
       const USER = [SQLのユーザー名];
       const PASS = [パスワード];
       
       public function __construct(){
              try {
                     $this->pdo = new PDO(self::DSN, self::USER, self::PASS);
                     $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
              } catch(PDOException $Exception) {
                     die('エラー:' . $Exception->getMessage());
              }
              
       }
       
       
       public function get_list(){
       
              try {
                           
                     $sql = 'select * from test1';
                     $stt = $this->pdo->prepare($sql);
                     $stt->execute();
                           
                     if ($list = $stt->fetchAll(PDO::FETCH_ASSOC) ){
                           return $list;
                     }
                           
              } catch(PDOException $Exception) {
                     die('エラー:' . $Exception->getMessage());
              }
       
       }
       
       
       public function get_keys(){
              
              try {
              
                     $sql = 'show columns from test1';
                     $stt = $this->pdo->prepare($sql);
                     $stt->execute();
              
                     while ($column = $stt->fetch(PDO::FETCH_ASSOC) ){
                           $keys[] = $column['Field'];
                     }
              
              } catch(PDOException $Exception) {
                     die('エラー:' . $Exception->getMessage());
              } finally {
                     return $keys;
              }
              
       }
       
}
======== test.tpl ================

<!DOCTYPE html>
<head>
<title>{$values.title}</title>
</head>
<body>
<center>
       <h4>{$values.head}</h4>
       <table border=1 >
              {foreach $values.keys as $key}<th>{$key}</th>{/foreach}
              {foreach $values.list as $row}
                     <tr>{foreach $row as $val}<td>{$val}</td>{/foreach}</tr>
              {/foreach}
       </table>
</center>
</body>
</html>

DBも次のように準備

MariaDB [(none)]> select * from test.test1;
+------+-------+
| id   | name  |
+------+-------+
|    1 | name1 |
|    2 | name2 |
|    3 | name3 |
|    4 | name4 |
+------+-------+

で、対象をブラウザで見てみると、

これで、”get_list()”メソッドの”$sql”次第で、
クエリをいろいろいじっても対応できるはず。

コメントを残す

メールアドレスが公開されることはありません。