【问题标题】:if else statement is not working on codeigniterif else 语句不适用于 codeigniter
【发布时间】:2013-07-11 06:08:05
【问题描述】:

我很少有 if else 语句只返回真或假,而且每个语句都不能正常工作。我尝试了很多方法,但不知道问题所在。如果有人解决这个问题,将会非常有帮助。

这是我的模型代码

function exists($email){
    $this->db->where('email',$email);
    $query=$this->db->get('member_info');
    echo "model is called"; // after execution this text is shown but not the others
    if ($query->num_rows == 1) {           
        return true;        
        echo "i got one";
    }else{
        return false;
        echo "i got nothing";
    }       
}

这是我的控制器

function is_exists($email){
    echo "this is loaded";  //this line works properly
    if($this->validation_model->exists($email)){
        return false;
        echo "true"; //this doesn't 
    }else{
        return true;
        echo "false"; // this doesn't
    }
}

【问题讨论】:

  • $query->num_rows() 试试这个
  • return 之后的任何内容都不会运行。 return 退出函数。

标签: php codeigniter if-statement


【解决方案1】:

您在打印回显部分之前返回函数。你应该在返回之前回显。

还要换行检查多个

if ($query->num_rows() > 0 ) {

更新

试试这个方法。相应地替换表名、id 值。

$query = $this->db->query("select id form your_table where email=".$email);
if ($query->num_rows() > 0 ){
echo "i got one";
return true;
}
else{
echo "i got nothing";
return false;
}

还请查看您的控制器逻辑,当存在电子邮件时它会返回 false。最好改控制器的真假返回。

【讨论】:

  • +1 这是有道理的。如果你也写一些代码会很有用。
  • i 该模型的结果总是正确的(当输入的电子邮件是全新的时,它会在数据库中收到一封电子邮件)。这背后的原因是什么?
【解决方案2】:

试试看

if($this->validation_model->exists($email)){
    echo "true";
    return false; 
}else{
    echo "false";
    return true;
}  

将echo放在return之前,应该是这样的

$query->num_rows()

【讨论】:

    【解决方案3】:

    因为你使用的是return,所以return之后的代码不会执行

    return false;
    echo "true"; // this doesn't because you have return before this line
    
    return true;
    echo "false"; // this doesn't because you have return before this line
    

    【讨论】:

      【解决方案4】:

      改变这一行:

      if ($query->num_rows() == 1) {    
      

      【讨论】:

        【解决方案5】:
        //num_rows() is a function
        <?php 
        
        function exists($email){
            $this->db->where('email',$email);
            $query=$this->db->get('member_info');
            echo "model is called"; // after execution this text is shown but not the others
        
            //num_rows() is a function
            if ($query->num_rows() == 1) {
        
                //add message before return statement
                echo "i got one";
                return true;
        
        
            }else{
        
                echo "i got nothing";
                return false;
        
            }
        }
        

        【讨论】:

          【解决方案6】:

          你的模型应该是:

          if ($query->num_rows == 1) {           
              return true;        
          }else{
              return false;
          }
          

          无需打印额外的回声。与您的控制器相同的故事

          if($this->validation_model->exists($email)){
              return false;
          }else{
              return true;
          }
          

          据我所知,您无法在 return 语句之后执行任何代码(在函数返回中)。

          我的解决方案是:

          在你的控制器中这样写

          if($this->validation_model->exists($email)){
              echo "EMAIL EXIST";
          }else{
              echo "EMAIL DOES NOT EXIST";
          }
          

          【讨论】:

            【解决方案7】:

            改变这个:

            if ($query->num_rows == 1) { 
            

            到这里:

            if ($query->num_rows() == 1) { 
            

            并更改以下内容:

            if ($query->num_rows() > 0 ){
                echo "i got one";
                return true;
            }
            else{
                echo "i got nothing";
                return false;
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-08-26
              • 1970-01-01
              • 2022-01-25
              • 2013-12-25
              • 2021-12-18
              相关资源
              最近更新 更多