如何在查詢WHERE中使用另一個foreach連接foreach。 我已經在第二個Foreach中存儲了帶有第一個ID的ID的內容,並且希望在相應的列中顯示它。 因此,如果在第二列db_buy.tb_buy_shop具有ID 2,本應顯示在HTML的列,其中db_shop.tb_shop_id 2.foreach連接foreach並選擇SUM
視圖:
<?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');
}
歡迎來到StackOverflow。我建議你閱讀[如何提出問題](https://stackoverflow.com/help/how-to-ask),以便獲得所需的幫助。 – ryantxr