duananyantan04633 2014-07-25 06: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 08: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部