繁体   English   中英

如何将数组值转换为字符串并保存到数据库

[英]how to convert array value to string and save to database

我有麻烦,如何在数据库中保存数组值,每次输入数据时,仅输入存储在数据库中的最后一个数据。 这是我的代码:

控制器:

function tambah_rm($id)
{
    $this->load->helper('form');
    $this->load->library('form_validation');

    $data['title'] = 'Tambah Detail Rekam Medis';

    $this->form_validation->set_rules('tgl_berobat', 'tgl_berobat', 'required');
    $this->form_validation->set_rules('anamnesa', 'anamnesa', 'required');
    $this->form_validation->set_rules('diagnosa', 'diagnosa', 'required');
    $this->form_validation->set_rules('therapi', 'therapi', 'required');
    $this->form_validation->set_rules('keterangan', 'keterangan', 'required');

    if ($this->form_validation->run() === FALSE)
    {
        $data['obat']=$this->m_pasien->get_obat();
        $data['header']=$this->m_pasien->header_pasien($id)->row_array();
        $this->template->load('template','v_tambahRM',$data);

    }
    else
    {
        $this->m_pasien->tambahrm($data);
        redirect('pasien');
    }
}

我的模特:

function tambahrm() 
{
    $this->load->helper('url');
    $username = trim($this->session->userdata('id_user'));
    $data = array(
        'tgl_berobat' => $this->input->post('tgl_berobat'),
        'anamnesa' => $this->input->post('anamnesa'),
        'diagnosa' => $this->input->post('diagnosa'),
        'therapi' => $this->input->post('therapi'),
        'keterangan' => $this->input->post('keterangan'),
        'id_user' => $username,
        'id_pasien' => $this->input->post('id_pasien'),
    );

    if ($id == 0) {
        return $this->db->insert('tbl_riwayat', $data);
    } else {
        $this->db->where('id', $id);
        return $this->db->update('tbl_riwayat', $data);
    }
}

function get_obat() 
{
    $data_obat = array();
    $this->db->select('nama_obat');
    $this->db->from('tbl_obat');
    $query = $this->db->get();

    if ($query->num_rows() > 0) {
        foreach ($query->result_array() as $row) {
            $data_obat[] = $row;
        }
    }
    return $data_obat;
}

视图

<div class="form-group m-b-20">
            <div class="col-xs-12">
                <label for="therapi">Therapi</label>
                <select class=" form-control js-example-basic-multiple" multiple="multiple" name="diagnosa">
                      <?php
                        foreach($obat as $option)
                        {
                            ?>
                            <option value="<?=$option['nama_obat']?>"><?=$option['nama_obat']?></option>
                            <?php
                        }
                        ?>
                    </select>
            </div>
        </div>

这是存储在数据库中的数据的屏幕截图,我将黄色表示为我在多个选择字段中输入的最后一个数据。 在此处输入图片说明

Php have multiple option to convert array into string some of the option you can try are listed below.
implode() /explode()
json_encode() / json_decode()
serialize() / unserialize()


Above answer telling you how to convert array into serialized . So i  only mention other two option in my answer.

to convert the array into a string and back:
#Option one
function tambahrm() 
{
    $this->load->helper('url');
    $username = trim($this->session->userdata('id_user'));
    $data = array(
        'tgl_berobat' => $this->input->post('tgl_berobat'),
        'anamnesa' => $this->input->post('anamnesa'),
        'diagnosa' => json_encode($this->input->post('diagnosa')),
        'therapi' => $this->input->post('therapi'),
        'keterangan' => $this->input->post('keterangan'),
        'id_user' => $username,
        'id_pasien' => $this->input->post('id_pasien'),
    );

    if ($id == 0) {
        return $this->db->insert('tbl_riwayat', $data);
    } else {
        $this->db->where('id', $id);
        return $this->db->update('tbl_riwayat', $data);
    }
}

function get_obat() 
{
    $data_obat = array();
    $this->db->select('nama_obat');
    $this->db->from('tbl_obat');
    $query = $this->db->get();

    if ($query->num_rows() > 0) {
        foreach ($query->result_array() as $row) {
            $row['diagnosa'] = json_decode($row['diagnosa'],true);
            $data_obat[] = $row;
        }
    }
    return $data_obat;
}


#option two

function tambahrm() 
{
    $this->load->helper('url');
    $username = trim($this->session->userdata('id_user'));
    $data = array(
        'tgl_berobat' => $this->input->post('tgl_berobat'),
        'anamnesa' => $this->input->post('anamnesa'),
        'diagnosa' => implode(",",$this->input->post('diagnosa')),
        'therapi' => $this->input->post('therapi'),
        'keterangan' => $this->input->post('keterangan'),
        'id_user' => $username,
        'id_pasien' => $this->input->post('id_pasien'),
    );

    if ($id == 0) {
        return $this->db->insert('tbl_riwayat', $data);
    } else {
        $this->db->where('id', $id);
        return $this->db->update('tbl_riwayat', $data);
    }
}

function get_obat() 
{
    $data_obat = array();
    $this->db->select('nama_obat');
    $this->db->from('tbl_obat');
    $query = $this->db->get();

    if ($query->num_rows() > 0) {
        foreach ($query->result_array() as $row) {
            $row['diagnosa'] = explode(",",$row['diagnosa']);
            $data_obat[] = $row;
        }
    }
    return $data_obat;
}

First option using explode and implode and second option is using json_encode and decode.

you can use any option according to your requirement. But i personally suggest you json encode or serialized  pick from this two as they do not change key index of any type of array .

您可以使用

  • json_encode() / json_decode()
  • serialize() /反unserialize()

array转换为序列化的string并返回:

function tambahrm() 
{
    $this->load->helper('url');
    $username = trim($this->session->userdata('id_user'));
    $data = array(
        'tgl_berobat' => $this->input->post('tgl_berobat'),
        'anamnesa' => $this->input->post('anamnesa'),
        'diagnosa' => json_encode($this->input->post('diagnosa')),
        'therapi' => $this->input->post('therapi'),
        'keterangan' => $this->input->post('keterangan'),
        'id_user' => $username,
        'id_pasien' => $this->input->post('id_pasien'),
    );

    if ($id == 0) {
        return $this->db->insert('tbl_riwayat', $data);
    } else {
        $this->db->where('id', $id);
        return $this->db->update('tbl_riwayat', $data);
    }
}

function get_obat() 
{
    $data_obat = array();
    $this->db->select('nama_obat');
    $this->db->from('tbl_obat');
    $query = $this->db->get();

    if ($query->num_rows() > 0) {
        foreach ($query->result_array() as $row) {
            $row['diagnosa'] = json_decode($row['diagnosa']) ?: null;
            $data_obat[] = $row;
        }
    }
    return $data_obat;
}

供参考,请参阅:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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