proning Posted June 7, 2011 Share Posted June 7, 2011 Hi im trying to create a rss read function which will read an rss news feed and store it in a mysql database so it can be formated and sorted. i have managed to read the rss and save to the database but i want to prevent duplicate data entrys every time the page is loaded heres my code so far <?php //function to take feed url and obtain rss entries function newsfeed($feedURL) { $rss = simplexml_load_file($feedURL); $i = 0; $z = 0; foreach ($rss->channel->item as $feedItem) { $i++; //split and store rss structure to varibles $feedTitle = $feedItem->title; $feedLink = $feedItem->link; $feedDescription = $feedItem->description; $feedDate = ($feedItem->pubDate); $dateForm = explode(" ", $feedDate); //select db and load news to check for duplicate entrys $news = mysql_query("SELECT * FROM rssnews"); $fnews = mysql_fetch_array($news); //test db entry echo $news['Title']; //if database already contasins news then do not save if ($feedTitle == $news['Title']){ // do nothing if matches echo "<h1>News Not updated in database</h1>"; }else { //insert new news to DB if not already on DB $query = "INSERT INTO rssnews (Title, Link, Description, Date1, Date2, Date3, Date4, Date5) VALUES ('$feedTitle', '$feedLink', '$feedDescription', '$dateForm[1]', '$dateForm[2]', '$dateForm[3]', '$dateForm[4]', '$dateForm[5]')"; mysql_query($query); echo "<p> -" . $feedTitle . " - " . $feedLink . " - " . $dateForm[1] . "- </p>"; } if($i >=5) break; } } ?> all help is appreciated im pretty much a php novice so sorry in advance Quote Link to comment Share on other sites More sharing options...
TeNDoLLA Posted June 7, 2011 Share Posted June 7, 2011 Use header('Location: /path/' . $somepage); after a succesful data entry. Quote Link to comment Share on other sites More sharing options...
proning Posted June 7, 2011 Author Share Posted June 7, 2011 i dont understand how that will prevent duplicate data entrys sorry Quote Link to comment Share on other sites More sharing options...
TeNDoLLA Posted June 7, 2011 Share Posted June 7, 2011 Did you mean that if you make an insert and u hit refresh in browser it inserts it again or something else? If you meant that the header location will work after the insert. If you did not mean it.. then I misunderstood and don't know really what you meant. Quote Link to comment Share on other sites More sharing options...
proning Posted June 7, 2011 Author Share Posted June 7, 2011 yes i did but i dont want to stop in inserting i just want to stop inserting if there is already an entry the same Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted June 7, 2011 Share Posted June 7, 2011 Doing the checks as you describe would require to do a mysql query and compare values of new content versus stored content, depending on if they match or not can do nothing/update....else insert new. The above process would slow everything down, it may be better to just make a script that cycles through your database and deletes any similar stored content......meaning duplicate entries. Quote Link to comment Share on other sites More sharing options...
proning Posted June 7, 2011 Author Share Posted June 7, 2011 so ive managed to stop duplicate entrys by setting a the url to a unique key in the mysql table how ever there are 10 sets of news in the rss which i can echo out and render to the site but it is only entering 8 to the db heres my code <?php //function to take feed url and obtain rss entries function newsfeed($feedURL) { $rss = simplexml_load_file($feedURL); $i = 0; foreach ($rss->channel->item as $feedItem) { $i++; //split and store rss structure to varibles $feedTitle = $feedItem->title; $feedLink = $feedItem->link; $feedDescription = $feedItem->description; $feedDate = ($feedItem->pubDate); $dateForm = explode(" ", $feedDate); //insert new news to DB $query = "INSERT INTO rssnews (Title, Link, Description, Date1, Date2, Date3, Date4, Date5) VALUES ('$feedTitle', '$feedLink', '$feedDescription', '$dateForm[1]', '$dateForm[2]', '$dateForm[3]', '$dateForm[4]', '$dateForm[5]')"; mysql_query($query); // test the lines entered into DB echo $feedLink; echo $feedTitle; echo $feedDescription; echo '<br /> <br />'; if($i >=200) break; } } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.