2016-11-05 54 views
0

任何人都可以提供幫助。我想盡量減少我的髒代碼,我在邏輯上很弱。預先感謝您..如何最小化此PHP代碼

//These are all fields 
$prodname = $this->input->post('prodname'); 
$price = $this->input->post('price'); 
$qty = $this->input->post('qty'); 
$desc = $this->input->post('desc'); 
$status= $this->input->post('status'); 


// This part I want to minimize, or any shorthand for this code?? 
if ($prodname != NULL && $price != NULL && $qty != NULL && $desc != NULL && $status != NULL) 
    $datas = $this->controller->add_product($data); 
+1

您的問題張貼在http://codereview.stackexchange.com/ – sTg

+0

如果這是來自FORM。然後,是的,使用codeigniters form_validation,你會實現,再加上一些驗證,以確保從表單提供的數據是你所追求的。 – TimBrownlaw

回答

1

是的,你可以做到以下幾點:

//These are the fields coming from form "username" is the name of field 

$this->form_validation->set_rules('username', 'Username', 'required'); 
$this->form_validation->set_rules('password', 'Password', 'required'); 
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required'); 

添加要添加儘可能多的規則,那麼你只是想確認它是否有效或不使用此:

if ($this->form_validation->run() == FALSE) 
{ 
    $this->load->view('myform'); 
} 
else 
{ 
    $this->load->view('formsuccess'); 
} 

參考:https://www.codeigniter.com/userguide3/libraries/form_validation.html

+0

感謝Bro @Habib ........現在正在工作, – Marky