English 中文(简体)
代码化:检索记录和总和
原标题:Codeigniter: fetching records and sum

我有下一张表格,我正试图将p_tall 和表中所有其他记录一起显示。我设法展示了总额,但如果我展示了,则仅显示表格中拳头行的数值。尽管总和显示正确为600,但总和却显示为600。

请告诉我下面我代码里的问题在哪里?

提前感谢:)

我的 DB 表格 :

 p_id   p_name    p_quantity    p_rate   p_total       user_id
  1     Pepsi        12           30       360            1
  2     Hot Breads   12           20       240            1

我的模型有以下代码

$this->db->select( *,SUM(temporary_table.p_total AS Total );
$this->db->from( temporary_table );
$this->db->where( user_id ,$user_id);                                   
$getData = $this->db->get(  );

if($getData->num_rows() > 0) {
   return $getData->result_array();
}

else { 
   return null;
}

这是我的控制器:

$this->load->model( mod_sales );
$data[ records ]= $this->mod_sales->add_to_temporary_table($user_id);

我的视图 :

 foreach ($records as $row)
 {   
   <td><?php echo $row[ p_name ];?></td>
   <td><?php echo $row[ p_quantity ];?></td>
   <td><?php echo $row[ p_rate ];?></td>
   <td><?php echo $row[ p_total ];?></td>
 }

<?php echo $row[ Total ];?>
最佳回答

在您的视图中 :

$total_sum=0;

foreach ($records as $row){

 <td><?php echo $row[ p_name ];?></td>
 <td><?php echo $row[ p_quantity ];?></td>
 <td><?php echo $row[ p_rate ];?></td>
 <td><?php echo $row[ p_total ];?></td>

 $total_sum+=$row[ p_total ];
}

<?php echo $total_sum;?>
问题回答

您有一个完整和单独的列表 。

$this->db->select_sum( total_price );
$this->db->from( order );
$this->db->where( dates BETWEEN DATE_ADD(NOW(), INTERVAL -7 DAY) AND NOW()  );
$query=$this->db->get();
$data[ total ]=$query->row()->total_price;

$this->db->select( * );
$this->db->from( order );
$this->db->where( dates BETWEEN DATE_ADD(NOW(), INTERVAL -7 DAY) AND NOW() );
$query=$this->db->get();
$data[ count ]=$query->num_rows();

产出:

 Array ( 

 [total] => 2759 

 [count] => 5 

)

您重新丢失了 GROUP by 条款 。

据我所知,你希望这样:

SELECT *, SUM(p_total) FROM temporary_table WHERE user_id = ... GROUP BY p_id

不熟悉代码编码器, 但猜测你需要这条线 在“ 在哪里” 之后 :

$this->db->group_by( p_id );




相关问题
PHP Framework: Ebay Like Site

I am going to be builiding a site like ebay - with all the features of ebay. Please note my payment method is limited to paypal. What would be the best PHP framework to use to build this quickly, ...

What s the proper MVC way to do this....?

Quick question about general MVC design principle in PHP, using CodeIgniter or Kohana (I m actually using Kohana). I m new to MVC and don t want to get this wrong... so I m wondering if i have ...

Check session from a view in CodeIgniter

What is the best way to check session from a view in CodeIgniter, it shows no way in their user guide, otherwise I will have to make two views on everything, which is kinda weird...still a newbie to ...

Using SimplePie with CodeIgniter and XAMPP

I am using CodeIgniter 1.7.2 with XAMPP 1.7.2 on a Windows computer. I am trying to make use of SimplePie. I followed all the instructions I could find: a copy of simplepie.inc is in my applications/...

CodeIgniter adding semicolons

How do I stop CodeIgniter adding semicolons ; to data sent via POST that contains ampersand &? For example it is converting "a=1&b=2&c=3" into "a=1&b;=2&c;=3". From looking on the forums ...

Best way to make Admin pages in CodeIgniter?

I m working on an app in CodeIgniter, and I want to have admin pages for several of the objects in the application, and I m wondering what would be the better way to put these into an MVC structure. ...

CodeIgniter form verification and class

I m using the form validation library and have something like this in the view <p> <label for="NAME">Name <span class="required">*</span></label> <?...

热门标签