Home > Mobile >  display data from database to dropdown with join table in codeigniter
display data from database to dropdown with join table in codeigniter

Time:09-01

I'm having difficulty with display data from the db to dropdown with join table. i want to join tb_users.id_user with tb_bonus.id_user

This is what I have tried:

Model (Umum_model.php) :

function getNama(){
        $this->db->select('*');
        $this->db->from('tb_bonus');
        $this->db->join('tb_users', 'tb_users.id_user = tb_bonus.id_user');
        $join_query = $this->db->get();
    }

Controller (Bonus.php) :

function __construct()
    {
        parent::__construct();

        $this->load->library('form_validation');
        $this->load->model('Umum_model', 'umum');

    }

    public function index()
    {
        $data = [
            'view' => 'admin/bonus',
            'active' => 'bonus',
            'sub1' => 'bonus',
        ];

        $namalengkap['namaUser'] = $this->umum->getNama();
        
        $this->load->view('template_admin/index', $data, $namalengkap);
    }

View (bonus.php) :

<select  id="id_user" name="id_user" required>
  <option value="">No Selected</option>
    <?php foreach($namaUser as $nama){ ?>
      <option value=""><?=$nama->nama_lengkap?></option>
    <?php } ?>
</select>

And this is what the result looks like :

The data is not displayed

CodePudding user response:

$namalengkap = $this->umum->getNama();

In your code you the controller don't pass the variabile $namaUser to view.

<select  id="id_user" name="id_user" required>
  <option value="">No Selected</option>
    <?php foreach($namalengkap as $nama){ ?>
      <option value=""><?=$nama->{{key_of_col}}?></option>
    <?php } ?>
</select>

CodePudding user response:

change your function index() to this:

    public function index()
    {
        $data = [
            'view' => 'admin/bonus',
            'active' => 'bonus',
            'sub1' => 'bonus',
        ];

        $data['namaUser'] = $this->umum->getNama();
        
        $this->load->view('template_admin/index', $data);
    }

The third parameters of $this->load->view() is boolean (TRUE/FALSE), don't pass the data to it. The only way passing data to view is second parameter.

https://codeigniter.com/userguide3/general/views.html

The other file (Model and View) is fine.

  • Related