0

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.

1

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

18 Comments

Hi man, thx before, i appreciate your help, but this still doesn't work.. the data doesn't appear.. :(
It could probably mean that the query you're running doesn't generate any value. You can print the query by echo $this->db->last_query(); and then run the query in phpmyadmin. See if it returns any row.
i got this message.... SELECT * FROM tabel_harga JOIN tabel_vendor ON tabel_vendor.id_vendor=tabel_harga.id_vendor JOIN tabel_hari ON tabel_hari.id_hari=tabel_harga.id_hari ORDER BY id_harga ASC
Yes, now copy the query and run it in phpmyadmin, if it shows no result then it means that your query is not returning any value and your now changed code is correct.
still same doesnt work :(, but thank you very much for sharing, really appreciete, success for you..
|
0

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

1 Comment

i have an update model and controllers, hope this help
0
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; 
}

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.