1.使用composer下载Yii的命令
composer create-project --prefer-dist --stability=dev yiisoft/yii2-app-basic basic
注意:一定要切换到WWW工作目录下 以上命令是DOS命令 cmd中运行,前提是你已经安装了最新版本的composer
2.建表 watch
CREATE TABLE `watch` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`wname` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`wprice` decimal(10, 2) NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = MyISAM AUTO_INCREMENT = 14 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
INSERT INTO `watch` VALUES (4, '天王手表', 8989.00);
INSERT INTO `watch` VALUES (7, 'wwww', 222.00);
INSERT INTO `watch` VALUES (9, 'wwww', 555.00);
INSERT INTO `watch` VALUES (10, 'wwww', 555.00);
INSERT INTO `watch` VALUES (11, 'wwww', 222.00);
INSERT INTO `watch` VALUES (12, 'wwww', 222.00);
INSERT INTO `watch` VALUES (13, '天梭手表88', 11236.00);
3.添加表单
<form action="?r=watch/add" method="post">
手表名称 <input type="text" name="wname" id=""><br>
手表价格 <input type="text" name="wprice" id=""><br>
<input type="submit" value="添加">
</form>
4.列表展示页面
<table>
<tr>
<td>id</td>
<td>手表名称</td>
<td>手表价格</td>
<td>操作</td>
</tr>
<?php foreach($data as $k=>$v):?>
<tr>
<td><?php echo $v['id'];?></td>
<td><?php echo $v['wname'];?></td>
<td><?php echo $v['wprice'];?></td>
<td>
<a href="?r=watch/edit&id=<?php echo $v['id']?>">编辑</a>||
<a onclick="return confirm('确定要删除吗?');" href="?r=watch/delete&id=<?php echo $v['id'];?>">删除</a>
</td>
</tr>
<?php endforeach;?>
</table>
<a href="?r=watch/index">添加</a>
5.修改页面
<form action="?r=watch/update" method="post">
<input type="hidden" name="id" value="<?php echo $info['id'];?>">
手表名称 <input type="text" name="wname" value="<?php echo $info['wname'];?>"><br>
手表价格 <input type="text" name="wprice" value="<?php echo $info['wprice'];?>"><br>
<input type="submit" value="修改">
</form>
6.WatchController控制器页面
<?php
namespace app\controllers;
use Yii;
use app\models\Watch;
use yii\web\Controller;
class WatchController extends Controller
{
public $enableCsrfValidation = false;
/**
* @inheritDoc
*/
public function actionIndex()
{
return $this->render('index');
}
/**
* 添加
*/
public function actionAdd(){
// print_r(Yii::$app->request->post('wname'));die;
$watch = new Watch;
//接收表单以post方式传输过来的数据 最上方要引入Yii
$watch['wname'] = Yii::$app->request->post('wname');
$watch['wprice'] = Yii::$app->request->post('wprice');
$res = $watch->save();
//返回bool true表示添加成功
if($res){
Yii::$app->session->setFlash('success', '添加成功!');
$this->redirect(array('watch/show'));
}else{
Yii::$app->session->setFlash('error', '添加失败!');
$this->redirect(array('watch/index'));
}
}
//列表展示
public function actionShow(){
$data = Watch::find()->all();
return $this->render("show",["data"=>$data]);
}
//删除
public function actionDelete(){
$id = \Yii::$app->getRequest()->get('id');
$model = Watch::findOne($id);
$res = $model->delete();
if($res){
Yii::$app->session->setFlash('success', '删除成功!');
$this->redirect(array('watch/show'));
}else{
Yii::$app->session->setFlash('error', '删除失败!');
$this->redirect(array('watch/show'));
}
}
//编辑
public function actionEdit($id){
//接收id值 以下写法是获取网址中某一个参数的值
// $id =Yii::$app->getRequest()->get('id');
// var_dump($id);
$info = Watch::findOne($id);
return $this->render('update',['info'=>$info]);
}
//修改
public function actionUpdate(){
// 实例化对象 接收id值
$id = Yii::$app->request->post('id');
//接收新值
$wname = Yii::$app->request->post('wname');
$wprice = Yii::$app->request->post('wprice');
$data = [
'wname' => $wname,
'wprice'=>$wprice,
];
//写修改的sql语句
//"id=$id"注意这里不能写'' 否则会报错
$res = Watch::updateAll($data,"id=$id");
//var_dump($res);//返回 int 1表示修改成功 int 0表示未做修改
if($res==1){
Yii::$app->session->setFlash('success', '修改成功!');
$this->redirect(array('watch/show'));
}else if($res==0){
Yii::$app->session->setFlash('error', '数据未做任何修改!!!');
$this->redirect(array('watch/show'));
}else{
Yii::$app->session->setFlash('error', '修改失败!');
$this->redirect(array('watch/edit',['id',$id]));
}
}
}
?>
7.模型文件 Watch.php
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "country".
*
* @property string $code
* @property string $name
* @property int $population
*/
class Watch extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'watch';
}
public function rules(){
return [
[['wname'],'string','max'=>100],
[['wprice','integer']],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'wname' => Yii::t('app','手表名称'),
'wprice' => Yii::t('app','手表价格'),
];
}
}
支持原创,学习记录的,如果对您有帮助,请关注,互相学习!!!
您的点赞是博主前进的动力,老铁!!!