Trying to filter on yii2. There is a form field in it 3 input(type="radio") , each input must search for products with a price in this range.
Controller code where the search is performed:
public function actionFilter() { $filter = trim(Yii::$app->request->get('filter')); $this->setMeta('MAC-SHOPPER | ' . $filter); if (!$filter) { return $this->render('filter'); } /* if ($filter <= 15) { $query = Product::find()->where(['<=', 'price', 15]); }*/ $model = new Product(); if($Button1) { $query = Product::find()->where(['between', 'price', "0", "50" ])->all(); } //Π‘ΠΎΠ·Π΄Π°Π΅ΠΌ ΠΎΠ±ΡΠ΅ΠΊΡ ΠΊΠ»Π°ΡΡΠ° Pagination //ΠΠ΅ΡΠ΅Π΄Π°Π΅ΠΌ ΡΠΎΡΠ°Π» ΠΊΠ°ΡΠ½Ρ - ΠΎΠ±ΡΠ΅Π΅ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ Π·Π°ΠΏΠΈΡΠ΅ΠΉ, ΠΊΠΎΡΠΎΡΡe ΠΌΡ Π²ΡΡΠ°ΡΠΈΠΌ $pages = new Pagination(['totalCount' => $query->count(), 'pageSize' => 2, 'forcePageParam' => false, 'pageSizeParam' => false]); //ΠΡΠΏΠΎΠ»Π½ΡΠ΅ΠΌ ΡΠ°ΠΌ Π·Π°ΠΏΡΠΎΡ //offset - Ρ ΠΊΠ°ΠΊΠΎΠΉ Π·Π°ΠΏΠΈΡΠΈ Π½Π°ΡΠΈΠ½Π°ΡΡ //limit - ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ Π·Π°ΠΏΠΈΡΠ΅ΠΉ $products = $query->offset($pages->offset)->limit($pages->limit)->all(); return $this->render('filter', compact('products', 'pages', 'filter', 'model')); } Product Model:
<?php namespace app\models; use yii\db\ActiveRecord; //ΠΊΠ»Π°ΡΡ Π΄Π»Ρ ΡΠ°Π±Π»ΠΈΡΡ ΠΊΠ°ΡΠ΅Π³ΠΎΡΠΈΠΈ class Product extends ActiveRecord { public $Button1; public $Button2; public $Button3; public $radioButtonList; //ΠΠΎΠ²Π΅Π΄Π΅Π½ΠΈΠ΅ Π΄Π»Ρ ΠΊΠ°ΡΡΠΈΠ½ΠΎΠΊ public function behaviors() { return [ 'image' => [ 'class' => 'rico\yii2images\behaviors\ImageBehave', ] ]; } public static function tableName() { return 'product'; } public function getCategory() { //Π‘Π²ΡΠ·Ρ ΡΠ°Π±Π»ΠΈΡ, ΠΎΠ΄ΠΈΠ½ ΠΏΡΠΎΠ΄ΡΠΊΡ ΠΌΠΎΠΆΠ΅Ρ ΠΈΠΌΠ΅ΡΡ ΠΎΠ΄Π½Ρ ΠΊΠ°ΡΠ΅Π³ΠΎΡΠΈΡ (hasOne()) return $this->hasOne(Category::className(), ['id' => 'category_id']); } } ?> The form itself:
<?php $form = ActiveForm::begin([ 'id' => 'task-form', 'action' => \yii\helpers\Url::to(['category/filter']), ] )?> <?= $form->field($model, 'radioButtonList') ->radioList([ 'Button1' => 'ΠΎΡ 0-1500', 'Button2' => 'ΠΎΡ 3000-5000', 'Button3' => 'ΠΎΡ 5000-20000' ],[ 'id' => 'radio_button', ]); ?> <?= Html::submitButton('ΠΠ°ΠΉΡΠΈ', ['class' => 'btn btn-success']);?> <?php $form = ActiveForm::end() ?> How do I put in the properties $Button1 , $Button2 , $Button3 price from the table of products, so that when I click on a certain input, it displays the goods as done in the controller's condition (that is, by price range)
radioButtonListfieldradioButtonListDid you try to look into it after submitting the form? - Bookin