javascript - 如何在 codeigniter 中为同一个表创建动态/链接下拉列表

标签 javascript php html mysql 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
;

这是我的 View ,但只能在下拉列表中显示 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();
    }
}

这是 Controller

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 的新手,我希望有人能帮助我

最佳答案

据我所知,您必须使用 javascript 才能实现该功能。
假设您已经像这样设置了两个 select 输入:

<p>
<label for="ibu_select">Nama Ibu</label><br/>
<select name="id_orang_tua" id="ibu_select" >
    <option selected>-- Pilih dari daftar --</option>
    <?php 
    foreach($all_orang_tua as $orang_tua)
    {
    $selected = ($orang_tua['id'] == $this->input->post('id_orang_tua')) ? ' selected="selected"' : "";
    ?>
    <option value="<?= $orang_tua['id'] ?>" data-ayah="<?= $orang_tua['n_ayah'] ?>" <?= $selected ?> ><?= $orang_tua['n_ibu'] ?></option>
    <?php
    }
    ?>
</select>
</p>
<p>
<label for="ayah_select">Nama Ayah</label><br/>
<select name="n_ayah" id="ayah_select">
    <option id="ayah_select_val">-- Pilih dari daftar --</option>
</select>
</p>

那么您可以为此目的使用 HTML data-* 属性,因为 n_ayah 数据与 n_ibu 数据位于同一行,合并在第一个下拉输入上使用 onchange 事件监听器:

<script>

var selection = document.getElementById("ibu_select");
selection.onchange = function(event){
    var n_ayah = event.target.options[event.target.selectedIndex].dataset.ayah;
    if (n_ayah) {
        document.getElementById("ayah_select_val").value = n_ayah;
        document.getElementById("ayah_select_val").text = n_ayah;
    } else {
        document.getElementById("ayah_select_val").value = "";
        document.getElementById("ayah_select_val").text = "-- Pilih dari daftar --";
    }
};

</script>

脚本将分别根据每个第一个 data- 值设置第二个下拉值。

引用:

关于javascript - 如何在 codeigniter 中为同一个表创建动态/链接下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57078298/

相关文章:

javascript - 为函数接受 2 个参数且只给出一个参数时设置默认参数 - Javascript

php - 从 2 个表中获取数据并在一个查询中创建一个表

html - CSS 语音弹出窗口和内部列表分隔符

html - 使用css3动画的 float 按钮

javascript - 使用 javascript 获取嵌入视频的 URL

javascript - 将 Google 电子表格转换为 JSON 格式

javascript - "angular-in-memory-web-api"英雄之旅教程问题

PHP 更新从数据库中选择标签

php - 如何在 Symfony 1.2 中获取表单中的用户数据?

html - 如何在图标后对齐多行文本