douan3182 2012-08-02 19:13
浏览 14
已采纳

将值从一种方法返回到另一种方法

I don't know why this don't work at all. I maybe wrong with my understanding that is why.

here is the situation.

  1. MVC pattern
  2. form validation stuffs

Here are the codes

public function userExist($data) 
{
    $string = "SELECT student_number FROM users WHERE student_number = :user";
    $sth = $this->db->prepare($string);
    $sth->execute(array(
        ':user' => $data['user']
    ));
    return $sth->rowCount() == 0 ? true : false;
}

public function validate($data) {
    $this->userExist($data);
}

What i want is to return a string, that says "user exists", if the userExist method is false ... But this code doesn't work:

if($sth->rowCount() == 0) {
    return true;
} else {
    return "User Already Exists";
}

This is, how i call them in the controller:

if ($this->model->validate($data) == true) {
    $this->model->create($data);
    header('Location: '.URL.'users');
} else {
    echo $this->model->validate($data);
    die();
}

What do you think is the best solution?

  • 写回答

1条回答 默认 最新

  • douzhanlie9209 2012-08-03 04:49
    关注

    First of all, you need to return the value of validate:

    public function validate($data) {
        $this->userExist($data);
    }
    

    But there are some other problems here. You don't need to call $this->model->validate($data) twice in your controller. You could do something like:

    $result = false;
    $result = $this->model->validate($data);
    if ( true === $result {
        $this->model->create($data);
        header('Location: '.URL.'users');
    } else {
        die($result);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号