PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, March 8, 2022

[FIXED] Yii2 export after filter gridview

 March 08, 2022     mysql, php, sql, yii, yii2     No comments   

Issue

This is my url after I done my filtering.

http://localhost/VMS/frontend/web/index.php?r=report%2Ffilter&ic_passport=&name=&unit_no=&category=4&purpose=

1) I tried to get the param inside the url

$category = Yii::$app->getRequest()->getQueryParam('category');

Yii::$app->request->getParam('category');

Yii::$app->request->get('category');

but it does not works. Anything I done wrong?

2)Let say I wanted to do query based on the URL so that I can only export out the result that only been filter

  `Table1::find()
  ->andwhere(['category_id'=>$category])
  ->andWhere(['visitor_name'=>$visitor_name])
  ->andWhere(['ic'=>$ic_passport])
  ->andWhere(['unit_no'=>$unit_no])
  ->andWhere(['purpose_id'=>$purpose])
  ->all(),`

Based on the filter url, it will come out result that have category 4. But when I used my own created query, it will come out 0 result because other attribute is blank. Why in the url the attribute can be leave blank and it works but in the query it cant?

UPDATED SOLUTION:

  `Table1::find()
  ->andFilterwhere(['category_id'=>$category])
  ->andFilterWhere(['visitor_name'=>$visitor_name])
  ->andFilterWhere(['ic'=>$ic_passport])
  ->andFilterWhere(['unit_no'=>$unit_no])
  ->andFilterWhere(['purpose_id'=>$purpose])
  ->all()`

Just use ->andFilterWhere.


Solution

1) get() and getQueryParam() should definitely work (first one is basically alias for the second) - if it's not working you must do something wrong.

2) Use andFilterWhere() instead of andWhere() - it works the same way but if variable in condition is empty it's ignored.



Answered By - Bizley
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing