【问题标题】:pass true or false from model to controller将真或假从模型传递到控制器
【发布时间】:2015-04-18 06:44:34
【问题描述】:

我需要在我的控制器中使用条件语句,我应该从模型中传递它。

型号

function checking() {

   ....
   ....
   ....

   foreach ($query->result() as $row)
     if (one field is not empty) {
          $statement == true
     }
     else {
          $statement == false
     }

}

控制器

   $data['records'] = $this->the_model->checking();       
   ...
   if($statement == true) {
       something will be generate here;      
   }     

   else if ($statement == false) {
       something else be generate here;      
   }     
   else {
       nothing;      
   }

有一件事是我不想在循环中将它作为数组传递,因为我从我的 sql 查询中得到的结果是在循环中。如果是falsetrue,我只想获取SIGNLE 信息。它可能看起来像 cookie 或会话(不可能从模型中获取)或其他什么?

提前致谢。

【问题讨论】:

    标签: php codeigniter model controller


    【解决方案1】:

    在模型中

    function checking() {
    
       ....
       ....
       ....
    
       foreach ($query->result() as $row)
         if (one field is not empty) {
              $statement == true
         }
         else {
              $statement == false
         }
    
       //return the value of statement
       return $statement;
    
    }
    

    在控制器中

    $statement = $this->the_model->checking();
    
    if($statement == true) {
           something will be generate here;      
       }     
    
       else if ($statement == false) {
           something else be generate here;      
       }     
       else {
           nothing;      
       }
    

    【讨论】:

      【解决方案2】:

      在模型中创建一个返回布尔值的函数...例如

       public function getBooleanValue(){
          //based on your checks return either true or false;
       }
      

      在控制器端:

       $statement= $this->the_model->getBooleanValue();
      

      在代码中将此 $statement 用作布尔值

      【讨论】:

      • 你能告诉我如何在我的function checking()中滑动它(getBooleanValue()
      • @iyal 只需在关闭函数之前返回 $statement } 并更改 $data['records'] = $this->the_model->checking();到 $statement =$this->the_model->checking();
      • 我还是不太清楚你在说什么,请你把它完整地画在答案框里好吗..
      猜你喜欢
      • 2012-08-01
      • 2013-11-20
      • 2019-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多