Skip to content

How to Update some records with still maintain other records in Codeignitter?

Table Info :

id-data id-note note
1 5 I went home with John Harrison
2 5 I had dinner with John Harrison
3 5 I went shopping with John Harrison
4 3 I had dinner with John Harrison

I want to updateJohn Harrison” in note where id-note (5), with “Sebastian” but still maintain the other sentence, using codeigniter-3 model-controller

Table Info after update should be :

id-data id-note note
1 5 I went home with Sebastian
2 5 I had dinner with Sebastian
3 5 I went shopping with Sebastian
4 3 I had dinner with John Harrison

All suggestions will be very helpful.

Answer

I made an example for you that I think works for you.

you need to find and use str_replace for change your note. see example:

      $findText    = 'John Harrison';
      $replaceText = 'Sebastian';

      $this->db->select('*');
      $this->db->from('test');
      $this->db->where('id-note',5);
      $query = $this->db->get();
      $result = $query->result_array();

      foreach($result as $val) {
          $youText   = $val['note'];
          $replace_1 = ["$findText"];
          $replace_2 = ["$replaceText"];
          $result = str_replace($replace_1, $replace_2, $youText);

          $updateText = array(
              'note'=>$result
          );
          $this->db->where('id-data',$val['id']);
          $this->db->update('info',$updateText);
      }