Php codeigniter按产品分组,在单独的分支上储存
在codeigniter中,我在数据库中有以下内容:Php codeigniter按产品分组,在单独的分支上储存,php,mysql,arrays,codeigniter,Php,Mysql,Arrays,Codeigniter,在codeigniter中,我在数据库中有以下内容: 产品标识产品名称库存分公司 1.5纽约 1鞋10洛杉矶 1号鞋5号底特律 2件衬衫10件,纽约 2件衬衫5件底特律 3.7底特律 我想要实现的是这个表(视图中): 产品标识产品名称库存纽约洛杉矶底特律 1鞋20 5 10 5 2件衬衫15 10-5 3裙子7--7 如何在Codeigniter 3中编码?首先触发此查询 SELECT product_id, product_name, SUM(stock), GROUP_CONCAT(b
产品标识产品名称库存分公司
1.5纽约
1鞋10洛杉矶
1号鞋5号底特律
2件衬衫10件,纽约
2件衬衫5件底特律
3.7底特律
我想要实现的是这个表(视图中):
产品标识产品名称库存纽约洛杉矶底特律
1鞋20 5 10 5
2件衬衫15 10-5
3裙子7--7
如何在Codeigniter 3中编码?首先触发此查询
SELECT product_id, product_name, SUM(stock),
GROUP_CONCAT(branch,"#@#",stock) branch_stock FROM your_table GROUP BY product_id
然后,您将通过触发此查询来获取数组
foreach($array as $k => $v){
$temp = explode("#@#", $v['branch_stock']);
$array[$k][$temp[0]] = $temp[2];
}
要获取所有列名,可以执行以下操作
$columns = array_keys($array);
我希望这能解决您的问题。在车型中:
function product_stock(){
$this->db->select('*');
$this->db->select('SUM(case when branch = "New York" then stock else NULL end) as branch_ny');
$this->db->select('SUM(case when branch = "Los Angeles" then stock else NULL end) as branch_la');
$this->db->select('SUM(case when branch = "Detroit" then stock else NULL end) as branch_dt');
$this->db->from('product');
$this->db->group_by("product_id");
return $this->db->get()->result_array();
}
鉴于:
<table class="table table-striped table-bordered">
<tr>
<th>Produk Id</th>
<th>Produk Nama</th>
<th>New York</th>
<th>Los Angeles</th>
<th>Detroit</th>
</tr>
<?php foreach($allproduct as $e){ ?>
<tr>
<td><?php echo $e['product_id']; ?></td>
<td><?php echo $e['product_name']; ?></td>
<td><?php echo $e['branch_ny']; ?></td>
<td><?php echo $e['branch_la']; ?></td>
<td><?php echo $e['branch_dt']; ?></td>
</tr>
<?php } ?>
产品标识
产品名称
纽约
洛杉矶
底特律
它适合我它是透视表