萌新刚接触YII 做个学习笔记 [ 2.0 版本 ]
学习 YII 以来,一直在yiichina中潜水,查找资料、观看文章,受益众多。对 YII 自我感觉基本入门,于是决定做个简单的文章审核检验自己的学习成果。本系列纯属个人学习笔记文章,诸多不足,多多谅解,大神轻喷。
<?php
//控制器
namespace backend\controllers;
use common\models\Jest_Class;
use Yii;
use yii\web\Controller;
use yii\helpers\Url;
use common\models\Jc;
use common\models\Jests;
use common\models\Jest;
use common\models\JestClass;
use common\models\Comment;
use yii\web\UploadedFile;
use common\models\Login;
use yii\db\Query;
class JestController extends Controller
{
public function actionExamine()
{
$model=new Jests();
$data=$model->jest_list();
//print_r($data);die;
//分类
$jestcalss=new JestClass();
$class=$jestcalss->j_class();
//print_r($class);die;
//评论
$modelp=new Comment();
foreach($data['data'] as $k=>$v){
$data['data'][$k]['com_num']=$modelp->com_list($v['id']);
}
return $this->render('examine',[
'data'=>$data['data'],
'page'=>$data['page'],
'class'=>$class['data'],
'model'=>$model,
]);
}
//修改
public function actionSeen($id)
{
$model=Jest::findOne($id);
//print_r($model);die;
$query=new Query();
$arr=$query->select('cid')->from('jc')->where('jid='.$id)->all();
//print_r($arr);die;
$arr=array_column($arr,'cid');
//print_r($arr);die;
if(Yii::$app->request->isPost)
{
$model->load(Yii::$app->request->post());
$model->save(false);
//关系表修改:先删除后入库
Yii::$app->db->createCommand("delete from jc where jid='$id'")->execute();
$label=Yii::$app->request->post('label');
foreach ($label as $cid) {
Yii::$app->db->createCommand("insert into jc (jid,cid) VALUE ('$id','$cid')")->execute();
}
Yii::$app->session->addFlash('success', '修改数据成功');
return $this->redirect(['examine']);
}
$label_all=Yii::$app->db->createCommand("select * from Jest_class")->queryAll();//标签赋值
$label_all=array_combine(array_column($label_all,'id'),array_column($label_all,'class'));
return $this->render('seen',[
'model'=>$model,
'arr'=>$arr,
'label_all'=>$label_all,
]);
}
public function actionStatus()
{
if (Yii::$app->request->isAjax)
{
$joke_id = Yii::$app->request->get('id');
$status = Yii::$app->request->get('status');
$res=Yii::$app->db->createCommand("update jest set status='$status' where id='$joke_id'")->execute();
if($res) echo 1;
}
}
}
//视图层
/ @var $this \yii\web\View /
/ @var $content string /
use yii\helpers\Html;
use yii\bootstrap\Nav;
use yii\bootstrap\NavBar;
use yii\widgets\Breadcrumbs;
use yii\helpers\Url;
use frontend\assets\AppAsset;
use common\widgets\Alert;
use yii\widgets\LinkPager;
use yii\widgets\ActiveForm;
use yii\web\View;
?>
<tr>
<td>标题</td>
<td>内容</td>
<!-- <td>分类</td> -->
<td>审核</td>
<td>操作</td>
</tr>
<?php foreach($data as $v){?>
<tr>
<td><?= $v['title'].'       '?></td>
<td><?= substr($v['content'],0,10).'......'?></td>
<td>
<select id="sel" ids="<?php echo $v['id']?>">
<option value="">请选择</option>
<option value="0"<?php if($v['status']==0) echo "selected"?>>未通过</option>
<option value="1"<?php if($v['status']==1) echo "selected"?>>审核通过</option>
</select>
</td>
<td><?= Html::a('修改', ['seen', 'id' => $v['id']], ['class' => 'btn btn-primary']) ?></td>
</tr>
<?php }?>
<?= LinkPager::widget([ 'pagination' => $page,]);?>
<?php $this->beginBlock('abc'); ?>
$(document).on('click','.pagination a',function(e){
e.preventDefault();
var url=$(this).attr('href');
$.get(url,function(msg){
//alert(msg);
$('#list').html(msg);
});
});
<?php $this->endBlock(); $this->registerJs($this->blocks['abc'], View::POS_END); ?>
<?php $this->beginBlock('abc'); ?>
$(document).on('change','#sel',function(){
var status=$(this).val();
var id=$(this).attr('ids');
$.ajax({
url:"?r=jest/status",
data:'status='+status+'&id='+id,
success:function(msg){
if(msg==1){alert('状态更新,成功!')}
}
})
})
<?php $this->endBlock(); $this->registerJs($this->blocks['abc'], View::POS_END);
//还有修改文章内容
<?php
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\widgets\ActiveForm;
use yii\ueditor\UEditor;
?>
<div class="row">
<div class="col-lg-5">
<?php $form = ActiveForm::begin(['id' => 'login-form']); ?>
分类标签:
<?php echo Html::checkboxList('label',$arr,$label_all); ?>
<?= $form->field($model, 'title')->textInput(['autofocus' => true]) ?>
<?= $form->field($model,'content')->widget('yii\ueditor\UEditor',[
'clientOptions' => [
//编辑区域大小
'initialFrameHeight' => '200',
//设置语言
'lang' =>'zh-cn', //中文为 zh-cn
]
]);?>
<div class="form-group">
<?= Html::submitButton('提交', ['class' => 'btn btn-primary', 'name' => 'login-button']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
</div>
qq9421
最后登录:2017-09-20
在线时长:1小时33分
- 粉丝1
- 金钱45
- 威望0
- 积分55
共 0 条评论