zoukankan      html  css  js  c++  java
  • Yii2.0页面提示消息

    适用情况:比如提交一个表单,提交完成之后在页面展示一条提示消息。

    控制器里面这样写:

    单条消息:

    Yii::$app->getSession()->setFlash('error', 'This is the message');

    Yii::$app->getSession()->setFlash('success', 'This is the message');

    Yii::$app->getSession()->setFlash('info', 'This is the message');

    多条消息:

    Yii::$app->getSession()->setFlash('error', ['Error 1', 'Error 2']);

    然后是视图里面:

    先引入Alert:use yiiootstrapAlert;

    然后是:

    if( Yii::$app->getSession()->hasFlash('success') ) {
    	echo Alert::widget([
    		'options' => [
    			'class' => 'alert-success', //这里是提示框的class
    		],
    		'body' => Yii::$app->getSession()->getFlash('success'), //消息体
    	]);
    }
    if( Yii::$app->getSession()->hasFlash('error') ) {
    	echo Alert::widget([
    		'options' => [
    			'class' => 'alert-error',
    		],
    		'body' => Yii::$app->getSession()->getFlash('error'),
    	]);
    }
    

    如果有消息就会显示对应消息,表现是一个div,和bootstrap的警告框是一样的。
    你想把消息提示放在哪里,把上述代码就放到那里就可以了。

    *** 题外话,这个编辑器是要用Markdown语法写?

    public function actionIndex()
    {
    $db = Yii::$app->db;
    $art = $db -> createCommand("select * from country where id=:id")->bindValue(':id','3')->queryOne();

    //print_r($art);die;
    $model = new Country();
    $request = Yii::$app->request->post();

    if($model->load($request) && $model->validate()){

    $model->name = $request['Country']['name'];
    $model->password = $request['Country']['password'];
    $model->repassword = $request['Country']['repassword'];
    $model->selects = $request['Country']['selects'];
    if($model->save()){
    Yii::$app->getSession()->setFlash('success', '保存成功了亲');
    return $this->refresh();//防刷新
    }else{
    Yii::$app->getSession()->setFlash('error', '保存失败');
    }
    }
    return $this->render('index', [
    'model' => $model,
    ]);
    }

    <?php
    $form = ActiveForm::begin([
    'action' => ['test/getpost'],
    'method'=>'post',
    ]); ?>

    <? echo $form->field($model, 'username')->textInput(['maxlength' => 20]) ?>
    <? echo $form->field($model, 'password')->passwordInput(['maxlength' => 20]) ?>
    <? echo $form->field($model, 'sex')->radioList(['1'=>'男','0'=>'女']) ?>
    <? echo $form->field($model, 'edu')->dropDownList(['1'=>'大学','2'=>'高中','3'=>'初中'],
    ['prompt'=>'请选择','style'=>'120px']) ?>
    <? echo $form->field($model, 'file')->fileInput() ?>
    <? echo $form->field($model, 'hobby')->checkboxList(['0'=>'篮球','1'=>'足球','2'=>'羽毛球','3'=>'乒乓球']) ?>
    <? echo $form->field($model, 'info')->textarea(['rows'=>3]) ?>

    <? echo $form->field($model, 'userid')->hiddenInput(['value'=>3]) ?>

    <? echo Html::submitButton('提交', ['class'=>'btn btn-primary','name' =>'submit-button']) ?>
    <? echo Html::resetButton('重置', ['class'=>'btn btn-primary','name' =>'submit-button']) ?>
    <?php ActiveForm::end(); ?>

    查数据:

    1: 此方法返回 ['name' => 'daxia'] 的所有数据;

    User::find()->where(['name' => 'daxia'])->all();
    2: 此方法返回 ['name' => 'daxia']的一条数据

    User::find()->where(['name' => 'daxia'])->one();
    3: 在条件name的基础上,额外添加另一个条件sex

    User::find()->where(['name' => 'daxia'])->andWhere(['sex' => '女'])->one();

    或者:

    User::find()->where(['name' => 'daxia', 'sex' => '女'])->one();

    说明: 这两种方法都是可以的
    4: andFilterWhere/andWhere应用: 在[1427925600-1427968800]之间查询

    User::find()->andFilterWhere(['between', 'regtime', '1427925600', '1427968800’])
    说到andFilterWhere,下面我把用到的各种的情况示例列出:

    1) : sql: id=1 AND id=2
    条件: ['and', 'id=1', 'id=2']

    2) : sql: id=1 OR id=2
    条件: ['or', 'id=1', 'id=2']

    3) : sql: id BETWEEN 1 AND 10
    条件: ['between', 'id', 1, 10]

    4) : sql: id IN (1, 2, 3)
    条件: ['in', 'id', [1, 2, 3]]

    5) : sql: name LIKE '%tester%' 模糊查询
    条件: ['like', 'name', 'tester']

    6) : sql: age>10
    条件: ['>', 'age', 10]

    5: orderBy() 应用

    sql: ORDER BY `id` ASC, `name` DESC

    Yii对应的model书写如下:
    $query->orderBy([
    'id' => SORT_ASC, 升序 默认
    'name' => SORT_DESC, 降序
    ]);
    6: groupBy() 应用:

    sql: ... GROUP BY `id`, `status`

    Yii对应的model书写如下:

    $query->groupBy(['id', 'status']);
    7: having()应用:

    sql: ... HAVING `status` = 1

    Yii对应的model书写如下:

    $query->having(['status' => 1]);
    8: limit() offset() 应用:

    sql: ... LIMIT 10 OFFSET 20

    Yii对应的model书写如下

    $query->limit(10)->offset(20);
    9: 用自己书写的sql语句,去查询符合的数据

    User::findBySql('SELECT * FROM user')->one(); 此方法是用 sql 语句查询 user 表里面的一条数据;

    User::findBySql('SELECT * FROM user')->all(); 此方法是用 sql 语句查询 user 表里面的所有数据;
    说明: 测试 - 你也许想要测试或者使用一个由 yiidbQuery 对象创建的 SQL 语句。 你可以使用以下的代码来达到目的:

    $query->createCommand()->getRawSql();
    下面就是官网上面展示的,一些比较常见的查询方法:

    yiidbQuery 提供了一整套的用于不同查询目的的方法。
    ● yiidbQuery::all(): 将返回一个由行组成的数组,每一行是一个由名称和值构成的关联数组(译者注:省略键的数组称为索引数组)。
    ● yiidbQuery::one(): 返回结果集的第一行。
    ● yiidbQuery::column(): 返回结果集的第一列。
    ● yiidbQuery::scalar(): 返回结果集的第一行第一列的标量值。
    ● yiidbQuery::exists(): 返回一个表示该查询是否包结果集的值。
    ● yiidbQuery::count(): 返回 COUNT 查询的结果。
    ● 其它集合查询方法: 包括 yiidbQuery::sum(), yiidbQuery::average(), yiidbQuery::max(), yiidbQuery::min() 等. $q 是一个必选参数, 既可以是一个字段名称,又可以是一个 DB 表达式。

  • 相关阅读:
    LeetCode-860. Lemonade Change
    LeetCode-455.Assign Cookies
    LeetCode-122.Best Time to Buy and Sell Stock II
    LeetCode-438.Find All Anagrams in a String
    LeetCode-50.Pow(x,n)
    LeetCode-236.Lowest Common Ancestor of a Binary Tree
    LeetCode-235.Lowest Common Ancestor of a Binary Search Tree
    LeetCode-98.Validate Binary Search Tree
    LeetCode-18.4Sum
    LeetCode-15.3Sum
  • 原文地址:https://www.cnblogs.com/yszr/p/7895559.html
Copyright © 2011-2022 走看看