0

首先我要道歉,因为我刚刚学习了 Codeigniter,我在使用 Select 选项显示数据库中的数据时遇到问题,没有错误但数据没有出现,为了您的信息,我已经加入了 3 个表。

这是我的控制器

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

这是我的模型

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

这是我的观点

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

如果你能帮助我,我将非常感激,谢谢你们。

4

2 回答 2

1

数据没有出现可能是因为您正在使用result()which 返回object并且您正在获取数据,如array. 模型view

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

另外,请确保检查$tabel_harga您的viewie中的值

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

希望这对您有所帮助。

于 2020-06-02T17:28:51.017 回答
0

尝试这个

看法

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

楷模


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
}


控制器

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);
}
于 2020-06-02T18:08:40.813 回答