duananyantan04633 2014-07-25 14:32
浏览 50
已采纳

如何比较Yii中使用CDBCRITERIA的字符串?

I am using Yii framework, I want to do some search filtering, but I am getting wrong results in some cases as follow:

I have 2 check-boxes: Canada and united states, if I check Canada I will get all the results related to Canada only, while if I check united states I will get all the results in the db regardless its related to united states or not, and this bug is absolutely happen because united states string is of 2 parts so it need to be in quotations. here is my code:

the view page:

 echo '<div class="checkbox"><label>'.
       CHtml::checkBox($m2->tag, false, array('value'=>"$m2->tag")).$m2->tag              
       .'</label></div>';

the controller:

$c = new CDbCriteria();
        $c->order = "idJob DESC";
            $model = Jobs::model()->findAll($c);
            $model2 = Tags::model()->findAll();
            $lcr = "";
            $tag="";
            foreach($model2 as $m2){

                if(isset($_POST[$m2->tag])){

                    $tag = $_POST[$m2->tag];                           

                    if($m2->category=='Location')

                        $lcr[]= $tag; 
                }
            }


            if($lcr!="")

                $c->addInCondition('location', $lcr, 'AND');

            $model = Jobs::model()->findAll($c);  
  • 写回答

2条回答 默认 最新

  • douchuntang2827 2014-07-25 16:00
    关注

    OK it sounds like I found a solution for this problem and its working correctly, here is the solution:

    in the controller:

     foreach($model2 as $m2){
    
                if (strpos($m2->tag, ' ') !== FALSE)
                    $m2->tag = str_replace(" ","_",$m2->tag);
    
                    if(isset($_POST["$m2->tag"])){
    ....
    

    So, as you see in the code, if the tag contain any white space it will be replaced by _ , and in the HTML the id attribute will replace the white space by _ automatically, so they are matched now.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入