我有一个表Tax包含tax_id(1&2),Tax_price(12&8)和Tax_name(vat&Gst)。
我想在文本框('txtvat')中显示增值税价格
SQl Query : SELECT`tax_price` FROM `tbl_taxmaster` WHERE `tax_name` = "VAT"
查看
<div class="row form-group">
<div class="col col-md-4"><label class=" form-control-label">VAT</label>
</div>
<?php foreach($taxvat as $row){ ?>
<div class="col-12 col-md-5">
<input type="text" id="txtVat" name="txtVat" value="<?php echo $row->tax_name;?>" class="form-control">
</div>
<?php } ?>
</div>
控制器
public function displayBillingPage()
{
$this->load->model('CrudModel');
$records['taxvat']=$this->CrudModel->getVatName();
$this->load->view('generatebill',$records);
}
型号
public function getVatName()
{
$this->db->select('tax_price');
$this->db->from('tbl_taxmaster');
$this->db->where('tax_name',"VAT");
$query = $this->db->get();
return $query->result()->row()->tax_price;
}
答案 0 :(得分:0)
您不能同时使用row()
和result()
row()
仅用于获得一行,而result()
仅用于获得一行
如果您使用的是row()
,则可以这样做row()->column_name
。但不能使用result()
如果在select()
中使用列名,则不要这样做row()->column_name
如果仅获得一行,则无需使用foreach
循环
连续
方法1
public function getVatName(){
$this->db->select('tax_price');
$this->db->from('tbl_taxmaster');
$this->db->where('tax_name',"VAT");
return $this->db->get()->row();
}
方法2
public function getVatName(){
return $this->db->get_where('tbl_taxmaster', ['tax_name' => 'VAT'])->row()->tax_price;
}
用于多行
方法1
public function getVatName(){
$this->db->select('tax_price');
$this->db->from('tbl_taxmaster');
$this->db->where('tax_name',"VAT");
return $this->db->get()->result();
}
方法2
public function getVatName(){
return $this->db->get_where('tbl_taxmaster', ['tax_name' => 'VAT'])->result();
}
答案 1 :(得分:0)
视图:
<div class="row form-group">
<div class="col col-md-4"><label class=" form-control-label">VAT</label></div>
<div class="col-12 col-md-5">
<input type="text" id="txtVat" name="txtVat" value="<?php echo $taxvat;?>" class="form-control">
</div>
</div>
型号:
public function getVatName()
{
$this->db->select('tax_price');
$this->db->from('tbl_taxmaster');
$this->db->where('tax_name',"VAT");
$query = $this->db->get();
return $query->row('tax_price');
}
答案 2 :(得分:0)
**View**
<div class="row form-group">
<div class="col col-md-4"><label class=" form-control-label">VAT</label></div>
<?php foreach($taxvat as $row){ ?>
<div class="col-12 col-md-5">
<input type="text" id="txtVat" name="txtVat" value="<?php echo $row->tax_price;?>" class="form-control">
</div>
<?php } ?>
</div>
**Controller**
public function displayBillingPage()
{
$this->load->model('CrudModel');
$records['taxvat']=$this->CrudModel->getVatName();
$this->load->view('generatebill',$records);
}
**Model**
public function getVatName()
{
$this->db->select('tax_price');
$this->db->from('tbl_taxmaster');
$this->db->where('tax_name',"vat");
$query = $this->db->get();
return $query->result();
}