Bram Wiratma
Bram Wiratma

Reputation: 75

Display data from database with Select Option Codeigniter

first I want to apologize because I just learned Codeigniter, I have problems to display data from the database by using the Select Option, there is no error but the data does not appear, for your information, i have joined 3 tables.

Here's my Controller

class Harga extends CI_Controller{

function __construct(){
parent::__construct();
  $this->load->model('m_harga');
  $this->load->helper('url');
  $this->load->database();
}

function index(){
  $this->load->helper('form');
  $data['tabel_harga'] = $this->m_harga->tampil_data();
  $this->load->view('v_harga',$data);
}

Here's my Model

class M_harga extends CI_Model{
 function tampil_data(){
    $this->db->order_by('id_harga','ASC');
    return $this->db->from('tabel_harga')
    ->join('tabel_vendor','tabel_vendor.id_vendor=tabel_harga.id_vendor')
    ->join('tabel_hari','tabel_hari.id_hari=tabel_harga.id_hari')
    ->get()
    ->result();
}

and Here's my Views

<select class="form-control">
    <option value="">All</option>
      <?php
       foreach($tabel_harga as $u)
       {
        echo '<option value="'.$u['id_vendor'].'">'.$u['nama_vendor'].'</option>';
       }
      ?>
</select>

I will be very grateful if you help me, thank you guys.

Upvotes: 0

Views: 2252

Answers (3)

Venus Marks
Venus Marks

Reputation: 1

db->select("name, value"); 
$this->db->from('settings'); 
$query = $this->db->get(); 
if ($query->num_rows()) 
{ 
  foreach ($query->result_array() as $row) 
  { // Your data is coming from multiple rows, so need to loop on it. 
      $siteData[$row['name']] = $row['value']; 
   } 
   } 
  return $siteData; 
}

Upvotes: 0

dmforaname
dmforaname

Reputation: 63

try this

view

<select class="form-control">
    <option value="">All</option>
      <?php
       foreach($tabel_harga as $u)
       {
        echo '<option value="'.$u->id_vendor.'">'.$u->nama_vendor.'</option>';
       }
      ?>
</select> 

Models


class M_harga extends CI_Model{
 function tampil_data(){

     $this->db-join('tabel_vendor','tabel_vendor.id_vendor=tabel_harga.id_vendor')
     $this->db-join('tabel_hari','tabel_hari.id_hari=tabel_harga.id_hari')
     $this->db->order_by('id_harga','ASC');
     $sql = $this->db->get('tabel_harga');
    
     return $sql->result(); // returns an array of objects
}


controller

class Harga extends CI_Controller{

function __construct(){
parent::__construct();
  $this->load->model('M_harga');
  $this->load->helper(array('url','form'));
}

function index(){

  $data['tabel_harga'] = $this->M_harga->tampil_data();
  $this->load->view('v_harga',$data);
}

Upvotes: 0

sauhardnc
sauhardnc

Reputation: 1961

The data doesn't appear probably because you're using result() which returns object and you're getting data as array in your view.

Model

class M_harga extends CI_Model{

    function tampil_data(){

        $this->db->select('*');
        $this->db->from('tabel_harga'); 
        $this->db->join('tabel_vendor','tabel_vendor.id_vendor=tabel_harga.id_vendor', 'INNER');
        $this->db->join('tabel_hari','tabel_hari.id_hari=tabel_harga.id_hari', 'INNER');
        $this->db->order_by('id_harga','ASC'); 

        $query = $this->db->get()->result_array(); // use result_array() instead of result() as you're getting value as an array in your view.

        return $query;
    }
}

Also, make sure to check $tabel_harga for values in your view ie

<select class="form-control">
    <option value="">All</option>
      <?php
          if(!empty($tabel_harga)){
              foreach($tabel_harga as $u){
      ?>            
                  <option value="<?php echo $u['id_vendor']; ?>"><?php echo $u['nama_vendor']; ?></option>
      <?php 
              }
          }
      ?>
</select>

Hope this helps you.

Upvotes: 1

Related Questions