(゚д゚)ノ こんばんわ、中の人デス。


               /|:::::::::::::::::::::ヽ.:.:.:.:、:.:.:.:、:.:.:.、.:.、.:.:.:.:.:.::`゛>
           /{::|:\:::::::\.:.:.:\.:.:.ヽ::.::.ヽ:.:.ヽ::::::::::.:.`゛ー- ..,__
: 何 :    /:|::',:ト、::::::ヽ、:.\:.:.:.\:.:.ヽ:.:.:\.:.:.:.:.:::.:.:.:.:::.::::_;:-'´   : : :
: が :   //:/:::|::',|::'、:::::::::\:.:\.:.:.ヽ:.:.:\:.:..\::::::::::::\、::::\    : : :
: 何 :  /!::|::l::::/|:::l:ヽ:\::ヽ:.:\:.:\.:::ヽ:.:.:ヽ:.:.:.:\::::::::::::\ ̄   : : :
: だ :   |/l::|::|::|:ト、:::::::::、、:ヽ、:.:.:.:::::::::::::::ヽ::::.:ヽ:.:.:.:.\:.:.:.ヽ:::\.   : : :
: か :   |::|::/l::|::|r‐ヽ:::::ヽ(ヽー,―\::::::、::::::::::ヽ::.:.::::::.:::::::ヾ. ̄   : : :
:    :   }//l::|:::|{(:::)ヾ、:::ヽ \!(:::) ヽ,:::ヽ:::::::::::::::::::::::::::::::::::ヾ、   : : :
: わ :.  |/l::|::|:::|ヽ==''" \:ヽ、ヽ=='" |:::::::::::::::::::::::::::::::::::ヽ、::::\
  か     / ',|::|:::|   /   `゛       |!::::::::::::::::::::::::::::ト、::ト、_` ゛`
  ら      l::!::::ト、  '、 _         ||::::::::::::::::::::::::ト:ヽヾ| | ̄ ̄ ̄`ヽ、
  な     r'"´||',::::',                 |:::::/l:::::|\:::ト、ヾ | |     / / \
  い   /   ll ',::', 、 ーこニ=-       /!::/ ヽ:::|  ヾ、  ノ ノ  /  ,イ   ヽ、
       ,'    |  '、:, \ --       ,. '´ |;'  l ヾ、.   //     / |    l: l
      |   |!  ヽ;  ヽ       /.:    i!  /   ゛// |l      / |      | |


