foreach 连接 foreach 和 select SUM
foreach connection foreach and select SUM
如何在查询 WHERE 中加入一个 foreach 和另一个 foreach。
我已经在第二个 Foreach 中存储了具有第一个 ID 的 ID 的内容,并希望在相应的列中显示它。
因此,如果在第二列中 db_buy.tb_buy_shop 的 ID 为 2,则应显示在 HTML 列中,其中 db_shop.tb_shop_id 2.
查看:
<?php
foreach ($shops_where_1 as $shop_where_1):
foreach ($buy_sums as $buy_sum):
if($buy_sum['tb_buy_shop']==$shop_where_1['tb_shop_id']) {
$gesamtsumme = $buy_sum['gesamtsumme'];
}
endforeach;
?>
<tr>
<td><?php echo $shop_where_1['tb_shop_name']; ?></td>
<td></td>
<td><div class="input-group"><div class="input-group-addon">€</div><input type="text" class="form-control" value="<?php echo number_format($buy_sum['gesamtsumme'],2,",",".");?>" disabled></div></td>
<td><div class="input-group"><div class="input-group-addon">€</div><input type="text" class="form-control" value="<?php echo number_format($gesamtsumme,2,",",".");?>" disabled></div></td>
<td></td>
<td></td>
</tr>
<?php
// endforeach;
endforeach;
?>
型号:
public function shops_where_1()
{
$this->db->select('*');
$this->db->from('db_shop');
$this->db->where('db_shop.tb_shop_buy = 1');
$this->db->order_by('tb_shop_name');
$query = $this->db->get();
return $query->result_array();
}
public function buy_sums()
{
$this->db->select('(SELECT SUM(db_buy.tb_buy_gesamt) FROM db_buy) AS gesamtsumme');
$this->db->select('(SELECT SUM(db_buy.tb_buy_abbezahlt) FROM db_buy) AS abbezahlt', FALSE);
$this->db->select('tb_buy_shop');
$this->db->from('db_buy');
$query = $this->db->get();
return $query->result_array();
}
控制器:
public function buy_shop($slug)
{
// if (!$this->session->userdata('logged_in'))
// {
// redirect('users/login');
// }
$data['get_shops'] = $this->Admin_model->get_shops($slug);
$data['shops_where_1'] = $this->Admin_model->shops_where_1();
$this->load->view('templates/header_acp');
$this->load->view('admin/buy_shop', $data);
$this->load->view('templates/footer');
}
如果我对你的要求是正确的,那么你似乎只是想知道如何格式化代码以便构建 table。这就是如何做到这一点。
<?php
foreach ($shops_where_1 as $shop_where_1):
?> <tr>
<td><?php echo $shop_where_1['tb_shop_name']; ?></td>
<?php
foreach ($buy_sums as $buy_sum):
if($buy_sum['tb_buy_shop']==$shop_where_1['tb_shop_id']) {
?>
<td><div class="input-group"><div class="input-group-addon">€</div><input type="text" class="form-control" value="<?php echo number_format($buy_sum['gesamtsumme'],2,",",".");?>" readonly></div></td>
<?php
}
endforeach; // endforeach 2
</tr>
endforeach; // endforeach 1;
?>
如何在查询 WHERE 中加入一个 foreach 和另一个 foreach。 我已经在第二个 Foreach 中存储了具有第一个 ID 的 ID 的内容,并希望在相应的列中显示它。 因此,如果在第二列中 db_buy.tb_buy_shop 的 ID 为 2,则应显示在 HTML 列中,其中 db_shop.tb_shop_id 2.
查看:
<?php
foreach ($shops_where_1 as $shop_where_1):
foreach ($buy_sums as $buy_sum):
if($buy_sum['tb_buy_shop']==$shop_where_1['tb_shop_id']) {
$gesamtsumme = $buy_sum['gesamtsumme'];
}
endforeach;
?>
<tr>
<td><?php echo $shop_where_1['tb_shop_name']; ?></td>
<td></td>
<td><div class="input-group"><div class="input-group-addon">€</div><input type="text" class="form-control" value="<?php echo number_format($buy_sum['gesamtsumme'],2,",",".");?>" disabled></div></td>
<td><div class="input-group"><div class="input-group-addon">€</div><input type="text" class="form-control" value="<?php echo number_format($gesamtsumme,2,",",".");?>" disabled></div></td>
<td></td>
<td></td>
</tr>
<?php
// endforeach;
endforeach;
?>
型号:
public function shops_where_1()
{
$this->db->select('*');
$this->db->from('db_shop');
$this->db->where('db_shop.tb_shop_buy = 1');
$this->db->order_by('tb_shop_name');
$query = $this->db->get();
return $query->result_array();
}
public function buy_sums()
{
$this->db->select('(SELECT SUM(db_buy.tb_buy_gesamt) FROM db_buy) AS gesamtsumme');
$this->db->select('(SELECT SUM(db_buy.tb_buy_abbezahlt) FROM db_buy) AS abbezahlt', FALSE);
$this->db->select('tb_buy_shop');
$this->db->from('db_buy');
$query = $this->db->get();
return $query->result_array();
}
控制器:
public function buy_shop($slug)
{
// if (!$this->session->userdata('logged_in'))
// {
// redirect('users/login');
// }
$data['get_shops'] = $this->Admin_model->get_shops($slug);
$data['shops_where_1'] = $this->Admin_model->shops_where_1();
$this->load->view('templates/header_acp');
$this->load->view('admin/buy_shop', $data);
$this->load->view('templates/footer');
}
如果我对你的要求是正确的,那么你似乎只是想知道如何格式化代码以便构建 table。这就是如何做到这一点。
<?php
foreach ($shops_where_1 as $shop_where_1):
?> <tr>
<td><?php echo $shop_where_1['tb_shop_name']; ?></td>
<?php
foreach ($buy_sums as $buy_sum):
if($buy_sum['tb_buy_shop']==$shop_where_1['tb_shop_id']) {
?>
<td><div class="input-group"><div class="input-group-addon">€</div><input type="text" class="form-control" value="<?php echo number_format($buy_sum['gesamtsumme'],2,",",".");?>" readonly></div></td>
<?php
}
endforeach; // endforeach 2
</tr>
endforeach; // endforeach 1;
?>