ThinkPHP5給前端頁面簡單賦值
阿新 • • 發佈:2019-01-05
資料庫預設埠:3306
獲取上一條執行的SQl語句:Db::getLastSql();
PHP給HTML賦值:
建立一個控制器,然後查詢資料庫獲取所有資料:
use think\Controller;
use think\Db;
class Demo extends Controller
{
$data=Db::query('select * from student');
$this->assign('data',$data);
$num='aaaaa';
$this->assign('num',$num);
return view();
}
下面是html程式碼
我們先列印一下$data的值
<?php
var_dump($data);
?>
結果
array(6) { [0]=> array(8) { ["uid"]=> string(4) "1020" ["name"]=> string(12) "小西康路" ["studentid"]=> int(123456) ["sex"]=> string(3) "女" ["class"]=> string(8) "3年2班" ["polity"]=> string(6 ) "團員" ["birthday"]=> string(10) "2017-10-28" ["phone"]=> int(2147483647) } [1]=> array(8) { ["uid"]=> string(4) "1000" ["name"]=> string(9) "管理員" ["studentid"]=> int(123456) ["sex"]=> string(3) "男" ["class"]=> string(9) "教務處" ["polity"]=> string(6) "黨員" ["birthday"]=> string(9 ) "2017-10-8" ["phone"]=> int(2147483647) } [2]=> array(8) { ["uid"]=> string(4) "1034" ["name"]=> string(6) "林二" ["studentid"]=> int(115555) ["sex"]=> string(3) "男" ["class"]=> string(4) "2班" ["polity"]=> string(6) "團員" ["birthday"]=> string(8) "1996-2-9" ["phone"]=> int(1111111111) } [3]=> array(8) { ["uid"]=> string(4) "1037" ["name"]=> string(6) "林三" ["studentid"]=> int(88) ["sex"]=> string(3) "男" ["class"]=> string(4) "1班" ["polity"]=> string(6) "團員" ["birthday"]=> string(8) "1995-4-9" ["phone"]=> int(1523625010) } [4]=> array(8) { ["uid"]=> string(4) "1048" ["name"]=> string(3) "林" ["studentid"]=> int(25) ["sex"]=> string(3) "男" ["class"]=> string(1) "1" ["polity"]=> string(6) "團員" ["birthday"]=> string(9) "1999-4-18" ["phone"]=> int(1565952012) } [5]=> array(8) { ["uid"]=> string(4) "1049" ["name"]=> string(6) "羅一" ["studentid"]=> int(19) ["sex"]=> string(3) "男" ["class"]=> string(1) "1" ["polity"]=> string(6) "團員" ["birthday"]=> string(9) "1998-4-18" ["phone"]=> int(1565950213) } }
我們給頁面賦值
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>頁面賦值</title>
</head>
<body>
<table style="border: 1px solid red">
<tr >
<td>姓名</td>
<td>學號</td>
<td>性別</td>
<td>班級</td>
<td>身份</td>
<td>生日</td>
<td>聯絡方式</td>
</tr>
<!--volist迴圈遍歷 name 控制器傳值的key,標誌, value獲取的值-->
{volist name='data' id='value'}
<tr>
<!--{{$value.name}} 值裡面包含的欄位-->
<td>{$value.name}</td>
<td>{$value.studentid}</td>
<td>{$value.sex}</td>
<td>{$value.class}</td>
<td>{$value.polity}</td>
<td>{$value.birthday}</td>
<td>{$value.birthday}</td>
</tr>
{/volist}
</table>
<h2>{$num}</h2> //單個數據賦值
</body>
</html>