0

I need to fetch rss feeds from 12 different websites. and i want only selected feeds will be display to user on main page. I am using Magpie RSS parser to display RSS feeds. But how i can store them in database. I used following code to fetch feeds.

<?php
include('magpierss/rss_fetch.inc');
define('MAGPIE_CACHE_DIR', '/var/cache');
$rss = fetch_rss(' here is url link');
$items = array_slice($rss->items, 0, 10);

foreach ($items as $item) {
    $href = $item['link'];
    $title = $item['title'];
    $desc = $item['description'];
    echo "<p><a href='$href'>$title</a><br>";
    if ($desc) {
        if (strlen($desc) >= 125) {
            $desc = substr($desc, 0, 124) . "...";
        }
    }
    echo $desc;
}
;
?>

now how i can store it to database.

René Höhle
  • 26,716
  • 22
  • 73
  • 82
manpreet
  • 81
  • 1
  • 12
  • Can't understand what part of storing is problematic for you. Can you write what you have tried? What tables do you have in DB? – lvil Jun 26 '12 at 08:58
  • You can create simple query, but the main question is: "Is it a good idea?" – Leri Jun 26 '12 at 08:58
  • @PLB what we can do with client requirement? They asked for this thing storing all feeds in database and then publish them to front end by choosing. – manpreet Jun 26 '12 at 09:05
  • @lvil well for database i need following fieldsdemo_feeds`( `Published` datetime DEFAULT NULL, `Author` varchar(25) CHARACTER SET latin1 DEFAULT NULL, `Title` mediumtext COLLATE utf8_unicode_ci, `Content` longtext COLLATE utf8_unicode_ci, `Link` mediumtext CHARACTER SET latin1, `Categories` mediumtext CHARACTER SET latin1, `itemid` varchar(40) CHARACTER SET latin1 DEFAULT NULL, `Id` int(11) NOT NULL AUTO_INCREMENT, PRIMARY KEY (`Id`), UNIQUE KEY `Id` (`Id`) ) ; – manpreet Jun 26 '12 at 09:09

1 Answers1

0

Try this logic I hope its work for you.....

<?php
include('magpierss/rss_fetch.inc');
define('MAGPIE_CACHE_DIR', '/var/cache');
$rss = fetch_rss(' here is url link');
$items = array_slice($rss->items, 0, 10);

$data = array();
$i = 0;
foreach ($items as $item) {
        $href = $item['link'];
        $title = $item['title'];
        $desc = $item['description'];
        echo "<p><a href='$href'>$title</a><br>";
        if($desc)
        if (strlen($desc) >= 125)
        {
                $desc = substr($desc,0,124)."...";
        }
        echo $desc;

        $data[$i]['href'] = $href;
        $data[$i]['title'] = $title;
        $data[$i]['desc'] = $desc;

        //create insert function like this InsertDbEntry($data) and set in function $data as argument;

$i++;
}
;

function InsertDbEntry($data){
    // Also you can add if condition for check this title is already exists in database or not
    foreach($data as $dbData){
        mysql_query("insert into table ('href','title','desc') values ('".$dbData['href']."','".$dbData['title']."','".$dbData['desc']."')");
    }
} 
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Query Master
  • 6,989
  • 5
  • 35
  • 58