【问题标题】:Display data from database with Select Option Codeigniter使用 Select Option Codeigniter 显示数据库中的数据
【发布时间】:2020-06-02 16:52:31
【问题描述】:

首先我要道歉,因为我刚刚学习了 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>

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

【问题讨论】:

    标签: php codeigniter drop-down-menu select-options


    【解决方案1】:

    数据没有出现可能是因为您使用的是result(),它返回object,并且您在view 中以array 的形式获取数据。

    模型

    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 中的值以获取您的view

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

    希望对你有所帮助。

    【讨论】:

    • 你好,谢谢你,感谢你的帮助,但这仍然不起作用..数据没有出现.. :(
    • 这可能意味着您正在运行的query 不会产生任何价值。您可以通过echo $this-&gt;db-&gt;last_query(); 打印query,然后在phpmyadmin 中运行query。看看它是否返回任何行。
    • 我收到了这条消息.... 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
    • 是的,现在复制查询并在phpmyadmin 中运行它,如果它没有显示任何结果,则表示您的查询没有返回任何值并且您现在更改的代码是正确的。
    • 还是一样不工作:(,但非常感谢你的分享,真的很感激,你成功了..
    【解决方案2】:

    试试这个

    查看

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

    【讨论】:

    • 我有更新模型和控制器,希望对您有所帮助
    猜你喜欢
    • 2013-12-21
    • 2017-10-05
    • 1970-01-01
    • 2014-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多