Tutorial Details
- Program: CodeIgniter PHP Framework
- Version: 1.7.1
- Difficulty: Easy
- Estimated Completion Time: 30 minutes
Step 1: What we Need

For this purpose, here are some dummy entries you can import. Create a database called tut_feeds. Then, copy the following code, and import it into your MySQL database.
01
02
03
04
05
06
07
08
09
10
11
12
| 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
Before we start writing code, we need to configure CodeIgniter.Browse to your CI folder, and then into system/application/config. We will need to edit the following files:
- autoload.php
- config.php
- database.php
- routes.php
1
| $autoload [ 'libraries' ] = array ( 'database' ); |

Edit the config.php like so:
1
|

Edit the database.php like so:
1
2
3
4
| $db [ 'default' ][ 'hostname' ] = "localhost" ; // your host $db [ 'default' ][ 'username' ] = "root" ; $db [ 'default' ][ 'password' ] = "" ; $db [ 'default' ][ 'database' ] = "tut_feeds" ; |

password with your personal database info.
Edit the routes.php like this:
1
| $route [ 'default_controller' ] = "Feed" ; |

localhost/YOUR DIRECTORY, this default controller will be loaded first. We'll create the feed in the next step.
Step 3: Creating the Feed Controller
In this controller, all the magic happens. Browse to system/application/controllers and create a new filecalled feed.php. Next, create the Feed controller and have it extend the parent CI Controller.
1
2
3
4
5
6
7
| class Feed extends Controller { function Feed() { parent::Controller(); } } |
Easy Development with CodeIgniter tutorial.
After you've learned the basics, return to continue this tutorial! :)
Before the next step, we'll make use of CI's great helpers. Load the xml and text helper.
01
02
03
04
05
06
07
08
09
10
| 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 CIuserguide. Browse to system/application/models
and create a file called posts_model.php.
1
2
3
4
5
6
7
8
| class Posts_model extends Model { // get all postings function getPosts( $limit = NULL) { return $this ->db->get( 'posts' , $limit ); } } |
from the database. The first parameter declares the table we want to use and with the second we can set a limit - so we
can tell CI how many records we want to retrieve.
Perhaps you've noticed that $limit is set to NULL by default. This makes it possible to set a limit, but you don't have to.
If you don't set a second parameter, this function will simply return all records.
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.
01
02
03
04
05
06
07
08
09
10
11
| class Feed extends Controller { function Feed() { parent::Controller(); $this ->load->helper( 'xml' ); $this ->load->helper( 'text' ); $this ->load->model( 'posts_model' , 'posts' ); } } |
method which is the method called by default. Let's set up some information for the feed view later too.
01
02
03
04
05
06
07
08
09
10
11
| function index() { $data [ 'feed_name' ] = 'MyWebsite.com' ; // your website $data [ 'encoding' ] = 'utf-8' ; // the encoding $data [ 'page_description' ] = 'What my site is about comes here' ; // some description $data [ 'page_language' ] = 'en-en' ; // the language $data [ 'creator_email' ] = 'mail@me.com' ; // your email $data [ 'posts' ] = $this ->posts->getPosts(10); header( "Content-Type: application/rss+xml" ); // important! } |
header("Content-Type: application/rss+xml"); is a very important part. This tells the browser to parse it as
an RSS Feed. Otherwise the browser will try to parse it as plain text or html.
With $data['posts'] = $this->posts->getPosts(10); we are using our model and are storing all records in the $posts array.
I set the limit to 10; so it will return, at most, 10 records. You can set this value higher or lower if you want. If we leave it
blank, like $data['posts'] = $this->posts->getPosts();, it would return all records.
Finally, we need to load the view which we will create in the next step.
01
02
03
04
05
06
07
08
09
10
11
12
13
| function index() { $data [ 'feed_name' ] = 'MyWebsite.com' ; $data [ 'encoding' ] = 'utf-8' ; // the encoding $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 ); } |
Your feed controller should now look like this:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| class Feed extends Controller { function Feed() { parent::Controller(); $this ->load->helper( 'xml' ); $this ->load->helper( 'text' ); $this ->load->model( 'posts_model' , 'posts' ); } 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 calledrss.php.
First we set the xml version and the encoding within the head.
1
| <?php echo '<?xml version="1.0" encoding="' . $encoding . '"?>' . "\n" ; ?> |
1
2
3
4
5
6
7
8
| <rss version= "2.0" <channel> |
1
2
3
4
5
6
7
8
9
| <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> |
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
| <?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> |
I hope you noticed CDATA. This is used for text-output (content). Remember how we learned in the head that this is xml;
so it has to be xml valid. If we don't set CDATA we'll potentially end up with invalid markup.
Step 7: Overview
Now your files should look like this:system/application/controllers/feed.php
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| class Feed extends Controller { function Feed() { parent::Controller(); $this ->load->helper( 'xml' ); $this ->load->helper( 'text' ); $this ->load->model( 'posts_model' , 'posts' ); } 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 ); } } |
1
2
3
4
5
6
7
8
| class Posts_model extends Model { // get all postings function getPosts( $limit = NULL) { return $this ->db->get( 'posts' , $limit ); } } |
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
| <?php echo '<?xml version="1.0" encoding="' . $encoding . '"?>' . "\n" ; ?> <rss version= "2.0" <channel> <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> <?php foreach ( $posts ->result() as $post ): ?> <item> <title><?php echo xml_convert( $post ->title); ?></title> <link><?php echo site_url( 'blog/posting/' . $post ->id) ?></link> <guid><?php echo site_url( 'blog/posting/' . $post ->id) ?></guid> <description><![CDATA[ <?php echo character_limiter( $post ->text, 200); ?> ]]></description> <pubDate><?php echo $post -> date ; ?></pubDate> </item> <?php endforeach ; ?> </channel> </rss> |

Conclusion
I hope you've learned how easy it is to build an RSS 2.0 Feed with the power of CodeIgniter. For more tutorials and screencasts on CodeIgniter, check out Jeffrey`s CodeIgniter from Scratch series.Follow us on Twitter,

No comments:
Post a Comment