如何最小化此PHP代码

时间:2016-11-05 08:29:33

标签: codeigniter-3

任何人都可以提供帮助。我想尽量减少我的脏代码,我的逻辑很弱。提前谢谢..

//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 个答案:

答案 0 :(得分: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