输出Dropdowlist到TPL文件
问题描述:
我有一些正常工作的PHP代码。不过,我想输出Dropdownlist到我的TPL文件。我怎样才能做到这一点?输出Dropdowlist到TPL文件
<?php
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name ORDER BY SORT_ORDER ASC";
$result=mysql_query($sql);
if ($result === false) { echo "An error occurred."; }
?>
<html>
<head>
<title></title>
</head>
<body>
<select name="usrname" id="usrname">
<option>Select employ</option>
<?php
while($rows=mysql_fetch_array($result)){
?>
<option value="<? echo $rows['ID']; ?>"><? echo $rows['TITLE']; ?></option>
<?php
}
mysql_close();
?>
</select>
</body>
</html>
答
<option value="<? echo $rows['ID']; ?>"><? echo $rows['TITLE']; ?></option>
而忘php text in <?php .... ?>
..please更改此代码这样的..
<option value="<?php echo $rows['ID']; ?>"><?php echo $rows['TITLE']; ?></option>
答
你这是什么意思.tpl?
你可以转移你“而(...){...}”部分到另一个文件(说select.inc.php)和 包括它
,但如果你想有一个$ TPL对象在template.class.php先创建一个类,如:
class Template {
private $file;
public $result;
public function __construct($file) {
$this->file = $file;
}
public function display() {
ob_start();
include $this->file;
ob_end_flush();
}
}
包括它并创建模板文件select.tpl:
<select name="usrname" id="usrname">
<?
while($rows=mysql_fetch_array($this->result)) {
?>
<option value="<?=$rows['id']?>"><?=$rows['name']?></option>
<?
}
?>
</select>
最后创建的实例并调用display()方法在你的脚本:
$tpl = new Template('select.tpl');
$tpl->result = $result;
$tpl->display();
这不是我的意思。我想我需要分配它: $ tpl - >分配 我想从.tpl拉起它而不是PHP。 – user2063873 2013-03-09 07:19:27