状態をなんとか脱した模様。多分。



            ,.
          /   _,,. -‐  ,. ィ
     ,. -─//. ,∠- ',. -‐':::/
    i::i  /::://:::: ̄::::::::::∠-‐──- 、
     {:::V::::::::::::::::::::-‐-──:::::::::::::::::::::::``ヽ.、
    ,.>::::::::::ヽ::::ヽ::::::::::::::` ヽ 、::::::::::::::::::::ヽ::ヽ、::\__
   /::::::::::::::::::\::ヽ:::::::::::::::::::::::::`ヽ、:::::::::::::ヽ:::::ヽ、_>
  /:::::::i、::ヽ::ヽ::::::::::\::::::::::r-,.- 、,.、:::::::::::::::::::::::::ヽ、__
  l::::/:::::i::::ヽヽ:::\:::::,.-‐'´``   ヽ ヽ、::::::::::::::::::::ヽ__
 .l::/::::::::l::::ト、::::\::ヽ:{ヽ、        l:::::::::::::::::::::::::ニ>
 |::l::i:::l:::|:::::|.〔)ヽ::::::::l | .i         ',:::::::::::::二ニヽ
 |:トlハ /ヽ:| レ′|ハ.l | .|         ',:::::::::::::::二ニヽ、ヽ
 | K. _  !     i! しi  ヽ、|      .l:::::::::::::::::::>──
 ! !. \`       |l    〉|      | ,r── 、 ̄`ヽ 、  <ヌルポ
    li〈 __     /`‐‐-//.l     l//        \ ヽ
    / `i'─`    /  ◎ 7 |     |          ヽ  ',
      ヽ     / ̄`─./ .|     | 〉       `ヽ、 ヽ
       {、__,.-/    / .|     |      ヘ_,,.... \ ',
          /     /`ヽl       |   / ̄´__  〉 .|
         `─---′ /        | / ,. -‐ '´   /  |
               /     ,...  、レ' /      /   .|
             _,,.⊥-‐' ´     冫      /     ,|
         __  ./|        ハ/        / /
      ,. '´  `'/ | |  /    /  |        //
     /      ヽ| |  /     /  ├‐┐    /    |
    /        |.l /        |ハ |   /     |


            ,   
           ,r/ ,;-ー_ 
         /:::レ:::::三:::::`ミ::=-、 
         ,i::::::::::::::::、:::::::、::::ヾ::ヽミー-、
         ,r'::::::::::::::::::ヽ::::::::\.:::::::::::::\
      //:::::/:::;:::i:::::ヽ:::::::::::::\::::::::::::::ヽ
.       /::/:::::::/_ヽ:!:::::ヽ:::i`_、:::::ヽ ::ヽ:::::::!      ガッ!!

       i::/::::::::/'oヾ、:::|ヽ::Y o\:::::i!::::ヽ::::\    
     l:::i:::::::::lゝ- ' ヾ、゙ ! ー '、ヽ:::::!:::::ヽ::i ヽ
     !::!:::i:::::!   ` ` ヽ     i:::::!::::::::::::i
       l:!:::l:::l !  ,r_ー‐-、     l::::!::::::::::;::i
      !|:.!::',   i'/ ̄ ̄ i     l:::::::::::::ハ:!
    .   l::::::ヽ.  i    i    ィ::/i:::::ハi  !
        i::li:::\ `ー--‐'   / リ |:::リfハ、_
         _..!:ハ::::l;ヽ、    /  " l::/i ヽ. ``''ー- 、._
   ,r‐'''"´  ! リ!i ヽ `='"   ,   リ /  ン       /ヽ、
   /ヽ       ヽ ヽ ヽ   /   /        /   ヽ
  l   \      \  '     //          /    i



(´・ω・`)相変わらずXAMPP環境対応なのですが。


SQLiteでのDB作成条件が特殊すぐるので手順を追って解説します。



PHP2009-sqli4
まずは『スタート』→『アクセサリ』→『コマンドプロンプト』を。



PHP2009-sqli5
sqli3.exeのあるフォルダへ移動。「C:\xampp\apache\bin」。



PHP2009-sqli6
sqlite3.exeがあるフォルダに移動したら『sqlite3 作成するデータベース名』でエンター。


PHP2009-sqli7
すると画面名が作成するsqlite3 DB名になり、コマンドラインにsqliteのバーションが表示される。


*注*これでDBは作成されたことになるが、SQLiteの固有性として、DB作成状態でTABLEを最低1つでも作成しなければならない。しないとDB吹っ飛ばされます。(゚∀。)アヒャヒャ


さーということで作成したtest.dbにテーブルを作成しませう。



PHP2009-sqli8
テーブル名は「testtable」。カラムは「id」と「name」で。


そしてテーブルを作成すると…。



PHP2009-sqli9
sqlite3.exeのフォルダに作成したDB名のデータベースファイルが作成される。


試しにいろいろやってみませう。



PHP2009-sqli10
適当に5個ほどレコードを登録してみます。


   イ  ヽ       j   .す
   ン   ゙,      l.    ご
   サ    !      ',     く
   |    ',        ',    :
   ト    ト-、,,_    l
  で    !   `ヽ、 ヽ、    _
  す    /      ヽ、`゙γ'´
   ;   /         \
      !   ト,       ヽ
ヽ__  ___ノ ,!   | | ト,       ゙、
  レ'゙ ,イ ./|!  .リ | リ ! .|! | ト|ト}
 ,イ ,/ ./〃/ / | / .リ/ //イ|.リ
// //ノノ  //゙ ノ'////|.リ/
´彡'゙,∠-‐一彡〃 ト.、,,,,,,,,,,,レ゙
二ニ-‐'''"´      /`二、゙゙7
,,ァ''7;伝 `        {.7ぎ゙`7゙
  ゞ‐゙''  ,. ,. ,.  l`'''゙" ,'
  〃〃"      !   |
              !  l
 !       (....、 ,ノ  !
 j        `'゙´  ,'
     ー--===ァ   /  …ゴクリ
      _ _   ./
\     ` ̄   ,/
  ` .、       /
   :ミ:ー.、._  /``'''ー-、
    `゙三厂´



さて、これでDB作成、テーブル作成、レコード登録と一通り終わったので、ちゃんとデータが保持されるのか確かめてみましょう。



PHP2009-sqli11
SQLiteを抜けるには『.exit』コマンドです。


ちなみに×ボタンでもOKですが再度cdするのが面倒なので再度sqlite3 test.dbで。



PHP2009-sqli12
(゚д゚)それであSELECT文でデータを確認してみませう。



PHP2009-sqli13
(゚д゚)クワッ



PHP2009-sqli14
登録データの一覧が表示されます。(´Д`;)id3がイパーイあるのは登録ミスです。orz


(´Д`;)とりあえずこれでSQLiteの解説は一応終了。本筋に戻ります。



                   , '´  ̄ ̄ ` 、
               \  i r-ー-┬-‐、i /
                  | |,,_   _,{|       / ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄
                ̄  N| "゚'` {"゚`lリ  ̄  < やらないかやらないか!
               /   ト.i   ,__''_  ! \    \_________
                    \ ー .イ   
                       ̄

                , '´  ̄ ̄ ` 、   , '´  ̄ ̄ ` 、
 ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄\   i r-ー-┬-‐、i   i r-ー-┬-‐、i  
やらないか~~~! > | |,,_   _,{|   | |,,_   _,{   / ̄ ̄ ̄ ̄ ̄
________/   N| "゚'` {"゚`lリ  N| "゚'` {"゚`lリ  < やらないか!!
                ト.i   ,__''_  !   ト.i   ,__''_  !    \_____
                ∩\ ー .イ    ∩ \ ー .イ∩
                |    〈     \     /
                / /\_」     |    / 
                 ̄          |   |
                           / /\」
                          / /

提供:やらないか産業






(゚д゚)さてインターバルを終えて続きに入りましょう。


      ::::::::::::::::::::::::::::::::::::::::::::::::::::l<
      :::::::::,,ヅ彡ニミ;;,,::::::::::::::::::::F
    /||::::ィ<"  , ‐ 、 ゙ミ、:::::::::::::::l
   / ::::||Fミミ、, ゝ__゚,ノ,, イ〃ノ::::::::::|=-<
 /:::::::::::||::::::::゙゙'ヾ三≡彡'":::::::::::::::::,'、
 |:::::::::::::::||::::::::::::::::::::::::::::::::::::::::::::::::::/  ト
 |:::::::::::::::||::::::::::::::::::::::::::::::::::::::::::::::,ィ'
 |:::::::::::::::||::::::::::::::::::::::::::::::::::::::::,/\_
 |:::::::::::::::||::::::::::::::::::::::::::::::::::,イ    |
 |:::::::::::::::||::::::::::::::::::::_,, ┐'  ト-
 |:::::::::::::::||| ̄乃'゙人  ∧
 |:::::::::::::::||∧ ∧∩ ..||
 |:::::::::::::::|| ゚∀゚)/  .||ガチャッ
 |:::::::::::::::||∧ ∧∩ ..||
 |:::::::::::::::|| ゚∀゚)/ . ||
 |:::::::::::::::||    〈 . ..||
 |:::::::::::::::||,,/\」 . ..|                            / ̄ ̄ ̄ ̄ ̄ ̄
 \:::::::::::|| ̄ ̄ ̄ ̄                        ∧_∧ │
   \ ::::||                       ___\(・∀・; )< ・・・。
    \||                       \_/⊂ ⊂_ )│
                              / ̄ ̄ ̄ ̄ ̄ ̄ /| \______




(゚ω゚)まずSmartyで使うDBを作成しませう。


DM名はsmarty.sqliteで、次の構造のテーブルを作成します。


テーブル:personals


id integer not null primary key

name varchar(100)

mail varchar(255)

tel varchar(255)


(゚д゚)ついでにレコード例

INSERT INTO personals (name,mail,tel) VALUES('名前','メールアドレス','電話番号');

で何件か登録しておきましょう。大体3~5件程度でいい希ガス。


                      ,. ―‐- 、
                   /´`       ヽ、
                      ,'        、} ト.
.    rr、                ,'    '   ,.イ゙V }
  ,イ川、             ,' .!  ,' .i  〈_,イ「l/        フ ヽヽ         /
  |  !」_          / ,'::! .!,': ::! .:.:| ハ〉|     / J ヽ     ̄ ̄ ̄   Λ_丿
  l  //```ヽ、       l:i::!::! :::l:::l :::l ::::,`:!:i::|
  `ーl_{     ``丶、__ _从{::|:::::l::,':::::| ::::,':::'l::l::!                 ,.┐
     \_          `/ Λ!::/:ノ!::/!::ノ::/ノノノ                 /フ′
       `¨` ー- 、_   / /  ´ ´ ''´ '´7´/´ ̄`¨¨`¬……――''"´`>ーァ'´,イ__
             \! ∟..___    / /               / /  '´,.-┘
                 ∟...___   ̄´ /     ___ ___      {___j--‐'´
              |      ̄ ̄´ ´厂 ̄´        ̄`¨¨´
                /         ノ'´
                }___、      |
            /     ``¬―v‐'
            /        ,.   |
          //        /     !
         //      ,'      '、
        / /l         !        ヽ
        \,'|         !          >



でぁDBも用意できたことですし、早速phpへ移行しませう。


DBアクセス用クラスを定義します。

『smartyapp』フォルダ内に「db.php」というファイルを作成しませう。


db.php

<?php

class Db{
var $connect;

function __construct($file){
$this->connect = aqlite_open($file);
}

function getAllRecords($table){
$result = sqlite_query($this->connect,"select * from {$table};")
or exit('ERROR');
$arr = array();
$count = 0;
while($row = sqlite_fetch_array($result)){
$arr[$count++] = $row;
}
$return $arr;
}
}
?>


つぎにコントローラ部分。

index.php

<?php
require_once('Smarty.class.php');
$smarty = new Smarty();

if($_POST != null){
$smarty->assign('name',$_POST['name']);
$smarty->assign('mail',$_POST['mail']);
$smarty->assign('tel',$_POST['tel']);

}else{
$smarty->assign('name','');
$smarty->assign('mail','');
$smarty->assign('tel','');
}

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

?>


↑を↓のように修正。

<?php
require_once('Smarty.class.php');
$smarty = new Smarty();
require_once('db.php');

$db = new Db('C:\xampp\apache\bin\smarty.sqlite');
$smarty->assign('result',$db->getAllRecords('personals'));


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

?>



続いてビュー部分の修正。テンプレートファイルを修正しましょう。

index.tpl

{config_load file='index.conf' section='index'}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"
http://www.w3c.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns="
http://www.w3.org/1999/xhtml ">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>{#title#}</title>
<style>
{literal}
table{{/literal}{#table_style#}{literal}}
th{{/literal}{#th_style#}{literal}}
.msg{{/literal}{#msg_style#}{literal}}
{/literal}
</style>
</head>

<body>
<h1>{#headline#}</h1>
<pre><p class="msg">{$message|escape}</p></pre>
<table>
<form method="post" action="index.php">
<tr><th>名前</th>
<td>
{if $name==''}
<input type="text" name="name" size="40">
{else}
{$name}<input type="hidden" name="name" value="{$name}">
{/if}
</td></tr>


<tr><th>アドレス</th>
<td>
{if $mail == ''}
<input type="text" name="mail" size="40">
{else}
{$mail}<input type="hidden" name="mail" value="{$mail}">
{/if}
</td></tr>
<tr><th>電話番号</th>
<td>
{if $tel == ''}
<input type="text" name="tel" size="40">
{else}
{$tel}<input type="hidden" name="tel" value="{$tel}">
{/if}
</td></tr>
<tr><td></td>
<td><input type="submit" value="送信"></td></tr>
</form>
</table>

</body>
</html>


↑を↓のように修正。


{config_load file='index.conf' section='index'}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"
http://www.w3c.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns="
http://www.w3.org/1999/xhtml ">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>{#title#}</title>
<style>
{literal}
table{{/literal}{#table_style#}{literal}}
th{{/literal}{#th_style#}{literal}}
.msg{{/literal}{#msg_style#}{literal}}
{/literal}
</style>
</head>

<body>
<h1>{#headline#}</h1>
<table border="1">
{foreach from=$result item=row}
<tr>
{section name=n start=0 loop=6 step=1}
<td>{$row[n]}</td>
{/section}
</tr>
{/foreach}
</table>

</body>
</html>


(゚ω゚)これでできるはず…。



PHP2009-sqli15
なん…だと?!


(´・ω・`)エラーはかれた。ぐぐったところ、どうやらsqlite3でのデータ取得に対応していない模様。

PDOならできるぜ!というHPを頼りに書き直す。


db.php

<?php

class Db{
var $connect;

function __construct($file){
$dsn = 'sqlite:{$file}';
$pdo = new PDO($dsn);

// $this->connect = sqlite_open($file);
}

function getAllRecords($table){

$dsn = 'sqlite:{$table}';
$pdo = new PDO($dsn);
$sql = "select * from {$table}";
$entries = $pod->query($sql);
while($row = sqlite_fetch_array($result)){
$arr[$count++] = $row;
}

/*
$result = sqlite_query($this->connect,"select * from {$table};")
or exit('ERROR');
$arr = array();
$count = 0;
while($row = sqlite_fetch_array($result)){
$arr[$count++] = $row;
}
*/

return $arr;
}
}
?>

こんな風に。PDO使えばいけるんじゃね?的に実行。


PHP2009-sqli16
なん…だと…?


またもエラー。

オブジェクトじゃねーンだけど!!っておこられとりまんがな(´・ω・`)ショボンヌ


どんだけ使えないんだSQLite。データ扱えなきゃ意味ないぞSQLite。


(´・ω・`)本格的にSQLite勉強しなきゃ太刀打ちできない模様&タイムアップということで尻切れトンボ。

と見せかけて悪あがき。ポケモンかよ。


実務でSQLiteとかきたら拒否反応起こす様になりそうだ。



PHP2009-sqli17
sqlite3.exeと同じフォルダにあるsqlite.exeを試してみる。


バージョン変えて再度挑戦ということで修正db.php↓

<?php

class Db{
var $connect;

function __construct($file){
/*
$dsn = 'sqlite:{$file}';
$pdo = new PDO($dsn);
*/

$this->connect = sqlite_open($file);
}

function getAllRecords($table){
/*
$dsn = 'sqlite:{$table}';
$pdo = new PDO($dsn);
$sql = "select * from {$table}";
$entries = $pod->query($sql);
while($row = sqlite_fetch_array($result)){
$arr[$count++] = $row;
}

*/
$result = sqlite_query($this->connect,"select * from {$table};")
or exit('ERROR');
$arr = array();
$count = 0;
while($row = sqlite_fetch_array($result)){
$arr[$count++] = $row;
}


return $arr;
}
}
?>


次、index.phpも↓のように修正。

<?php
require_once('Smarty.class.php');
$smarty = new Smarty();
require_once('db.php');

$db = new Db('C:\xampp\apache\bin\smarty.db');
$smarty->assign('result',$db->getAllRecords('personals'));


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

?>


テンプレのindex.tplはそのまんま東。


(´・ω・`)だめもとでhttp://localhost/smartyapp/ へアクセス。



PHP2009-sqli18
なん…だと…!?


(´・ω・`)はじめから3じゃなくて素のSQlite使えよ。って話でしたがな。


ver違いですごいタイムロス。

。・゚・(ノД`)・゚・。ウエエェェン


原因も解決もわからないまま終了という最悪の事態は避けられたものの…やっぱはじめてのモノは苦戦しまくりです。そして時間かかりすぎ。(´・ω・`)ショボンヌ


DB作成、テーブル作成、レコード登録の部分は『sqlite3』を『sqlite』に置き換えてやってください。(sqlite3 DB名とかあったらsqlite DB名 で読み替えてくだしゃい(´・ω・`)ショボンヌ)


db.php

<?php

class Db{
var $connect;

function __construct($file){
/*
$dsn = 'sqlite:{$file}';
$pdo = new PDO($dsn);
*/

$this->connect = sqlite_open($file);
}

function getAllRecords($table){
/*
$dsn = 'sqlite:{$table}';
$pdo = new PDO($dsn);
$sql = "select * from {$table}";
$entries = $pod->query($sql);
while($row = sqlite_fetch_array($result)){
$arr[$count++] = $row;
}

*/
$result = sqlite_query($this->connect,"select * from {$table};")
or exit('ERROR');
$arr = array();
$count = 0;
while($row = sqlite_fetch_array($result)){
$arr[$count++] = $row;
}


return $arr;
}
}
?>

db.phpの解説。

コンストラクタでファイルパス(smarty.dbへのパス)を渡し、接続を開始します。

getAllRecodeメソッドでは引数に渡されたテーブルのレコード一覧を取り出し、それらを配列にまとめて返します。


コントロラ部分。

<?php
require_once('Smarty.class.php');
$smarty = new Smarty();
require_once('db.php');

$db = new Db('C:\xampp\apache\bin\smarty.db');
$smarty->assign('result',$db->getAllRecords('personals'));


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

?>


newDBでインスタンスを生成した後getAllRecodesでpersonalsテーブルの全レコード

を配列でビューに渡します。


{config_load file='index.conf' section='index'}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"
http://www.w3c.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns="
http://www.w3.org/1999/xhtml ">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>{#title#}</title>
<style>
{literal}
table{{/literal}{#table_style#}{literal}}
th{{/literal}{#th_style#}{literal}}
.msg{{/literal}{#msg_style#}{literal}}
{/literal}
</style>
</head>

<body>
<h1>{#headline#}</h1>
<table border="1">
{foreach from=$result item=row}
<tr>
{section name=n start=0 loop=6 step=1}
<td>{$row[n]}</td>
{/section}
</tr>
{/foreach}
</table>

</body>
</html>

ビューでは{foreach}&{section}を使って飛んできたテーブル内データ配列を表示しています。

外堀のforeachでは配列からレコードの値を順に取り出し、内側のsectionで各フィールドの値を取り出しています。

(´・ω・`)カラム数は(id、名前、メルアド、電話番号)の4つなのでsectionのloopは4でおkです。


(´・ω・`)6は間違いorz

sectionのloopを減らすと表示される項目も減ります。



PHP2009-sqli19
試しにloop=3にした結果。



テンプレートは、一つのファイルを読み込んで表示するというシンプルな使い方しかできない訳でもないです。

テンプレートの中に別のテンプレートを埋め込むというやり方も存在します。


そうした場合に使いのが、{include}という関数です。


では実際に『ヘッダー』『フッター』『コンテンツ』といった3っつのテンプレを組み合わせて表示させてみませう。


まずはベースを作成。

layout.tpl

{config_load file='layout.conf' section='$section'}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"
http://www.w3c.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns="
http://www.w3.org/1999/xhtml ">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>{#title#}</title>
<style>
{literal}
bodyheader{{/literal}{#body_style#}{literal}}
.header{{/literal}{#header_style#}{literal}}
.footer{{/literal}{#footer_style#}{literal}}
h1{{/literal}{#h1_style#}{literal}}
.content_style{{/literal}{#content_style#}{literal}}
{/literal}
</style>
</head>
<body>
<table width="100%">
<tr><td>{include file='header.tpl'}</td></tr>
<tr><td class="content_style">{include file=$content}</td></tr>
<tr><td>{include file='footer.tpl'}</td></tr>
</table>
</body>
</html>


(´ω`)でぁ次にヘッダー、フッターテンプレを適当につくりませう。

header.tpl

<div class="header">{$header_title}</div>


footer.tpl

<div class="footer">{$footer_title}</div>


次にレイアウト用のスタイルシートを用意。もち保存場所はC:\xampp\htdocs\smartyapp\configs。

layout.conf

body_style= background-color: #FFEEEE;
header_style = color: #FF0066; font-size: 10pt; font-weight: bold; border-style: none none solid none; margin: 0pt 0pt 10pt 0pt;
footer_style= color: #FF0066; font-size: 10pt; font-weight: bold; border-style: solid none none none; margin: 10pt 0pt 0pt 0pt;
h1_style= background-color: #FF6699; font-size: 12pt; padding: 3px; margin: 0pt 0pt 20pt 0pt;
content_style= color: #660000; padding: 3px;

[index]
title = "Welcome to Smarty"

[other]
title = "other page"


最後に、表示するコンテンツのページを用意しますですよ。

ここではダミーとして『index.tpl』に以下のように記述しておきますよ。

<h1>INDEXタイトル</h1>
<p>これはレイアウトを使って表示したコンテンツです。</p>


最後にプログラム部分の作成をします。

layout.php

<?php
require_once('Smarty.class.php');
$smarty = new Smarty();
$smarty->assign('section','index');
$smarty->assign('header_title','Smarty Layout Design');
$smarty->assign('footer_title','copyright 2009 naka');
$smarty->assign('content','index.tpl');
$smarty->display('layout.tpl');


?>


できたらhttp://localhost/smartyapp/layout.php へレッツアクセスヽ( ^ω^)ノ サクセス!




以上がうまくできていれば、次のような表示になります。


PHP2009-sm12
(゚д゚)こんなん。


設定ファイルで使用するセクション、ヘッダーとフッターの表示テキスト、表示するコンテンツファイル名といったものをassignしてdisplayでlayout.tplを表示しているのですよ。


複数のコンテンツファイルを用意し、それぞれassign('content'~)の値を書き換えて表示をいじってみませう。

統一したデザインですべてのページが表示されるのがわかるはずですぉ。

(´・ω・`)とりあえずすまーてーは以上。だいぶgdgdになりましたがね。(´・ω・`)ショボンヌ


(゚д゚)次回からCakePHPやります。


人気ブログランキングへ