2015-07-10 57 views
1

我正在使用codeigniter.I將稅務明細存儲在會話值中以存儲稅收是包含性的還是排他性的。我需要使用strstr()來檢查字符串值並進行一些計算。我做了計算波紋管我的控制器的需要更多關於strstr()的知識它不適用於我

<?php 
    $tax_percentage=$tax['company_tax13']; 
    if(strstr($service_detail['service_tax'],"inclusive")==true) { 
    $taxamount=$service_detail['service_price']-($service_detail['service_price']/(1+($tax_percentage/100))); 
    $grand=$service_detail['service_price']; 
    } 
    else { 
    $taxamount=($service_detail['service_price']*(1+($tax_percentage/100)))-$service_detail['service_price']; 
    $grand=$taxamount+$service_detail['service_price']; 
    } 
    ?> 

響應,當我運行的代碼是

Array ([service_detail] => Array ([service_price] => 1000 [service_tax] =>"inclusive") [tax] => Array ([0] => Array ([company_tax13] => 14))) 

我怎麼能對這個代碼。代碼的任何問題? 我的控制器文件。在這裏,我有一個存儲service_detail['service_tax']

public function add() 
    { 
     //if save button was clicked, get the data sent via post 
     if ($this->input->server('REQUEST_METHOD') === 'POST') 
     { 
$this->load->library('session'); 
      $this->session->unset_userdata('service_detail'); 
      //if the form has passed through the validation 
      if ($this->form_validation->run()) 
      { 
       $data_to_store = array(
       'id' => $this->input->post('id'), 
        'service_name' => $this->input->post('service_name'), 
        'category' => $this->input->post('category'), 
        'service_tax' => $this->input->post('service_tax'), 
        'service_length' => $this->input->post('service_length'),   
        'service_price' => $this->input->post('service_price') 
       ); 
       //if the insert has returned true then we show the flash message 
       if($this->service_model->store_service($data_to_store)){ 
        $data['flash_message'] = TRUE; 
        $this->session->set_flashdata('flash_message', 'updated'); 
        $this->session->set_userdata('service_detail', ['service_price'=>$this->input->post('service_price'),'service_tax'=>$this->input->post('service_tax')]); 
        redirect(base_url().'admin/service/view'); 
       }else{ 
        $data['flash_message'] = FALSE; 
       } 

      } 

     } 
     $data['category'] = $this->category_model->get_category(); 
     $data['main_content'] = 'admin/service/add'; 
     $this->load->view('includes/template', $data); 
    }  
+0

爲什麼你使用'的strstr()'? – slash197

+0

來比較我收到的字符串。是否有其他方法? – Anu

+0

嘗試使用'if(strpos($ service_detail ['service_tax'],「inclusive」)> -1)' –

回答

1

會話變量如果你的參數始終是inclusive/exclusive使用一個開關:

switch (strtolower($service_detail['service_tax'])) 
{ 
    case 'inclusive': 
     //do your stuff; 
     break; 

    case 'exclusive': 
     //do your stuff; 
     break; 
} 
+0

<?php \t \t $ tax_percentage = $稅[0] [ 'company_tax13']; \t \t \t \t開關(用strtolower($ service_detail [ 'service_tax'])) \t \t \t { \t \t \t情況下 '包括': \t \t \t $ TAXAMOUNT = $ service_detail [ 'service_price'] - ($ service_detail [ 'service_price'] /(1 +($ TAX_PERCENTAGE/100))); \t \t \t \t \t $ grand = $ service_detail ['service_price']; \t \t \t break; \t \t \t案 '獨家': \t \t \t \t $ TAXAMOUNT =($ service_detail [ 'service_price'] *(1 +($ TAX_PERCENTAGE/100))) - $ service_detail [ 'service_price']; \t \t \t \t \t $ grand = $ taxamount + $ service_detail ['service_price']; \t \t \t \t break; \t \t \t} \t \t?> – Anu

+0

可以這樣做嗎?但我無法得到我的預期輸出 – Anu

0

在視圖中的$ data數組的鍵轉換成變量,所以儘量var_dump($somekey)。這是通過extract完成的。

您還可以將strstr更改爲strcmp甚至===(因爲您不關心哪個字符串更大)。

if(strtolower($service_detail['service_tax']) === "inclusive") 

if(strcmp(strtolower($service_detail['service_tax']), "inclusive")) 
+0

我正在使用這兩個代碼段得到一個空白屏幕。 – Anu

+0

獲取數組內的值有任何問題? – Anu

+0

是的,這是一個抓取問題。我只是用正常的變量測試並且工作正常。讓我改進我的答案。 – antonioduarte

相關問題