简体   繁体   中英

How to display name value by foreign key in foreach in codeigniter 3

in my case study, i want to show the value name from relation table in foreign key. I already concentrated the foreign key with ID in another table.

And then how do I display the name of the foreign key that is already in the relationship please, i need help for my problem. Thank in advance...

this is table relation table relation

this is Model

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

this is Controller

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

this is View

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

this is view in browser

only display foreign key, but can't display value name the foreign key

In your model:

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

In your controller, update index function:

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

Apply this to the second function in your model and to opt2 in your controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM