http://localhost:8080/thinkphp/index.php/User/index
<?php
class UserAction extends Action {
public function index(){
echo "你好!";
}
}
?>
$m=M('user'); //返回一个对象:
<?php
class UserAction extends Action {
public function index(){
echo "你好!";
$m=M('user');
$arr=$m->select();
var_dump($arr);
$this->display();
}
}
?>
你好!
array (size=2)
0 =>
array (size=3)
'id' => string '1' (length=1)
'username' => string 'ztz2' (length=4)
'sex' => string '1' (length=1)
1 =>
array (size=3)
'id' => string '2' (length=1)
'username' => string 'MM' (length=2)
'sex' => string '0' (length=1)
xxxx
<?php
class UserAction extends Action {
public function index(){
#echo "你好!";
$m=M('user');
$arr=$m->select();
#var_dump($arr);
$this->assign('data',$arr);
$this->display();
}
}
?>
整个数组分配给标识data
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus?">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
</head>
<body>
<table border='1' width='500' align='center'>
<tr>
<th>id</th>
<th>username</th>
<th>sex</th>
<th>操作</th>
</tr>
<volist name='data' id='vo'>
<tr>
<td><{$vo.id}></td>
<td><{$vo.username}></td>
<td><{$vo.sex}></td>
<td>删除|修改</td>
</tr>
</volist>
</table>
</body>
</html>
</body>
</html>
模板遍历数组:
删除方法:
public function del(){
$m=M('user');
$id=$_GET['id'];
$count=$m->delete($id);
echo $count;
if ($count >0){
$this->success('数据删除成功');
}else{
$this->error('数据删除失败');
}
$this->display();
}
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus?">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
</head>
<body>
<table border='1' width='500' align='center'>
<tr>
<th>id</th>
<th>username</th>
<th>sex</th>
<th>操作</th>
</tr>
<volist name='data' id='vo'>
<tr>
<td><{$vo.id}></td>
<td><{$vo.username}></td>
<td><{$vo.sex}></td>
<td><a href="http://localhost:8080/thinkphp/index.php/User/del/id/<{$vo.id}>">删除</a>|修改</td>
</tr>
</volist>
</table>
</body>
</html>
</body>
</html>
修改数据:
<td><a href="http://localhost:8080/thinkphp/index.php/User/del/id/<{$vo.id}>">删除</a>|<a href="http://localhost:8080/thinkphp/index.php/User/modify/id/<{$vo.id}>">修改</a></td>
public function modify(){
$this->display();
}
跳转到一个修改页面:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus?">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
</head>
<body>
<form>
姓名:<input type="text" name='username' value="<{$data.username}>"/></br>
性别:男<input type='radio' name='sex' value='1'> 女<input type='radio' name='sex' value='0'></br>
<input type="submit" value='提交修改'/></br>
</form>
</body>
</html>
public function modify(){
$id=$_GET['id'];
$m=M('user');
$arr=$m->find($id);
$this->assign('data',$arr);
$this->display();
}
将值传递给前台模板
<table border='1' width='500' align='center'>
<tr>
<th>id</th>
<th>username</th>
<th>sex</th>
<th>操作</th>
</tr>
<volist name='data' id='vo'>
<tr>
<td><{$vo.id}></td>
<td><{$vo.username}></td>
<td><{$vo.sex}></td>
<td><a href="http://localhost:8080/thinkphp/index.php/User/del/id/<{$vo.id}>">删除</a>|<a href="http://localhost:8080/thinkphp/index.php/User/modify/id/<{$vo.id}>">修改</a></td>
</tr>
</volist>
</table>
</body>
</html>
</body>
</html>
删除方法 把值传递给http://localhost:8080/thinkphp/index.php/User/del/id/<{$vo.id}>
public function del(){
$m=M('user');
$id=$_GET['id'];
$count=$m->delete($id);
echo $count;
if ($count >0){
$this->success('数据删除成功');
}else{
$this->error('数据删除失败');
}
$this->display();
}
修改方法:
<table border='1' width='500' align='center'>
<tr>
<th>id</th>
<th>username</th>
<th>sex</th>
<th>操作</th>
</tr>
<volist name='data' id='vo'>
<tr>
<td><{$vo.id}></td>
<td><{$vo.username}></td>
<td><{$vo.sex}></td>
<td><a href="http://localhost:8080/thinkphp/index.php/User/del/id/<{$vo.id}>">删除</a>|<a href="http://localhost:8080/thinkphp/index.php/User/modify/id/<{$vo.id}>">修改</a></td>
</tr>
</volist>
</table>
把值传递给方法modify,
public function modify(){
$id=$_GET['id'];
$m=M('user');
$arr=$m->find($id);
$this->assign('data',$arr);
$this->display();
}
在把值分配到前台页面
modify 页面:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus?">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
</head>
<body>
<form>
姓名:<input type="text" name='username' value="<{$data.username}>"/></br>
性别:男<input type='radio' name='sex' value='1'> 女<input type='radio' name='sex' value='0'></br>
<input type="submit" value='提交修改'/></br>
</form>
</body>
</html>