2012-04-28 66 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