PHP+MySQL+PEAR+Smartyでリストボックスにデータベースから取得したデータを設定したい場合はSmartyのhtml_optionsを使う。
それに至るまでの困難の道のりをおいときます。
1.文字化け
DB接続後に以下クエリを実行して回避すべし。
$sql = "SET NAMES sjis";
$result = $this->db->query($sql);
文字コードはお好きなのを。
参考サイト様
一週間で覚えるPHP: MySQL文字化け解消。
一週間で覚えるPHP: PEAR::DBかぁ。
ブログタイトルに共感を覚えましたw
MySQL4.1系UTF-8の場合 - phpspot
DB取得メソッド getAll, getAssoc, getRow, getOne - PHP::PEAR - dozo PukiWiki
2.Smartyのhtml_optionsには配列を返す。
超省略バージョン。これだけでは動かない。
-- phpファイル --
// データ取得
$dbはPearのDB
$optionList = $db->getAssoc("select id, value from hoge where huge=?", false, array(1));
$smarty->assign('optionList', $optionList);
-- テンプレートファイル --
<select id="huge" name="huge">{html_options options=$optionList}</select>
※「空」項目や「全て」項目を加えたい場合は以下のようにする。
<select id="huge" name="huge"><option label="全て" value="All">全て</option>{html_options options=$optionList}</select>
参考サイト様
DB_common::getAssoc()
html_options
html_options - Smarty - livedoor Wiki(ウィキ)
ONLamp.com -- The Dynamic Duo of PEAR::DB and Smarty
Smarty :: View topic - html_options from SELECT query
Smarty :: View topic - clickable html_options value
投稿日時 : 2006年10月9日 4:08