/*传页面
* 添加、展示到表单页面
*/
public function actionMyshow(){
return $this->render('add');
}
/*增
* 添加接值、判断添加成功或失败
*/
public function actionAdd_do(){
$data = $_POST;
$yy = Yii::$app->db->createCommand()->insert('user',$data)->execute();
if($yy){
echo "<script>alert('添加成功')</script>";
$this->redirect('index.php?r=myshow/show');
}else{
echo "<script>alert('添加失败')</script>";
$this->redirect('index.php?r=myshow/add');
}
}
/*查
* 展示页面、查询
*/
public function actionShow()
{
$db = yii::$app->db;
$sql = "select * from user";
$data = $db->createCommand($sql)->queryAll();
$p = empty($_GET['page']) ? 1 : $_GET['page'];
$num = count($data);//总条数
$size = 2;//每页显示条数
$end = ceil($num / $size);//尾页
$prev = $p - 1 < 1 ? $p : $p - 1;//上一页
$next = $p + 1 >= $end ? $end : $p + 1;//下一页
$offset = ($p - 1) * $size;//偏移量
$sql = "select * from user limit $offset,$size";//分页查询
$arr = $db->createCommand($sql)->queryAll();
return $this->render('show', ['data' => $arr,'end'=>$end,'prev'=>$prev,'next'=>$next]);
}
/*删
* 删除、判断删除成功或失败
*/
public function actionDelete(){
/*删除表单页面接收当前条的id
* <a href="index.php?r=myshow/delete&id=<?php echo $v['id']?>">删除</a>
*/
$request = \Yii::$app->request;
$id = $request->get('id');
$res = \Yii::$app->db->createCommand()->delete('user', "id = $id")->execute();
if($res){
echo "<script>alert('删除成功')</script>";
$this->redirect('index.php?r=myshow/show');
}else{
echo "<script>alert('删除失败')</script>";
$this->redirect('index.php?r=myshow/show');
}
}
public function actionUpdate(){
$request = \Yii::$app->request;
$id = $request->get('id');
$command = \Yii::$app->db->createCommand("SELECT * FROM user WHERE id=$id");
$post = $command->queryOne();
return $this->render('update',['list'=>$post]);
//表单页面接到值展示如下:
/*
* <tr>
<td>姓名</td>
<td><input type="text" name="name" value="<?php echo $list['name']?>"/></td>
</tr>
*/
}
/*
* 修改数据判断修改成功或失败
*/
public function actionUpdate_do(){
$id=$_POST['id'];
$data = $_POST;
$res = \Yii::$app->db->createCommand()->update('user',$data,["id" => $id])->execute();
if($res){
echo "<script>alert('修改成功')</script>";
$this->redirect('index.php?r=myshow/show');
}else{
echo "<script>alert('修改失败')</script>";
$this->redirect('index.php?r=myshow/show');
}
}