如何在视图页面中获取存储在数据库中的值

时间:2015-07-09 07:30:59

标签: php mysql codeigniter

我正在使用codeigniter。我需要在设置表中获取税值保存并在以下视图页面中使用它。 sample_view.php

<fieldset>
        <div class="form-group">
            <label for="email">Service price: <?=$service_detail['service_price']?> Rs</label>
          </div>

         <div class="form-group">
            <label for="email">Tax Percentage : 14%</label>
          </div>

          <div class="form-group">
          <?php 
          $tax_percentage=14;
          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'];
          }
          ?>
            <label for="email">Tax Amount : <?php echo $taxamount; ?> Rs</label>
          </div>

          <div class="form-group">
            <label for="email">Grand Amount : <?php echo $grand; ?> Rs</label>
          </div>

          <div class="form-actions">
            <a href="<?=base_url()?>admin/service"><button class="btn" type="reset">Back</button></a>
          </div>
        </fieldset>

在此视图页面中,我默认设置了税率。但是我需要获取设置页面中可用的值。我应该如何为此编码。有人可以帮我编码吗?

CREATE TABLE IF NOT EXISTS `settings` (
  `id` int(15) NOT NULL AUTO_INCREMENT,
  `company_name` varchar(30) NOT NULL,
  `company_logo` varchar(50) NOT NULL,
  `service_tax` int(20) NOT NULL,
  `product_tax` int(20) NOT NULL,
  `date_format` int(20) NOT NULL,
  `time_format` int(20) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

这是我的表结构。在这里,我存储了service tax,现在我需要将此服务税纳入上一个查看文件sample_view.php 我有一个带有以下功能的模型文件

public function get_service_price($id)
     { 
     $this->db->select('service_tax');
    $this->db->from('service');
    $this->db->where('id', $id);
    $query = $this->db->get('service');
    return $query->row();  
 }

使用此功能我可以获得service_tax。我需要在视图文件中获取它。如何做到这一点?

2 个答案:

答案 0 :(得分:0)

在控制器中调用模型函数,然后使用$this->load->view('filename',$data);

在视图中传递返回数据

$ data variable是一个数组,包含需要传递给视图的所有数据。

答案 1 :(得分:0)

使用此

    <?php
        if(!empty($service_detail['service_price']))
        {
        //this to check whether `$service_detail['service_price']` data is set or unset. If set it execute below Query of unset it goes to `else` part

        $tax_percentage=14;
        $tax = $tax_percentage/100;//optimize the calculation

        if($service_detail['service_tax']=='inclusive')//not sure about strstr(),
        {
            $taxamount = $service_detail['service_price'] - ($service_detail['service_price'] / (1 + $tax ));
            $grand = $service_detail['service_price'];
            ?>
            <div class="form-group">
                <label for="email">Tax Amount : <?php echo $taxamount; ?> Rs</label>
            </div>

            <div class="form-group">
                <label for="email">Grand Amount : <?php echo $grand; ?> Rs</label>
            </div>
        <?php
        }
        else
        {
        $taxamount = ($service_detail['service_price'] * (1+ $tax)) - $service_detail('service_price');
        $grand = $taxamount+$service_detail['service_price'];
        ?>
        <div class="form-group">
            <label for="email">Tax Amount : <?php echo $taxamount; ?> Rs</label>
        </div>

        <div class="form-group">
            <label for="email">Grand Amount : <?php echo $grand; ?> Rs</label>
        </div>
    <?php
    }

    }
    else
    {

    }

?>

编辑01

在模型中

public function get_tax()
    {
        $query = $this->db->query("SELECT service_tax FROM settings ");
        $result = $query->result_array();
        return $result;
    }

在控制器

$data['tax'] = $this->model_name->get_tax();

$this->load->view('view_name', $data);

在视图中

foreach ($tax as $new_tax)
{
    tax_percentage = $new_tax['service_tax'];
}