user2704611
user2704611

Reputation: 11

Codeigniter Sum contents of the records in a field

I am trying fetching some information from mysql database and I am displaying it using the following script(I am using Codeigniter).

database order_details

order_id | qty  | price
4          2      0.9
4          1      0.9
4          1      0.85

in my view i have

<?php 
	$sum = $sum[0]->price;
	$qty = $qty[0]->qty;
	$total = $sum * $qty;

?>

<div class="container">

	<div class="well">

	<a href="<?php echo base_url('billing/view')?>" class="btn btn-success"><</a>
	<?php foreach($customer as $data):?>
	<a href="<?php echo base_url('print_pdf/reciept_pdf/'.$data->oid)?>" class="btn btn-info">PDF</a>
	<a href="<?php echo base_url('print_pdf/download_pdf/'.$data->oid)?>" class="btn btn-warning">Download PDF</a>

	
		
			<table class="table table-bordered">
			<br>
			<tr>
				<td>Nome cliente:</td>
				<td><?php echo $data->customer_name;?></td>
				<td>NIF:</td>
				<td><?php echo $data->customer_contact;?></td>
				
			</tr>
			<tr>
				<td>Pagamento:</td>
				<td><?php echo $data->payment;?></td>
				<td>Data da Ordem:</td>
				<td><?php echo $data->date;?></td>
			</tr>
		<?php endforeach;	?>


			<tr>
				<td>Unid.</td>
				<td>Item</td>
				<td>Qtd.</td>
				<td>Valor</td>
			</tr>
			<?php 
			foreach($results as $data):?>
			<tr>	
				<td></td>
				<td><?php echo $data->product_name;?></td>
				<td><?php echo $data->qty;?></td>
				<td>€ <?php 
					
				echo number_format($data->qty * $data->price,2);?></td>
			</tr>
			<?php endforeach;	?>

			 <tr>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
			</tr>

			<tr>
				<td></td>
				<td></td>
				<td>Total da Venda (IVA Incl)</td>
				<td>€ <?php echo number_format($total ,2) ;?></td>
				
			

				
				
			</tr>

			<tr>
				
				<td>Valor do IVA</td>
				<td>€ <?php echo number_format($vat = 0 * $total ,2) ;?></td>
				<td>Valor iliquido</td>
				<td>€ <?php echo number_format($vatable =$total - $vat ,2);?></td>
			</tr>

			<tr>
				<td>Tabela IVA</td>
				<td>€ <?php echo number_format($vatable =$total - $vat ,2);?></td>
				<td>Valor iliquido</td>
				<td>€ <?php echo number_format($vatable =$total - $vat ,2);?></td>
			</tr>
			 <tr>
				<td>VAT-Exempt</td>
				<td></td>
				<td>Less:SC/PWD Discount</td>
				<td></td>
			</tr>

			 <tr>
				<td>VAT Zero Rated</td>
				<td></td>
				<td>Amount Due</td>
				<td></td>
			</tr>

			  <tr>
				<td>VAT - 0%</td>
				<td>€ <?php echo number_format($vat = 0 * $total,2) ;?></td>
				<td>Adicionar: IVA</td>
				<td></td>
			</tr>

			<tr>
				<td></td>
				<td></td>
				<td>Total Amout Due</td>
				<td>€ <?php echo number_format($total,2)?></td>
			</tr>

			</table>
			
	</div>

</div>

but the result is 10.60 and not 3.55

the output

in models i have this

public function get_sum($oid)
{

    $this->db->select_sum('price')
             ->where('order_id',$oid);
    $query = $this->db->get('order_details');

    return $query->result();
}

public function get_qty($oid)
{

    $this->db->select_sum('qty')
             ->where('order_id',$oid);
    $query = $this->db->get('order_details');

    return $query->result();

in controllers i have this:

public function view_id($oid)
    {
        if($this->session->userdata('logged_in')){

        $data['qty'] = $this->billings->get_qty($oid);
        $data['sum'] = $this->billings->get_sum($oid);
        $data['results'] = $this->billings->fetch_order_details($oid);
        $data['customer'] = $this->billings->fetch_customer_details($oid);
        $this->load->view('order/order_details',$data);

        }else{
            redirect(base_url(''));
        }   
    }

Any help for this issue?

Upvotes: 0

Views: 151

Answers (1)

user2704611
user2704611

Reputation: 11

ok, is solved by now:

how?

insert in models a new sintax,

public function get_qty($oid)
	{
		
		$this->db->select('sum(price * qty) as qty', FAlSE)
				 ->where('order_id',$oid);
		$query = $this->db->get('order_details');

		return $query->result();
	}

in the view i change $sum = $sum[0]->price; to $sum = 1;

thanks for all comunity

Upvotes: 1

Related Questions