select() 是 ThinkPHP 中最常用的普通查询方法,得到的是一个二维数组。findAll() 为 select() 方法的别名,并建议使用 select()。
下面的例子将 user 表的所有数据读取出来并显示:
public function read(){
$Dao = M("User");
// 查询数据
$list = $Dao->select();
//dump($list); // 用 dump() 可以在调试阶段查看数据是否已读取
// 模板变量赋值
$this->assign("list", $list);
// 输出模板
$this->display();
}
假设上面的例子对应的 class 文件为 Lib/Action/IndexAction.class.php ,那么对应的模板文件为 Tpl/default/Index/read.html。
模板文件用于显示刚才读取的 User 表的数据。在学习阶段,要不想使用模板,也可以直接使用 foreach 语法在 read() 操作内直接显示读取的数据。下面是模板相应的代码片段,我们将读取的数据在一个表格中显示出来:
<table border="1">
<tr>
<th width="10%">ID</th>
<th width="30%">用户名</th>
<th width="30%">电子邮件</th>
<th>注册时间</th>
</tr>
<volist name="list" id="vo">
<tr>
<td align="center">{$vo['uid']}</td>
<td>{$vo['username']}</td>
<td>{$vo['email']}</td>
<td>{$vo['regdate']|date='Y-m-d H:i',###}</td>
</tr>
</volist>
</table>
要了解更多关于 ThinkPHP模板 的知识,请参阅:《ThinkPHP 模板》。