Find And Replace System in Codeigniter 3 

Find And Replace System in Codeigniter

Working as a web developer is a challenging task for every person. Your client always provides you the content int the form CSV and other formats.

You are the responsible person who uses the data according to the system need, even if you have some mistakes in your database or table, and you want to find the patter and replace it with another pattern.

Let take examples of how you can create the find and replace the system in Codeigniter.

You have the entries in a table Xyz, and you want to find ABC from a column and replace it with some DIE. So these are two patters mand let suppose you have millions of records in your database table, so it hards to find the ABC pattern and replace it with DIE manually. You can also do it manually with Microsoft excel and another tool. Still, you can create a system that finds your pattern from the database and replace it with another patter as we discussed above, so in this article, we are going to create a system which finds so0meting from your database table and replace with you another pattern.

Before starting you have to need one model and one controller, I have this model named ModEnglish and controller called Home

Here is my method in our model, which finds each record from the English table; if you have multiple tables, you can also use joins in Codeigniter to fetch the record from various tables.

<?php
class modEnglish extends CI_Model
{
    
    function __construct()
    {
        parent::__construct();
    }


    public function getallEnglishWrod()
    {
        return $this->db->get_where('english',
        array(
                'eng_status' => 1,
          )
        );
    }

    public function updateMyRecord($data)
    {
        return  $this->db->update_batch('english', $data, 'eng_id');

    }
}
?>

 

And here is the method named find and replace to find ABC and replacing with DIE, we suing the stripos function in if condition to check either the word exists or not.

If your word exists, we are storing the data in a variable named AddintData in the form of the associative array because we have to send this information to the database once we have the data. We have another method called updateMyRecord to transmit the data from controller to model, in the model we are using update_batch() method to update the data in the database.

<?php
class Home extends CI_Controller
{
    public function findAndReplace()
    {
        $words = $this->modEnglish->getallEnglishWrod();
        $AddintData = array();

        foreach ($words->result() as $myword) {
            if (stripos($myword->eng_word, "ABC") !== false) {
                $AddintData[] = array(
                                    'eng_id' => $myword->eng_id,
                                    'eng_word' => str_replace("ABC", "DIE", $myword->eng_word),
                                    'eng_updated' => date('Y-m-d H:i:s'),
                                    );
            }
        }

       $words = $this->modEnglish->updateMyRecord($AddintData);
       if ($words) {
          echo 'fine.';
       }
       else{
         echo 'not inserted';
       }

    }

}
?>

 

 

shakzee
administrator
My Name is Shehzad Ahmed and I am a full-time developer with over 7+ years of experience. In that time, I’ve worked as a developer with many big and small firms and was teaching web design and development skills to many keen learners and by the time passes I’ve found a great passion and potential on myself as a teacher on this particular field and now I am a full-time teacher and working as a freelancer.

Leave a Reply

Your email address will not be published. Required fields are marked *