2012-04-28 6 views
0

このコードサンプルを変更して、これらの条件が同時に評価されるようにしたいと考えています。複数の条件が真である場合、複数の引数を 'signup'関数に渡すことができます。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

+0

Thx。これは良いアプローチのように見えます。 func_get_args()を使用する。 signup関数では、errors配列が渡されると、個々の引数を読み込む各項目が単一の配列として読み込まれます。 – jsuissa

+0

これについては、ドキュメントをチェックするか、単にテストしてください(例:var_dump())。 signup()には、パラメータを指定することもできます。そこにfunc_get_args()を使用するのは不思議そうです。 – Zombaya

0

& &オペレータ

を使用= 1。 b = 2; if(a == 1 & & b == 2){ echo "これはページに表示されます"; }

+0

結果が条件ごとに異なることを除いて、これは機能します。また、すべてが真実である必要はありません。 – jsuissa

+0

Gotcha - 小さな変更に気付かなかった – Webnet

関連する問題