您的位置:首页 > 编程语言 > PHP开发

YII基本操作的一些代码

2016-10-25 19:16 399 查看
本人在学习YII的时候写的一些代码 特别乱建议初学者别模仿!!

$where=Yii::$app->request->get();
//print_r($where);die;
$query=new \yii\db\Query();
$query->from('student');
if(!empty($where['a_name'])){
$query->andWhere(['a_name'=>$where['a_name']]);
}else{
$where['a_name']="";
}
if(isset($where['age1'])&&$where['age1']!==""){
$query->andWhere(['>=','age',$where['age1']]);
}else{
$where['age1']="";
}
if(isset($where['age2'])&&$where['age2']!==""){
$query->andWhere(['<=','age',$where['age2']]);
}else{
$where['age2']="";
}
$pagination=new Pagination(['totalCount'=>$query->count()]);
$pagination->setPageS
4000
ize(2);
$users=$query->offset($pagination->offset)->limit($pagination->limit)->all();
return $this->render('sou',['users'=>$users,'where'=>$where,'pagination'=>$pagination]);


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

$session = Yii::$app->session;
$user_id = $session->get('id');
$b_id=$session->get('b_id');
$model = new Uploaded();
if(\Yii::$app->request->isPost) {
$data = Yii::$app->request->post();
//接取上传的文件
$model->imageFile = UploadedFile::getInstance($model, 'imageFile');
$model->validate();
$model->imageFile->saveAs('uploads/' . $model->imageFile->baseName . '.' . $model->imageFile->extension);
$path='uploads/' . $model->imageFile->baseName . '.' . $model->imageFile->extension;
unset($data['_csrf-frontend']);
unset($data['Uploaded']);
$data['img']=$path;
$connection=Yii::$app->db;
$sql="select a_name from student WHERE a_id=$user_id";
$name=$connection->createCommand($sql)->queryOne();
$data['people']=$name['a_name'];
$data['b_id']=$b_id;
$data['time']=date('Y-m-d H:m:s',time());
$query=$connection->createCommand()->insert('tobic',$data)->execute();
if($query){
Yii::$app->getSession()->setFlash('success', '发表成功');
return $this->redirect(['grade/sel']);
}else{
Yii::$app->getSession()->setFlash('error', '发表失败');
return $this->redirect(['grade/abb']);
}
}else{
return $this->render('hua', ['model' => $model]);
}

<?
use yii\bootstrap\ActiveForm;
use yii\helpers\Url;
use yii\helpers\Html;

$this->registerJsFile("assets/8e141091/jquery.min.js",['position'=>\yii\web\View::POS_HEAD]);
?>

<?php
$form=ActiveForm::begin([
'options'=>['enctype' => 'multipart/form-data'],
'action'=>Url::toRoute(['grade/hua']),
'method'=>'post',

]);
echo "文章题目:".Html::input('text','title','',['id'=>'title'])."<br><br>";
echo "内容编辑:".Html::textarea('content','',['id'=>'content'])."<br><br>";
echo Html::a('上传图片')."<br><br>";
echo $form->field($model, 'imageFile')->fileInput();
echo Html::submitButton("发表话题");
ActiveForm::end();
?>

<?php
use yii\widgets\ActiveForm;
use yii\helpers\Url;
use yii\helpers\Html;
?>
<?php
$form=ActiveForm::begin([
'action'=>Url::toRoute(['grade/sou']),
'method'=>'get',
]);
echo "姓名:".Html::input('text','a_name',$where['a_name']);
echo "年龄区间:".Html::input('text','age1',$where['age1']);
echo "-".Html::input('text','age2',$where['age2']);
echo Html::submitButton("搜索");
ActiveForm::end();
?>
<br>
<br>
<table class="table">
<?php foreach($users as $user){?>
<tr>
<td><?php echo $user['a_name'];?></td>
<td><?php echo $user['age']; ?></td>
</tr>
<?php }?>
</table>
<?php
echo \yii\widgets\LinkPager::widget([
'pagination'=>$pagination,
'nextPageLabel'=>'下一页',
'firstPageLabel'=>'首页',
]);
?>

$query = new Query();

$members = $query->select(['username','class_stu.user_id'])

 ->from('class_stu')

 ->innerJoin('user','class_stu.user_id=user.id')

 ->where(['class_id'=>$class_id])

 ->all();
$sql = "insert into `msg` (`msg`,`user_id`,`add_time`) values ('$msg',$user,'$data')";

$res = Yii::$app->db->createCommand($sql)->execute();
$connection=Yii::$app->db;
$connection->createCommand()->update('mess',['m_status'=>3],"b_id=$b_id")->execute();

$connection=Yii::$app->db;
$sql="select * from abcd";
$data=$connection->createCommand($sql)->queryAll();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: