【问题标题】:How to pass zero if input text is blank?如果输入文本为空白,如何传递零?
【发布时间】:2016-12-09 07:25:40
【问题描述】:

我的计算有点问题,当我传递折扣和借方值时,它正在完成它的工作,但是当折扣和借方的值什么都不是时,它返回一个空白页。这是我的模型.. 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;
}

【问题讨论】:

    标签: codeigniter codeigniter-2 codeigniter-3


    【解决方案1】:

    最好的方法是使用三元运算符。

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

    如果你的 php 版本是 7.0> 然后使用

    $subtotal = $this->input->post('subTotal') ?? 0;
    

    【讨论】:

      【解决方案2】:

      在赋值借方和折扣的值时,在数据数组中使用三元运算符。代码如下:

      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;
      

      }

      【讨论】:

      • 不需要在 CI $this->input->post() 数组中使用 isset。这已经检查了isset
      • 谢谢你们俩。 Hikmat & Razib.. 你的回答很有用,解决了问题
      【解决方案3】:

      三元运算符逻辑是使用(condition) ? (true return value) : (false return value) 语句来缩短if/else 结构的过程。

      因此,如果输入文本为空白,您可以使用三元运算符登录来传递零。

      现在,你的 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')
      );
      

      【讨论】:

      • 非常感谢。你救我。 :)
      猜你喜欢
      • 1970-01-01
      • 2016-03-03
      • 1970-01-01
      • 2018-05-21
      • 2022-11-08
      • 2015-08-19
      • 1970-01-01
      • 2013-06-26
      • 2017-11-11
      相关资源
      最近更新 更多