我正在使用codeigniter.I将税务详细信息存储在会话值中,以存储税是包含税还是独占税。我需要使用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 :(得分:1)
如果您的参数始终为inclusive
/ exclusive
,请使用开关:
switch (strtolower($service_detail['service_tax']))
{
case 'inclusive':
//do your stuff;
break;
case 'exclusive':
//do your stuff;
break;
}
答案 1 :(得分:0)
在视图中,$ data数组的键被转换为变量,因此请尝试var_dump($somekey)
。这是通过extract完成的。
您还可以将strstr
更改为strcmp
甚至===
(因为您不关心哪个字符串更大)。
if(strtolower($service_detail['service_tax']) === "inclusive")
或
if(strcmp(strtolower($service_detail['service_tax']), "inclusive"))