Search the Community
Showing results for tags 'yii2'.
-
I am developing a database application using Yii Framework. I am reading tables from MySQL database and displaying them to the user. I need the user to be able to filter the fields in the table or search for a certain value. For example, I have a table named "supermarkets": CREATE TABLE IF NOT EXISTS `supermarkets` ( `name` varchar(71) NOT NULL, `location` varchar(191) DEFAULT NULL, `telephone` varchar(68) DEFAULT NULL, `fax` varchar(29) DEFAULT NULL, `website` varchar(24) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; .../model/supermarkets: <?php namespace app\models; use yii\db\ActiveRecord; class Supermarkets extends ActiveRecord { } .../views/supermarkets/index.php: <?php use yii\helpers\Html; use yii\widgets\LinkPager; ?> <h1>Supermarkets</h1> <ul> <?php $array = (array) $supermarkets; function build_table($array){ // start table $html = '<table class="altrowstable" id="alternatecolor">'; // header row $html .= '<tr>'; foreach($array[0] as $key=>$value){ $html .= '<th>' . $key . '</th>'; } $html .= '</tr>'; // data rows foreach( $array as $key=>$value){ $html .= '<tr>'; foreach($value as $key2=>$value2){ $html .= '<td>' . $value2 . '</td>'; } $html .= '</tr>'; } // finish table and return it $html .= '</table>'; return $html; } echo build_table($array); ?> ....Controllers/SupermarketsController: <?php namespace app\controllers; use yii\web\Controller; use yii\data\Pagination; use app\models\Supermarkets; class SupermarketsController extends Controller { public function actionIndex() { $query = supermarkets::find(); $pagination = new Pagination([ 'defaultPageSize' => 20, 'totalCount' => $query->count(), ]); $supermarkets = $query->orderBy('Name') ->offset($pagination->offset) ->limit($pagination->limit) ->all(); return $this->render('index', [ 'supermarkets' => $supermarkets, 'pagination' => $pagination, ]); } } I need the user to be able to filter the table or search its fields by one or more attribute. I'm using Yii2, so CDbcriteria doesn't work. How can I do this?
-
I am using Yii 2 framework and I have a search form with input fields and 2 submit buttons Search and Export. When the user clicks on Search button then index action should execute and when user clicks on Export button then export action should execute. Now even if user clicks on Export button export action is not executed. Below is the search form <?php use yii\helpers\Html; use yii\widgets\ActiveForm; use yii\helpers\ArrayHelper; use app\models\Asccenter; use app\models\Ascuser; ?> <?php $form = ActiveForm::begin([ 'method' => 'get', 'id'=>'form1' ]); ?> <?= $form->field($model, 'ASCId')->dropDownList()?> <?php //Input field 1?> <?= $form->field($model, 'UserId')?> <?php // Input field 2?> <?= $form->field($model, 'Year')?> <?php // Input field 3?> <div class="form-group"> <?php // Search button. When user clicks on Search button then index action should execute ?> <?= Html::submitButton('Search' ,['class' => 'btn btn-primary','id'=>'searchbutton','name'=>'search1','value'=>'search2']) ?> <?php //Reset button to clear the form inputs?> <?= Html::a('Reset',['index'], ['class' => 'btn btn-default']) ?> <?php // Export button. When user clicks on Export button, then export action should execute?> <?= Html::submitButton('Export', ['class' => 'btn btn-default','id'=>'exportbutton','name'>'search1','value'=>'export1']) ?> </div> <?php ActiveForm::end(); ?> Below is the Controller <?php namespace app\controllers; use Yii; use app\models\Ascteacherreport; use app\models\AscteacherreportSearch; use yii\helpers\ArrayHelper; class AscteacherreportController extends Controller { public function actionIndex() { // When user clicks on Search button then this action should execute $searchModel = new AscteacherreportSearch(); if (!Yii::$app->user->can('indexAll')) { $searchModel->UserId = Yii::$app->user->identity->getonlyid(); } $dataProvider = $searchModel->search(Yii::$app->request->queryParams); $query= Ascteacherreport::find(); $count=$query->count(); $pagination= new Pagination(['defaultPageSize' => 5, 'totalCount' => $count]); $training = $query->offset($pagination->offset)->limit($pagination->limit)->all(); return $this->render('index', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'pagination' => $pagination, ]); } public function actionExport() { // When user clicks on Export button of the form then form parameters will be loaded and this action should execute. } }