<?php namespace HomeController; use ThinkController; class IndexController extends Controller { //控制器 public function index(){ $s = "hello";
$arr = array("code"=>"n001","name"=>"汉族"); //数组
$this->assign("name",$s); //注册变量
$this->assign("arr",$arr); //注册数组变量 $this->show(); //显示页面 } }
index.html
<div>{$name}</div> <div>{$arr.code}</div> //或者<div>{$arr["code"]}</div>
浏览器输入http://localhost/tp/index.php/Home/Index/index。显示页面:
从数据库中调取数据:
1 <?php 2 namespace HomeController; 3 use ThinkController; 4 class IndexController extends Controller { 5 public function index(){ 6 $db = D("Nation"); 7 $arr = $db->select(); 9 $this->assign("arr",$arr); 10 $this->show(); 11 } 12 }
视图:
<body> <table width="100%" border="1" cellpadding="0" cellspacing="0"> <tr> <td>代号</td> <td>名称</td> </tr> <foreach name="arr" item="v"> <if condition="$v['code']=='n001'"> <tr style="color: red"> <td>{$v.code}</td> <td>{$v.name}</td> </tr> <else /> <tr> <td>{$v.code}</td> <td>{$v.name}</td> </tr> </if> </foreach> </table> </body>