Home > Software design >  Multiple Foreach Loop Codeigniter
Multiple Foreach Loop Codeigniter

Time:09-25

I want to display data in different sections, but error when I run.

enter image description here

Controller

class Dfkamar extends CI_Controller {
public function index() {
    $data['tamu'] = $this ->m_tamu->daftar_kamar()->result();
        $this->load->view('templates/header');
        $this->load->view('templates/sidebar');
        $this->load->view('dfkamar', $data);
        $this->load->view('templates/footer');
}

View

    <?php
      foreach ($tamu as $tamu) : 
    ?>

        <button type="button" class="btn btn-success"><?php echo $tamu->gateA ?></button>
      </div>  
      <?php endforeach;?>

    <?php
        foreach ($tamu as $tamu1) : 
      ?>

        <button type="button" class="btn btn-success"><?php echo $tamu1->gateB ?></button>
      </div>  
      <?php endforeach;?>

CodePudding user response:

$tamu array variable get replaced $tamu string in first foreach so dont't do that

working code is as below:

$tamu = ['gateA'=>'test-A','gateB'=>'sdfs'];

      foreach ($tamu as $tamu_item) : 
        echo $tamu_item->gateA;
     endforeach;
     
     var_dump($tamu);

    foreach ($tamu as $tamu1) : 
        echo $tamu1->gateB;
     endforeach;
  • Related