yii添加数据方式三

模型

<?php 
	namespace backend\models;
	
	use Yii;
	use yii\db\ActiveRecord;
	
	class user extends ActiveRecord{
		public $admin=0;
		public $respass;
		public static function tableName(){
			return 'user';
		}
		public function rules(){
			return [
				['username','required','message'=>'用户名不能为空','on'=>['add']],
				['userpass','required','message'=>'密码不能为空','on'=>['add']],
				['usermail','required','message'=>'邮箱不能为空','on'=>['add']],
				['usermail','email','message'=>'邮箱格式不正确','on'=>['add']],
				['admin','required','message'=>'非法操作','on'=>['add']],
				['admin','number','message'=>'必须为数字','on'=>['add']],
				['static','required','message'=>'状态错误','on'=>['add']],
				['static','number','message'=>'状态必须为数字','on'=>['add']],
				['respass','required','message'=>'确认密码不能为空','on'=>['add']],
				['respass','compare','compareAttribute'=>'userpass','message'=>'密码和确认密码不一致','on'=>['add']],
			];
		}
		public function attributeLabels()
		{
			return [
				'username' => '用户名',
				'userpass'=>'密码',
				'usermail'=>'邮箱',
				'admin'=>'用户身份',
				'static'=>'状态',
				'respass'=>'确认密码'
			];
		}
		public function add($data){
			$this->scenario='add';
			if($this->load($data) && $this->validate()){
				$this->createtime=time();
				if($this->save(false)){
					return true;
				}else{
					return false;
				}
			}
			return false;
		}
	}
?>

控制器

<?php 
namespace backend\controllers;

use Yii;
use yii\web\Controller;
use backend\models\user;
use yii\helpers\Url;	
class UserController extends Controller{
	public $layout='mains';
	public function actionIndex()
	{
		$user=new user();
		$model=$user::find()->all();
	    return $this->render('index',[
			'model'=>$model
		]);
	}
	public function actionAdd(){
		$model=new user();
		if(Yii::$app->request->isPost){
			$post=Yii::$app->request->post();
			$res=$model->add($post);
			if($res){
				Yii::$app->session->setFlash('success','添加成功');
				return $this->redirect(Url::toRoute(['index']));
			}else{
				Yii::$app->session->setFlash('error','添加失败');
			}
		}
		return $this->render('add',[
			'model'=>$model
		]);
	}
	public function actionStatic(){
		$id=Yii::$app->request->get('id');
		$user=new user();
		$model=$user::find()->where(['id'=>$id])->one();
		if($model){
			$static=$model['static']==0?1:0;
			$model['static']=$static;
			$res=$model->save();
			if($res){
				
				return \json_encode(['res'=>'更新成功','data'=>2,'state'=>$static]);
			}else{
				return \json_encode(['res'=>'更新失败','data'=>1,'state'=>0]);
			}
		}else{
			return \json_encode(['res'=>'数据不存在','data'=>0,'state'=>0]);
		}
	}
}
?>

添加视图

<?php 
	use yii\helpers\Url;
	use yii\widgets\ActiveForm;
	
	$model['static']=1;
?>
<div class="alert alert-success" role="alert">
	<div class="container" style="padding: 0;">
		<a href="<?=Url::to(['index'])?>" class="alert-link">用户列表</a>
	</div>
</div>
<div class="main" style="width: 1200px;margin: 0 auto;margin-top: 100px;">
	<?
		$form= ActiveForm::begin([
			'fieldConfig'=>[
				'template'=>'<div class="form-group">{label}{input}{error}</div>'
			]
		]);
	?>
		<?= $form->field($model,'username')->textInput(['maxlength' => 20])->label('用户名')?>
		<?= $form->field($model,'userpass')->passwordInput(['maxlength' => 20])->label('密码')?>
		<?= $form->field($model,'respass')->passwordInput(['maxlength' => 20])->label('确认密码')?>
		<?= $form->field($model,'usermail')->textInput(['maxlength' => 20])->label('邮箱')?>
		<?= $form->field($model,'admin')->radioList(['0'=>'普通会员','1'=>'超级管理员'])->label('身份')?>
		<?= $form->field($model,'static')->radioList(['1'=>'正常','0'=>'禁止'])->label('状态')?>
		<!-- <div class="form-group">
			<label for="exampleInputEmail1">Email address</label>
			<input type="email" class="form-control" id="exampleInputEmail1" placeholder="Email">
		</div>
		<div class="form-group">
			<label for="exampleInputPassword1">Password</label>
			<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
		</div> -->
		<button type="submit" class="btn btn-default">提交</button>
	<?php 
		ActiveForm::end();
	?>
</div>

数据展示

<?php 
	use yii\helpers\Html;
	use yii\helpers\Url;
?>
<div class="alert alert-success" role="alert">
	<div class="container" style="padding: 0;">
		<a href="<?=Url::to(['add'])?>" class="alert-link">添加用户</a>
	</div>
</div>
<div class="main" style="width: 1200px;margin: 0 auto;margin-top: 50px;">
	<table class="table table-condensed">
		<tr>
			<th>用户名</th>
			<th>邮箱</th>
			<th>身份</th>
			<th>状态</th>
			<th>登陆ip</th>
			<th>登陆时间</th>
			<th>注册时间</th>
			<th>操作</th>
		</tr>
		<?php 
			foreach($model as $key=>$val){
		?>
			<tr>
				<td><?=$val['username']?></td>
				<td><?=$val['usermail']?></td>
				<td><?=$val['admin']?><?=$val['admin']==0?'普通会员':'超级管理员'?></td>
				<td id="fun<?=$val['id']?>" onclick="fun(<?=$val['id']?>)"><?=$val['static']==1?'<button type="button" class="btn btn-success">正常</button>':'<button type="button" class="btn btn-danger">禁止</button>'?></td>
				<td><?=$val['logip']?></td>
				<td><?=date('Y-m-d H:i:s',$val['logtime'])?></td>
				<td><?=date('Y-m-d H:i:s',$val['createtime'])?></td>
				<td>
					<a href="<?=Url::to(['add','id'=>$val['id']])?>" class="glyphicon glyphicon-pencil"></a>
					<a href="<?=Url::to(['del','id'=>$val['id']])?>" class="glyphicon glyphicon-trash" style="margin-left: 20px;"></a>
				</td>
			</tr>
		<?php 
			}
		?>
	</table>
</div>
<script>
	function fun(id){
		$.get("<?=Url::to(['static'])?>",{id:id},function(data){
			dat=JSON.parse(data);
			if(dat.data=='0'){
				alert(dat.res);
			}else if(dat.data==1){
				alert(dat.res);
			}else if(dat.data==2){
				if(dat.state==1){
					$('#fun'+id).html("<button type='button' class='btn btn-success'>正常</button>");
				}else{
					$('#fun'+id).html("<button type='button' class='btn btn-danger'>禁止</button>");
				}
			}
		})
	}
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

原克技术

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值