具体要求:
(1) 在 Student 控制器中编写 show()方法,输出学生列表视图。同时传递数据,使用数组保存学生列表数据,要求包含至少 3 名学生的“姓名”“性 别”和“年龄”信息。
(2)在视图中,使用循环输出学生列表数据。
请按要求完成,并写出详细步骤。
看你描述,我默认你使用tp,所以就基本这个简单写了一下
一. 创建控制器类,StudentController
//控制器
<?php
namespace Home\Controller;
use Think\Controller;
class StudentController extends Controller
{
public function show()
{
$resultData = array(); //结果集
//数据库查询
$resultData = D("student")->field("id,std_name,std_sex,std_age")->where(array("status" => "1"))->limit(3)->select();
//输出变量到模板
$this->assign("stdData", $resultData);
//模板路径 View/Student/show.html
$this->display();
}
}
?>
二. 创建简单显示数据模板
//模板视图
<!doctype html>
<html lang="zh-cmn-Hans">
<head>
<meta charset="utf-8">
<title>Student视图模板</title>
<meta name="keywords" content="">
<meta name="description" content="">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />
</head>
<body>
<table>
<tbody>
<tr>
<th>编号</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
</tr>
<foreach name="stdData" item="item">
<tr>
<td>{$item.id}</td>
<td>{$item.std_name}</td>
<td>{$item.std_sex}</td>
<td>{$item.std_age}</td>
</tr>
</foreach>
</tbody>
</table>
</body>
</html>