Menus

Saturday, 13 January 2018

Yii2 dropdown multiple selected



MultiSelect Widget for Yii2

It supports searching, remote data sets, and infinite scrolling of results.

https://github.com/kartik-v/yii2-widget-select2

Kartik Multiselect widget


in _form.php

use kartik\select2\Select2;



<?php
    echo Select2::widget([
        'model' => $model,
        'name' => 'certificates',
        'attribute' => 'certificates',
        'data' => ArrayHelper::map(Mulcertificates::find()->orderBy('description')->all(),'id','description'),  //['1'=>'1','2'=>2],
        'options' => [
            'placeholder' => 'Select certificates ...',
            'multiple' => true
    ],
    ]);
    ?>


Controller.php

public function actionCreate()
    {
        $model = new Mulselect();

        if ($model->load(Yii::$app->request->post())) {
         
            $model->certificates = implode(",",$model->certificates);
            
            $model->save();
            return $this->redirect(['view', 'id' => $model->id]);
         
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }


 public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            $model->certificates= explode(',', $model->certificates);
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }


Links

Select2 Widget 

Tuesday, 9 January 2018

PHP - MySQL - Connection


MySQL Connection Using PHP Script

PHP provides mysql_connect() function to open a database connection.

Syntax

connection mysql_connect(server,user,passwd,new_link,client_flag);


Returns a MySQL link identifier on success or FALSE on failure.


Example

<?php
         $dbhost = 'localhost';
         $dbuser = 'test';
         $dbpass = 'Ii@21!abg';
         $conn = mysql_connect($dbhost, $dbuser, $dbpass);
         if(! $conn ) {
            die('Could not connect: ' . mysql_error());
         }
         echo 'Connected successfully<br>';

         $sql = 'SELECT id, name, course FROM std';

         mysql_select_db('ellorahs_test');
         $retval = mysql_query( $sql, $conn );
   
         if(! $retval ) {
              die('Could not get data: ' . mysql_error());
         }
   
         while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
            echo "ID :{$row['id']}  <br> ".
                 "Name : {$row['name']} <br> ".
                 "Course : {$row['course']} <br> ".
                 "--------------------------------<br>";
         } 
         echo "Fetched data successfully\n";

         mysql_close($conn);
?>


mysql_close()


Disconnect from the MySQL database anytime using another PHP function mysql_close().


SELECT command is used to fetch data from the MySQL database. 


Selecting a MySQL Database Using PHP

mysql_select_db to select a database. 
It returns TRUE on success or FALSE on failure.

Fetching Data Using a PHP


The SQL SELECT command is used to fetch data from the MySQL database.

SELECT command into a PHP function mysql_query().


mysql_fetch_array() can be used to fetch all the selected data.