如何在代码点火器 3 中按外键显示名称值



在我的案例研究中,我想在外键中显示关系表中的值名称。我已经将带有 ID 的外键集中在另一个表中。

然后 如何显示关系中已有的外键的名称 拜托,我需要帮助来解决我的问题。 提前感谢...

这是表关系表关系

这是模型

public function get_list_kategories()
{
$this->db->select('prestasi_kategori');
$this->db->from($this->table);
$this->db->join('kategori', 'prestasi_kategori=kategori_id');
$this->db->order_by('prestasi_kategori', 'asc');
$query = $this->db->get();
$result = $query->result();
$kategories = array();
foreach ($result as $row) {
$kategories[] = $row->prestasi_kategori;
}
return $kategories;
}
public function get_list_jurusan()
{
$this->db->select('prestasi_jurusan');
$this->db->from($this->table);
$this->db->join('jurusan', 'prestasi_jurusan=jurusan_id');
$this->db->order_by('prestasi_jurusan', 'asc');
$query = $this->db->get();
$result = $query->result();
$jurusans = array();
foreach ($result as $row) {
$jurusans[] = $row->prestasi_jurusan;
}
return $jurusans;
}

这是控制器

public function __construct()
{
parent::__construct();
$this->data['title'] = 'Prestasi Mahasiswa';
$this->load->model('model_user');
$this->load->model('model_prestasi', 'm_prestasi');
}
public function index()
{
$kategories = $this->m_prestasi->get_list_kategories();
$jurusans = $this->m_prestasi->get_list_jurusan();
$opt = array('' => 'Semua Kategori');
foreach ($kategories as $kategori) {
$opt[$kategori] = $kategori;
}
$opt2 = array('' => 'Semua Jurusan');
foreach ($jurusans as $jurusan) {
$opt2[$jurusan] = $jurusan;
}
$this->data['form_kategori'] = form_dropdown('', $opt, '', 'id="kategori" class="form-control"');
$this->data['form_jurusan'] = form_dropdown('', $opt2, '', 'id="jurusan" class="form-control"');
$this->render_template('prestasi/index', $this->data);
}

这是视图

<form id="form-filter">
<div class="form-group">
<div class="row">
<div class="col-sm mr-auto">
<?php echo $form_kategori; ?>
</div>
<div class="col-sm">
<select class="form-control selectric" id="select_tahun" name="select_tahun">
<option value="All">Tahun</option>
<option value="2020">2020</option>
</select>
</div>
<div class="col-sm">
<?php echo $form_jurusan; ?>
</div>
<div class="col-sm ml-auto">
<button type="button" id="btn-filter" class="btn btn-primary">Filter</button>
<button type="button" id="btn-reset" class="btn btn-default">Reset</button>
</div>
</div>
</div>
</form>

这是在浏览器中查看

仅显示外键,但不能显示值名称外键

在您的模型中:

public function get_list_kategories()
{
$this->db->select('a.prestasi_kategori, b.kategori_nama');
$this->db->from($this->table." a");
$this->db->join('kategori b', 'a.prestasi_kategori=b.kategori_id');
$this->db->order_by('a.prestasi_kategori', 'asc');
$query = $this->db->get();
$result = $query->result();
$kategories = array();
foreach ($result as $row) {
$kategories[$row->prestasi_kategori] = $row->kategori_nama;
}
return $kategories;
}

在控制器中,更新索引函数:

$kategories = $this->m_prestasi->get_list_kategories();
$jurusans = $this->m_prestasi->get_list_jurusan();
$opt = array('' => 'Semua Kategori');
foreach ($kategories as $key => $value) {
$opt[$key] = $value;
}

将其应用于模型中的第二个函数和控制器中的 opt2。

相关内容

  • 没有找到相关文章

最新更新