PHP Smarty HTMLのラジオボタンを使用する

-


Topページ  >  お勉強  >  PHP  >  HTMLのラジオボタンを使用する 

HTMLのラジオボタンを使用する

Smartyで、HTMLのラジオボタンを表示する方法です。

※Smarty本体、必要なディレクトリは、「C:\tmp\」以下に配置・作成してある前提です。
 詳細は「Smartyのインストール方法」を参照してください。





1. PHPファイルの作成。

PHPが動作する任意の場所にPHPファイルを作成し、以下の内容を記述してください。

define( 'SMARTY_DIR', '/tmp/Smarty/libs/' );

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

// 各ディレクトリの設定
$smarty->template_dir = '/tmp/Smarty/templates/';
$smarty->compile_dir  = '/tmp/Smarty/templates_c/';
$smarty->config_dir   = '/tmp/Smarty/configs/';
$smarty->cache_dir    = '/tmp/Smarty/cache/';


// ラジオボタンに表示する要素を配列で宣言する
$colors['red']    = '赤';
$colors['blue']   = '青';
$colors['yellow'] = '黄';

// 宣言した配列を、変数「$colorArray」にセットします
// この変数がコンボボックスの要素として指定されます
$smarty->assign( 'colorArray', $colors );

// コンボボックスで「青」が選択されている状態にするために、
// 変数「$selectColor」に 'blue' をセットします
$smarty->assign( 'selectColor', 'blue' );

// テンプレートファイルを表示
$smarty->display( 'test.tpl' );


2. テンプレートファイル(HTML)の作成。

C:\tmp\Smarty\templates\test.tpl」 を作成して、以下の内容を記述してください。

<html>

{html_radios name="color" options=$colorArray selected=$selectColor separator='<br />'}

</html>


3. PHPの実行。

上で作成したPHPを実行してください。
以下のように表示されます。








Topページ  >  お勉強  >  PHP  >  HTMLのラジオボタンを使用する 






-