2017-08-24 2 views
1

Wie kann ich eine foreach mit einer anderen foreach in der Abfrage WHERE. Ich habe im zweiten Foreach den Inhalt mit der ID der ersten ID gespeichert und möchte dies in der jeweiligen Spalte anzeigen. So wenn in der zweiten Spalte db_buy.tb_buy_shop die ID 2 hat, sollte dies in der HTML-Spalte zeigen, wo die db_shop.tb_shop_id 2.foreach Verbindung foreach und wählen SUM

Ansicht:

<?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; 
?> 

Modell:

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(); 
} 

Controller:

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'); 
} 

Excpected enter image description here

+0

Willkommen bei StackOverflow. Ich empfehle Ihnen, [Wie Sie eine Frage stellen] (https://stackoverflow.com/help/how-to-ask) zu lesen, damit Sie die Hilfe erhalten, die Sie benötigen. – ryantxr

Antwort

0

Wenn ich richtig bin in dem, was Sie fragen, scheint es, dass Sie nur wissen wollen, wie man den Code formatiert, damit er die Tabelle bildet. So würde es gehen.

<?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; 
?> 
0

Leider nicht. Die Summe der zweiten foreach ist nicht korrekt. Bei ID 3 aus dem Shop ist nur 10.00 raus, aber es kommt 102.31 enter image description here