CREATE
TABLE
IF
NOT
EXISTS `posts` (
`id`
int
(11)
NOT
NULL
AUTO_INCREMENT,
`title`
varchar
(120)
NOT
NULL
,
`text` text
NOT
NULL
,
`
date
`
date
NOT
NULL
,
PRIMARY
KEY
(`id`)
) ENGINE=MyISAM;
INSERT
INTO
`posts` (`id`, `title`, `text`, `
date
`)
VALUES
(1,
'Some great article'
,
'It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using '
'Content here, content here'
', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for '
'lorem ipsum'
' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).'
,
'2009-08-10'
),
(2,
'Another great article'
,
'It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using '
'Content here, content here'
', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for '
'lorem ipsum'
' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).'
,
'2009-08-10'
),
(3,
'News from myfeed'
,
'It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using '
'Content here, content here'
', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for '
'lorem ipsum'
' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).'
,
'2009-08-10'
);
Step 2: Setting up CodeIgniter
Edit the autoload.php like so:
|
$autoload [ 'libraries' ] = array ( 'database' );
|
Edit the config.php like so:
|
Edit the database.php like so:
|
$db [ 'default' ][ 'hostname' ] = "localhost" ;
$db [ 'default' ][ 'username' ] = "root" ;
$db [ 'default' ][ 'password' ] = "" ;
$db [ 'default' ][ 'database' ] = "tut_feeds" ;
|
Edit the routes.php like this:
|
$route [ 'default_controller' ] = "Feed" ;
|
Step 3: Creating the Feed Controller
In this controller, all the magic happens. Browse to system/application/controllers and create a new file
called feed.php. Next, create the Feed controller and have it extend the parent CI Controller.
|
class Feed extends Controller {
function Feed()
{
parent::Controller();
}
}
|
Before the next step, we'll make use of CI's great helpers. Load the xml and text helper.
|
class Feed extends Controller {
function Feed()
{
parent::Controller();
$this ->load->helper( 'xml' );
$this ->load->helper( 'text' );
}
}
Step 4: Creating the Model
Next, will create a model to receive data from the database. If you don't know what models are, have a look at the CI
userguide. Browse to system/application/models
and create a file called posts_model.php.
|
class Posts_model extends Model {
function getPosts( $limit = NULL)
{
return $this ->db->get( 'posts' , $limit );
}
}
Step 5: Back to the Feed Controller
Now that we've created our model, we can continue with our feed controller. We'll load the posts_model that we just created.
|
class Feed extends Controller {
function Feed()
{
parent::Controller();
$this ->load->helper( 'xml' );
$this ->load->helper( 'text' );
$this ->load->model( 'posts_model' , 'posts' );
}
}
Now we create the index
method which is the method called by default. Let's set up some information for the feed view later too.
|
function index()
{
$data [ 'feed_name' ] = 'MyWebsite.com' ;
$data [ 'encoding' ] = 'utf-8' ;
$data [ 'page_description' ] = 'What my site is about comes here' ;
$data [ 'page_language' ] = 'en-en' ;
$data [ 'creator_email' ] = 'mail@me.com' ;
$data [ 'posts' ] = $this ->posts->getPosts(10);
header( "Content-Type: application/rss+xml" );
}
|
Finally, we need to load the view which we will create in the next step.
|
function index()
{
$data [ 'feed_name' ] = 'MyWebsite.com' ;
$data [ 'encoding' ] = 'utf-8' ;
$data [ 'page_description' ] = 'What my site is about comes here' ;
$data [ 'page_language' ] = 'en-en' ;
$data [ 'creator_email' ] = 'mail@me.com' ;
$data [ 'posts' ] = $this ->posts->getPosts(10);
header( "Content-Type: application/rss+xml" );
$this ->load->view( 'rss' , $data );
}
Step 6: Creating the View
Finally we have to create the view file - our output. Browse to system/application/views and crate a file called
rss.php.
First we set the xml version and the encoding within the head.
1
|
<?php echo '<?xml version="1.0" encoding="' . $encoding . '"?>' . "\n" ; ?>
|
Followed by some rss meta information.
|
<rss version= "2.0"
<channel>
|
Now we will access the array $data from the previous step. We can access this data via the array keys, like so:
|
<title><?php echo $feed_name ; ?></title>
<link><?php echo $feed_url ; ?></link>
<description><?php echo $page_description ; ?></description>
<dc:language><?php echo $page_language ; ?></dc:language>
<dc:creator><?php echo $creator_email ; ?></dc:creator>
<dc:rights>Copyright <?php echo gmdate ( "Y" , time()); ?></dc:rights>
|
Now we need to loop, with foreach, to get all records.
|
<?php foreach ( $posts ->result() as $post ): ?>
<item>
<title><?php echo xml_convert( $post ->title); ?></title>
<link><?php echo site_url( 'YOUR URL' . $post ->id) ?></link>
<guid><?php echo site_url( 'YOUR URL' . $post ->id) ?></guid>
<description><![CDATA[ <?php echo character_limiter( $post ->text, 200); ?> ]]></description>
<pubDate><?php echo $post -> date ; ?></pubDate>
</item>
<?php endforeach ; ?>
</channel>
<</rss>
|
|
|
|
|
|
0 comments:
Post a Comment