首页 > 解决方案 > 如何在 codeigniter 中为同一个表创建动态/链接下拉列表

问题描述

我正在尝试从不同列中的 1 个表创建动态下拉列表,但我找不到任何适合我需要的教程

它有orang_tua表,其中有n_ibun_ayah但我不知道如何创建动态下拉列表

这是orang_tua

CREATE TABLE `orang_tua` (
    `id` INT(3) NOT NULL AUTO_INCREMENT,
    `n_ibu` VARCHAR(100) NOT NULL,
    `n_ayah` VARCHAR(100) NOT NULL,
    `email` VARCHAR(100) NOT NULL,
    `no_tlp` VARCHAR(20) NOT NULL,
    `alamat` VARCHAR(100) NOT NULL,
    PRIMARY KEY (`id`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
AUTO_INCREMENT=2
;

这是我的观点,但只能在下拉列表中显示 1 列值,即n_ibu

<option value="">-- Pilih dari daftar --</option>
<?php 
foreach($all_orang_tua as $orang_tua)
{
$selected = ($orang_tua['id'] == $this->input->post('id_orang_tua')) ? ' selected="selected"' : "";

echo '<option value="'.$orang_tua['id'].'"'.$selected.'>'.$orang_tua['n_ibu'].'</option>';
} 
?>

这是模型

<?php

class Siswa_model extends CI_Model
{
    function __construct()
    {
        parent::__construct();
    }

    /*
     * Get siswa by id
     */
    function get_siswa($id)
    {
        return $this->db->get_where('siswa',array('id'=>$id))->row_array();
    }

    /*
     * Get all siswa
     */
    function get_all_siswa()
    {
        $this->db->order_by('id', 'desc');
        return $this->db->get('siswa')->result_array();
    }

    /*
     * function to add new siswa
     */
    function add_siswa($params)
    {
        $this->db->insert('siswa',$params);
        return $this->db->insert_id();
    }

    /*
     * function to update siswa
     */
    function update_siswa($id,$params)
    {
        $this->db->where('id',$id);
        return $this->db->update('siswa',$params);
    }

    /*
     * function to delete siswa
     */
    function delete_siswa($id)
    {
        return $this->db->delete('siswa',array('id'=>$id));
    }

    /*
     * custom function to show index siswa
     */
    function get_data_siswa()
    {
        $this->db->select('sw.id, sw.nama, sw.j_kelamin, sw.tmp_lahir, sw.tgl_lahir, sw.agama, sw.alamat, sw.no_tlp, sw.email, ot.n_ibu, ot.n_ayah, sk.nama_sek');
        $this->db->from('siswa sw');
        $this->db->join('orang_tua ot', 'ot.id=sw.id_orang_tua');
        $this->db->join('sekolah sk', 'sk.id=sw.id_sekolah');
        $this->db->distinct('siswa');
        return $this->db->get('siswa')->result_array();
    }
}

这是控制器

function add()
    {   
        $this->load->library('form_validation');

        $this->form_validation->set_rules('email','Email','valid_email|required');
        $this->form_validation->set_rules('id_orang_tua','Id Orang Tua','required');
        $this->form_validation->set_rules('id_sekolah','Id Sekolah','required');
        $this->form_validation->set_rules('nama','Nama','required');
        $this->form_validation->set_rules('j_kelamin','J Kelamin','required');
        $this->form_validation->set_rules('tmp_lahir','Tmp Lahir','required');
        $this->form_validation->set_rules('tgl_lahir','Tgl Lahir','required');
        $this->form_validation->set_rules('agama','Agama','required');
        $this->form_validation->set_rules('alamat','Alamat','required');
        $this->form_validation->set_rules('no_tlp','No Tlp','required');

        if($this->form_validation->run())     
        {   
            $params = array(
                'j_kelamin' => $this->input->post('j_kelamin'),
                'id_orang_tua' => $this->input->post('id_orang_tua'),
                'id_sekolah' => $this->input->post('id_sekolah'),
                'nama' => $this->input->post('nama'),
                'tmp_lahir' => $this->input->post('tmp_lahir'),
                'tgl_lahir' => $this->input->post('tgl_lahir'),
                'agama' => $this->input->post('agama'),
                'alamat' => $this->input->post('alamat'),
                'no_tlp' => $this->input->post('no_tlp'),
                'email' => $this->input->post('email'),
            );

            $siswa_id = $this->Siswa_model->add_siswa($params);
            redirect('siswa/index');
        }
        else
        {
            $this->load->model('Orang_tua_model');
            $data['all_orang_tua'] = $this->Orang_tua_model->get_all_orang_tua();

            $this->load->model('Sekolah_model');
            $data['all_sekolah'] = $this->Sekolah_model->get_all_sekolah();

            $data['_view'] = 'siswa/add';
            $this->load->view('layouts/main',$data);
        }
    }  

这是orang_tua_model

<?php

class Orang_tua_model extends CI_Model
{
    function __construct()
    {
        parent::__construct();
    }

    /*
     * Get orang_tua by id
     */
    function get_orang_tua($id)
    {
        return $this->db->get_where('orang_tua',array('id'=>$id))->row_array();
    }

    /*
     * Get all orang_tua
     */
    function get_all_orang_tua()
    {
        $this->db->order_by('id', 'desc');
        return $this->db->get('orang_tua')->result_array();
    }

    /*
     * function to add new orang_tua
     */
    function add_orang_tua($params)
    {
        $this->db->insert('orang_tua',$params);
        return $this->db->insert_id();
    }

    /*
     * function to update orang_tua
     */
    function update_orang_tua($id,$params)
    {
        $this->db->where('id',$id);
        return $this->db->update('orang_tua',$params);
    }

    /*
     * function to delete orang_tua
     */
    function delete_orang_tua($id)
    {
        return $this->db->delete('orang_tua',array('id'=>$id));
    }
}

我真的很想制作 2 个与 n_ibu 有关系的下拉菜单,所以当我选择n_ibu时,我可以n_ayah的同一行中选择下拉数据。我还是codeigniter的新手,希望有人可以帮助我

标签: javascriptphphtmlmysqlcodeigniter

解决方案


在您的视图页面中实现以下 HTML 和 PHP 脚本以创建动态下拉列表。

<select>
   <option value="">-- Pilih dari daftar --</option>
   <?php
   if(!empty($all_orang_tua))
   {
      foreach ($all_orang_tua as $orang_tua) 
      {
        $selectedValue="";
        if($orang_tua['id'] == $this->input->post('id_orang_tua'))
        {                                                          
           $selectedValue="selected";
        }
        ?>
        <option <?php echo $selectedValue;?> value="<?php echo $orang_tua['id'];?>"><?php echo $orang_tua['n_ibu'];?></option>
        <?php
      }
   }
   ?>
</select>

我希望使用上面的代码可以解决您的问题。


推荐阅读