简体   繁体   中英

How to save data in JSON format using CodeIgniter?

I have a value and the second one is the model part which is used for storing the data in JSON format. Here spam_management is the column in the table in that I have to store like [{delete:30}] . 30 is the value taken from the selected option. How can I do that?

public function update_selectedspmlds()
{
  $value = $this->input->post("value");
  $this->approval_model->update_selectedspmlds($value);
}

public function update_selectedspmlds($value)
{   
  $myJson = '{
    "delete": [{
      "lastName": '.$value.'
    }]
  }'; 
  $this->db->insert('pm1cti_details', ['spam_management' => $myJson]); 
}

In order to create a JSON and store like [{delete:30}] . You need to save the value in a nested array and create a JSON by json_encode before saving it to the DB.

public function update_selectedspmlds($value) {   
  $myArray = array(
               array(
                'delete' => 30
              )
            );
  $myJson = json_encode($myJson);
  $this->db->insert('pm1cti_details', ['spam_management' => $myJson]); 
}

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