2016-12-09 6 views
0

割引とデビットの値を渡したときに私の計算に少し問題がありますが、割引とデビットの値が何もないときは空白のページが返されます。私のモデルはここにあります。CODEIGNITER。入力テキストが空白の場合にゼロを渡す方法は?

function createInvoice() { 
    $this->load->helper('date'); 
    $date = date('Y-m-d H:i:s'); 
    $data = array(
     'Date'    => $date, 
     'Terms_Of_Payment' => $this->input->post('termsOfPayment'), 
     'Sub_Total'   => $this->input->post('subTotal'), 
     'Total'    => $this->input->post('total') - $this->input->post('discount'), 
     'Discount'   => $this->input->post('discount'), 
     'Debit'    => $this->input->post('debit'), 
     'Payment_Cridet' => $this->input->post('total') - $this->input->post('debit') - $this->input->post('discount'), 
     'Note'    => $this->input->post('note'), 
     'Customer_ID'  => $this->input->post('customerId'), 
     'User_ID'   => $this->session->userdata('id')); 

    $this->db->insert('invoice', $data); 
    return ($this->db->affected_rows() != 1) ? false : true; 
} 

答えて

1

三項演算子論理は(condition) ? (true return value) : (false return value)を使用するプロセスでありますあなたのif/else構造を短縮するためのステートメント。

したがって、入力テキストが空白の場合は、パス0に3者演算子ログインを使用できます。

さて、私のようなあなたのcreateInvoice()機能の何らかの変化:

$subTotal  = $this->input->post('subTotal') == "" ? 0 : $this->input->post('subTotal'); 
$total   = $this->input->post('total') == "" ? 0 : $this->input->post('total'); 
$discount  = $this->input->post('discount') == "" ? 0 : $this->input->post('discount'); 
$debit   = $this->input->post('debit') == "" ? 0 : $this->input->post('debit'); 

$data = array(
    'Date'    => $date, 
    'Terms_Of_Payment' => $this->input->post('termsOfPayment'), 
    'Sub_Total'   => $subTotal, 
    'Total'    => ($total - $discount), 
    'Discount'   => $discount, 
    'Debit'    => $debit, 
    'Payment_Cridet' => $total - $debit - $discount, 
    'Note'    => $this->input->post('note'), 
    'Customer_ID'  => $this->input->post('customerId'), 
    'User_ID'   => $this->session->userdata('id') 
); 
+0

ありがとうございました。 私を救ってください。 :) –

+0

あなたの答えが受け入れられました。 :) –

1

データアレイで三項演算子を使用借方の値をassingingとdiscount.Codeは以下のようにのように見えている。

function createInvoice() { 
$this->load->helper('date'); 
$date = date('Y-m-d H:i:s'); 
$data = array(
    'Date'    => $date, 
    'Terms_Of_Payment' => $this->input->post('termsOfPayment'), 
    'Sub_Total'   => $this->input->post('subTotal'), 
    'Total'    => $this->input->post('total') - isset($this->input->post('discount'))?$this->input->post('discount'):0, 
    'Discount'   => isset($this->input->post('discount'))?$this->input->post('discount'):0', 
    'Debit'    => isset($this->input->post('debit'))?$this->input->post('debit'):0, 
    'Payment_Cridet' => $this->input->post('total') - isset($this->input->post('debit'))?$this->input->post('debit'):0 - isset($this->input->post('discount'))?$this->input->post('discount'):0', 
    'Note'    => $this->input->post('note'), 
    'Customer_ID'  => $this->input->post('customerId'), 
    'User_ID'   => $this->session->userdata('id')); 

$this->db->insert('invoice', $data); 
return ($this->db->affected_rows() != 1) ? false : true; 

}

+1

必要がCIで '' isset'ます$ this->入力 - >ポスト() '配列を使用します。これはすでに 'isset'とチェックされています –

+1

お互いのおかげで。 Hikmat&Razib ..あなたの答えは役に立ち、問題を解決しました –

関連する問題