>[info]修改 models/Article.php 为:
~~~
<?php
/**
* Article model
*/
class Article
{
public static function first() {
$connect = mysqli_connect("localhost", 'root', 'root', 'uwebcms6') or die('连接失败'.mysqli_connect_error());
mysqli_query($connect,"set names utf8");
$query = mysqli_query($connect, "select * from uwebcms_document limit 1,2");
if(mysqli_num_rows($query) > 0) {
while($row = mysqli_fetch_assoc($query)){
$result[] = $row;
}
return $result;
}
mysqli_close($connect);
}
}
~~~
>[info]将包含查询结果的数组返回。修改 HomeController:
~~~
<?php
/**
* HomeController
*/
class HomeController extends BaseController
{
public function home(){
$article = Article::first();
require dirname(__FILE__).'/../views/home.php';
}
}
~~~
>[info]在views文件夹下新建home.php文件
~~~
<!DOCTYPE html>
<html>
<head>
<title>view视图调用测试</title>
</head>
<style type="text/css">
table, tr, th, td {
border: 1px solid #000;
border-collapse:collapse;
}
</style>
<body>
<table>
<tr>
<th>标题</th>
<th>作者</th>
<th>标签</th>
<th>描述</th>
</tr>
<?php foreach($article as $key=>$value) { ?>
<tr>
<td><? echo $value['title']; ?></td>
<td><? echo $value['author']; ?></td>
<td><? echo $value['tags']; ?></td>
<td><? echo $value['description']; ?></td>
</tr>
<?php } ?>
</table>
</body>
</html>
~~~
刷新页面后同样出现了数据:
![](https://box.kancloud.cn/be7dbeec46a76335d739c14c25ee2bc2_720x173.png)
此时视图调用成功实现!