Skip to content
Advertisement

Multiple Foreach Loop Codeigniter

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

image

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

Advertisement

Answer

$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;
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement