简体   繁体   English

如何使用 CodeIgniter 以 JSON 格式保存数据?

[英]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.我有一个值,第二个是用于以 JSON 格式存储数据的模型部分。 Here spam_management is the column in the table in that I have to store like [{delete:30}] .这里spam_management是表中的列,我必须像[{delete:30}]一样存储。 30 is the value taken from the selected option. 30是取自所选选项的值。 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}] .为了创建一个 JSON 并像[{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.您需要将值保存在嵌套数组中,并在将其保存到数据库之前通过json_encode创建一个 JSON。

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

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

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