2012-04-28 45 views
0

我想更改此代码示例,以便同时评估所有这些条件。如果多个条件为真,则可以将多个参数传递给'注册'功能。使用PHP一次评估多个条件

我最初尝试使用开关,但每个条件都需要能够评估不同的条件。

任何帮助指向我在这个正确的方向将是伟大的。

if ($this->form_validation->run() == FALSE) { 
     $this->signup(); 

    } else if ($this->membership_model->check_field('username',$this->input->post('username'))) { 

     $this->signup("An account with the username you've entered already exists."); 

    } else if ($this->membership_model->check_field('email_address',$this->input->post('email_address'))) { 

     $this->signup("An account with the e-mail you've entered already exists."); 

    } 

回答

2

您可以将所有错误/错误条件放入数组中,并将其传递到$this->signup()

if ($this->form_validation->run() == FALSE) { 
    $this->signup(); 
} 
else 
{ 
    $errors = array(); 
    if ($this->membership_model->check_field('username',$this->input->post('username'))) 
     $errors[]= "An account with the username you've entered already exists."; 
    if ($this->membership_model->check_field('email_address',$this->input->post('email_address'))) 
     $errors[]= "An account with the e-mail you've entered already exists."; 

    $this->signup($errors); 
} 
+0

你也可以查看[设置表单验证错误](http://codeigniter.com/user_guide/libraries/form_validation.html#settingerrors)。 – Zombaya 2012-04-28 23:22:29

+0

Thx。这看起来很不错。使用func_get_args();在注册函数中,当传递errors数组时,每个项目都会读取单个参数或单个数组。 – jsuissa 2012-04-28 23:24:30

+0

你应该检查文档或者只是测试它(例如用var_dump())。你也可以给signup()一个参数。似乎很奇怪在那里使用func_get_args()。 – Zombaya 2012-04-28 23:28:21

0

使用& &操作者

一个= 1; b = 2; if(a == & & b == 2){ echo“this will appear up on the page”; }

+0

这将工作,除了结果是不同的每个条件。他们并不都必须是真实的。 – jsuissa 2012-04-28 23:18:24

+0

Gotcha - 我没有注意到这些小改动 – Webnet 2012-04-28 23:27:00