
- Views: 4.1K
- Category: Codeigniter
- Published at: 03 Apr, 2020
- Updated at: 12 Aug, 2023
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 with content in the form of CSV and other formats.
You are the responsible person who uses the data according to the system's needs, even if you have some mistakes in your database or table, and you want to find the pattern and replace it with another pattern.
Let's take examples of how you can create the find and replace the system in Codeigniter.
You have the entries in table Xyz, and you want to find ABC from a column and replace it with some DIE. These are two patterns mand; let's suppose you have millions of records in your database table, so it is hard to find the ABC pattern and replace it with DIE manually. You can also do it manually with Microsoft excel and another tool. You can still create a system that finds your pattern from the database and replace it with another pattern, as we discussed above. This article will create a system that finds so0meting from your database table and replace it with you another pattern.
Before starting, you have to need one model and one controller; I have this model named ModEnglish and a 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 replace with DIE, we suing the stripes function in the if condition to check whether 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. 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 the controller to the model; in the model, we are using the 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';
}
}
}
?>
https://www.youtube.com/watch?v=zBQ72a3p97g
0 Comment(s)