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)

  • What are these variables in general? Why did you create them? Do you have a radioButtonList field radioButtonList Did you try to look into it after submitting the form? - Bookin

1 answer 1

So, go to the documentation , we see:

The data item used to generate the radio buttons. While the keys are the corresponding radio values.

Hence, radioList first parameter, it expects from you, the array where the keys are INPUT VALUES, and the array values ​​are labels (label / description).

Actually, if you look at the html that comes out of your code:

 $form->field($model, 'radioButtonList') ->radioList([ 'Button1' => 'ΠΎΡ‚ 0-1500', 'Button2' => 'ΠΎΡ‚ 3000-5000', 'Button3' => 'ΠΎΡ‚ 5000-20000' ]); 

That will see approximately:

 <label>ΠΎΡ‚ 0-1500</label> <input type="radio" value="Button1" name="radioButtonList"/> <label>ΠΎΡ‚ 3000-5000</label> <input type="radio" value="Button2" name="radioButtonList"/> <label>ΠΎΡ‚ 5000-20000</label> <input type="radio" value="Button3" name="radioButtonList"/> 

What will happen if you post this form? Well, for example a post? What will you get?

In the $_POST Button1 , you get the value of Button1 , Button2 or Button3 .

And how do we get value?

From the array to the line - $_POST['radioButtonList']

Through the framework method - Yii::$app->request->post('radioButtonList')

Upload to model -

 $model = new Product(); if($model->load(Yii::$app->request->post())){ ... $model->radioButtonList; } 

So far, your controller is a solid set of lines of code that doesn't even bind in your head, I don't even want to describe. Just show you that by default, your form will send a POST request.

The reason for your problems (it seems to me that the same reason why you do not want to help here) - you should study the documentation and repeat the examples that are in it. This is generally at least. (at least - link ).