Improve script that compares a local database against RSS feed results and only INSERTs missing rows using CodeIgniter
10:33 02 Dec 2020

I have some code that takes an RSS feed URL, expands the RSS link and gets the individual links inside that feed xml. After checking if a link exists, I insert it to a table if it does not exist and do nothing if it does. However my code is becoming more unreadable and one more check that requires another foreach it shall be even more unreadable.

This is the code

public function links_cron_job()
{
    //Get Rss Links
    $this->db->select("the_link_itself");
    $query = $this->db->get_where("search_engine_links", array("link_type" => "rss"));

    foreach ($query->result() as $row) {
        $url = $row->the_link_itself;
        $rss = Feed::loadRss($url);
        foreach ($rss->item as $item) {
            $this->db->where('the_link_itself',$item->link);
            $query3 = $this->db->get('search_engine_links');
            if ($query3->num_rows() > 0) {
                echo 'duplicates are there';
            } else {
                $data = array(
                    'link_country' => 'usa',
                    'the_link_itself' => $item->link,
                    'link_category' => 'news_website',
                    'link_added_by' => 'admin',
                    'link_type' => 'ordinary_link',
                    'link_local_type' => 'news',
                    'link_region' => 'countrywide',
                    'link_city' => 'washington',
                    'date_added' => $item->timestamp,
                    'last_updated' => time()
                );
                $this->db->insert('search_engine_links', $data);
                echo 'no duplicates are there';
            }
        }
    }
}

What would be another approach in doing what I am doing?

php codeigniter synchronization rss query-builder