Thor
Honorary Master
- Joined
- Jun 5, 2014
- Messages
- 44,236
Hello, this is basically a follow up with the next phase of my plan, but I am a little lost again.
I have an RSS feed from News24, I want to store that RSS feed in my DB and then read from it and create a new rss feed to post the Title, link and description on the forum.
Now I have the second part figured out creating the RSS feed is very straight forward basically:
Easy.
The question is.....
How do I import the original RSS feed without making duplicate entries?
currently my foreach loop has this inside:
I hope this makes sense?
Baiscally I will run this as a cron job as well but I do not know how to prevent inserting duplicate feeds into the DB.
This is the feed in question:
http://feeds.24.com/articles/Fin24/Markets/rss
EDIT:
This is my current cronjob this is what I do to insert the RSS feed into the DB however I am at a loss as to what to do in order to prevent duplicate inserts, this is where my knowledge ends (this is a lot of learning in the past two days) Thanks to everyone who helped so far! I greatly appreciate it.
This is how I fetch it from the DB to display on the main page:
I have an RSS feed from News24, I want to store that RSS feed in my DB and then read from it and create a new rss feed to post the Title, link and description on the forum.
Now I have the second part figured out creating the RSS feed is very straight forward basically:
PHP:
//DB code up here
while($row = $result->fetch_assoc()){
?>
<item>
<title><?php echo $row['title']; ?></title>
<link><?php echo $row['link']; ?></link> //this link will be changed according to your post's URL
<description><?php echo $row['description']; ?></description>
</item>
Easy.
The question is.....
How do I import the original RSS feed without making duplicate entries?
currently my foreach loop has this inside:
PHP:
$statement = $db->prepare("INSERT INTO market_feed (`title`, `description`, `link`, `date`) VALUES (:title, :description, :link, :date)");
$statement->execute(array(':title' => $title, ':description' => $description, ':link' => $link, ':date' => $date));
I hope this makes sense?
Baiscally I will run this as a cron job as well but I do not know how to prevent inserting duplicate feeds into the DB.
This is the feed in question:
http://feeds.24.com/articles/Fin24/Markets/rss
EDIT:
This is my current cronjob this is what I do to insert the RSS feed into the DB however I am at a loss as to what to do in order to prevent duplicate inserts, this is where my knowledge ends (this is a lot of learning in the past two days) Thanks to everyone who helped so far! I greatly appreciate it.
PHP:
<?php
$rss = new DOMDocument();
$rss->load('http://feeds.24.com/articles/Fin24/Markets/rss');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = 5;
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$date = date('l F d, Y', strtotime($feed[$x]['date']));
$statement = $db->prepare("INSERT INTO market_feed (`title`, `description`, `link`, `date`) VALUES (:title, :description, :link, :date)");
$statement->execute(array(':title' => $title, ':description' => $description, ':link' => $link, ':date' => $date));
}
?>
This is how I fetch it from the DB to display on the main page:
PHP:
<?php
//Select RSS feed to show
$sql = "SELECT * FROM (SELECT ID, title, description, link, date FROM market_feed ORDER BY ID DESC LIMIT 5) sub ORDER BY ID ASC";
$statement = $db->prepare($sql);
$statement->execute();
//Display the selected RSS feed
while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
echo '<p class="no-buffer"><strong><a href="'.$row["link"].'" target="_blank" title="'.$row["title"].'">'.$row["title"].'</a></strong><br />';
echo '<small><em>Posted on '.$row["date"].'</em></small></p>';
echo '<p class="no-buffer-description">'.$row["description"].'</p>';
}
?>
Last edited:
