ThinkPHP学习(三)

我们已经将数据保存到了后台数据库,那接下来我们肯定要将数据显示出来看看了。

先建立一个要显示数据的模板formlist.html:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title>ThinkPHP</title>
 </head>
 <style type="text/css">
table {
    font-family: verdana,arial,sans-serif;
    font-size:11px;
    color:#333333;
    border-width: 1px;
    border-color: #666666;
    border-collapse: collapse;
    text-align: center;
}
table th {
    border-width: 1px;
    padding: 8px;
    border-style: solid;
    border-color: #666666;
    background-color: #dedede;
}
table td {
    border-width: 1px;
    padding: 8px;
    border-style: solid;
    border-color: #666666;
    background-color: #ffffff;
}


 </style>
 <body>
 <div class="main">
 <h2>你好</h2>

<table align=center>
<tr>
    <th>主题</th><th>内容</th>
</tr>
<volist name="list" id="vo" empty="暂时没有数据">
<tr>
    <td>{$vo.title}</td><td>{$vo.content}</td>
</tr>
</volist>

</table>



</div>
 </body>
</html>

然后在后台保存成功后接着调用显示:

<?php

class IndexAction extends Action{
    
    public function index(){
        $hello='Hello,ThinkPHP !';
        $this->assign('hello',$hello);
        $this->display();
    }

    public function add(){
        $form = M('Form');
        $form->create();
        $form->add();

        $this->redirect('Index/formlist');
    }

    public function formlist(){
        $form = M('Form');
        $vo=$form->select();
        $this->assign('list',$vo);
        $this->display();
    }

}

对,就是简单地修改一下add()方法,保存成功后重定向显示模板,然后在formlist()中显示出所有数据来。

注意方法名字和模板名字要匹配噢。

再试试保存,保存后会直接打开显示所有保存结果的页面。当然了,实际中不能这么简单地显示所有数据。

原文地址:https://www.cnblogs.com/GarfieldTom/p/4230234.html