简体   繁体   中英

How to save in json format with Codeigniter

I have the following:

<form method="POST" action="<?php echo site_url('admin/updateCoursesIn'); ?>">
    <input type="text" value="1" name="values">
    <input type="text" value="2" name="values">
    <input type="text" value="3" name="values">

    <input type="submit" value="Submit">
</form>

Function in codeigniter:

public function updateCoursesIn($from = "") {
            $data['value'] = json_encode($this->input->post('values'));
            $this->db->where('key', 'courses');
            $this->db->update('frontend_settings', $data);
        }

I need to save these values via codeigniter in json format, like this:

["1","2","3"]

The function I created is not saving as I need it.

To make values array, add [] to name attribute:

<input type="text" value="1" name="values[]">
<input type="text" value="2" name="values[]">

With such naming $this->input->post('values') will be an array and will be correctly encoded to json.

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