i am running a cronjob every 5mins on a file that is pulling in a xml feed, at the moment it is just adding that feed to the database, so i may get below.
title, this is a new title
title, this is another title
but the problem i have got is when the cron runs again i get below
title, this is a new title
title, this is another title
title, this is a new title
title, this is another title
and when it runs again
title, this is a new title
title, this is another title
title, this is a new title
title, this is another title
title, this is a new title
title, this is another title
And so on…..
i only want it to insert a records if their is not one already in the database with the same title so my cron job will keep running and when it notices a different title THEN it inserts it into the db.
I have tried lots of separate option such a not_like etc.
Can someone tell me the best way to do this???
Thankyou 😉
I managed to do it with the following but definitely not the best way todo it
function addResults($data){
$this->db->select('title');
$this->db->from('articles');
$this->db->where('title = ' . "'" . $data['title'] . "'");
//$this->db->limit(1);
$query=$this->db->get();
echo $query->num_rows();
if($query->num_rows()){
}else{
echo 'New Record ' . $data['title'];
$this->db->set('date', 'NOW()', FALSE);
$this->db->insert('articles', $data);
}
}
I know I’m a little late for the party but here is a slightly better way to do it: