Professional Documents
Culture Documents
Mobile/Desktop Sw itching
Code
(/developing/page/mobiledesktop-
switching-code)
The dotMobi WordPress
Mobile Pack (/running/story/the-
dotmobi-wordpress-mobile-pack)
tac-device-recognition)
competitors such as Zend Framework, it comes with all the basic libraries that we are all using in every
single project we develop. The community also provides a number of extra libraries for some more specific
tasks. At the bottom of the article you will find a number of links to the official website, documentation
forums, etc.
In this short tutorial I will show you how to get started with the development of a simple site to display
mobiForge's RSS feed on a PC or on a generic mobile device. This will allow us to focus on how
CodeIgniter applications are developed and how its basic device detection can be implemented. Basic PHP
knowledge is required, especially if you want to enhance the application beyond this guide; knowledge of
RSS is not required. Basic knowledge of XHTML and CSS are also recommended, but not required.
The reasons why I immediately like CodeIgniter is the simplicity of the approach and the provision of a
number of very common features such as clean, semantic URLs, MVC (Model-View-Controller
(http://en.wikipedia.org/wiki/Model-view-controller) ) approach and extensibility.
I want to point out, though, that CodeIgniter assumes one install for every application that you want to
develop. Make sure you fill the appropriate parameters as described in the installation guide, in this case
setting only the base URL should be enough, in my case I used "makefeed.local" as I am developing on my
Mac. If you intend to move your application to a public server then you will have to use the appropriate
address, of course.
mobiforge.com/…/a-simple-mobile-site-… 1/7
12/4/2010 A Simple Mobile Site with CodeIgniter |…
Getting started
To get started we will create a new controller called feed.php in the system/application/controllers
directory, in fact all files that belong to our application will be in a subdirectory of system/application/ as per
the CodeIgniter defaults. Paste the following code in your new file using your favourite text editor:
<?php
c l a s s Feed e x t e n d s Controller {
f u n c t i o n index() {
$this->feed_list();
}
f u n c t i o n feed_list() {
$this->load->view('feed_basic');
}
}
?>
This way we have created a very basic controller that does nothing but show the content of the view called
feed_basic. The function index.php is executed by default when no other parameters are called and this will
invoke our default method feed_list that will show the list of articles in the feed.
We now need to create the view file. Note the line that initialises the view,
$this->load->view('feed_basic');, this is a standard CodeIgniter call and will load a file called
feed_basic.php in the views subdirectory. Let's create the file and paste this simple markup:
<?php
f u n c t i o n _feed_init() {
$this->load->library('simplepie'); //This is all you need to initialise a library in CodeIgniter
$this->simplepie->set_cache_location('/tmp/'); //Change this to any temp directory that works for you
mobiforge.com/…/a-simple-mobile-site-… 2/7
12/4/2010 A Simple Mobile Site with CodeIgniter |…
$this->simplepie->set_feed_url('mobiforge.com/rss.xml');
$this->simplepie->init();
}
Nothing special here, but if we want to pass dynamic parameters at a later stage it might be useful, for
example to manage different feeds or specific values for caching, encoding and so on.
Then a little loop to cycle through the elements, this is particularly useful to manage parameters for the items
that we want to load:
f u n c t i o n _get_items($offset, $amount) {
$this->load->helper('url'); // useful to check, parse and clean URLs
$blog_items = $this->simplepie->get_items($offset, $amount); //load $amount RSS items starting from $offset
$count = 0;
//Loop through the RSS items to create a nice associative array for display in a view
foreach($blog_items as $item) {
$data['blog_items'][$count]['title'] = $item->get_title();
$data['blog_items'][$count]['author'] = $item->get_author()->get_name();
$data['blog_items'][$count]['local_date'] = $item->get_local_date();
$data['blog_items'][$count]['content'] = $item->get_content();
$data['blog_items'][$count]['permalink'] = $item->get_permalink();
$count++;
}
return $data;
}
This was an important step because the view engine in CodeIgniter is not very powerful, but it is very
efficient; preparing the right variables in the right way will make it much easier and speedier to print. I will not
go into the detail of each SimplePie method, the name should be self-explanatory, you are welcome to read
the official documentation for more details.
Let's update the view to display some dynamic values. All the information loaded from SimplePie was
stacked up in an associative array called $data and passed to the view as the second parameter, this is the
standard way of doing this in CodeIgniter. By magic of the framework, the array will not be available in the
view, but all of the keys of the array will be variable names. When creating a more complex structure such
as the blog_items we will have to cycle them with a loop. Let's see how the new view should look like:
<div c l a s s ="articles">
< ? p h p foreach($blog_items as $item) : ? >
<h1><a href="<?php echo $item['permalink'] ?>"><?php e c h o ( ht t p :/ / ww w . ph p .n e t /e c h o) $item['title'
<p><em>Author: < ? p h p e c h o ( ht t p: / / ww w . ph p .n e t /e c ho ) $item['author'] ? > On < ? p h p e c h o ( h tt p : // w ww . p hp . n et / ec h o )
<p><?php e c h o ( h tt p :/ / w ww . ph p . ne t / ec h o) $item['content'] ?></p>
< ? p h p endforeach; ? >
</div>
To display the blog articles we had to use the alternative syntax for the foreach
loop, this is a requirement of CodeIgniter. You can read more
(http://www.php.net/manual/en/control-structures.alternative-syntax.php) about
this on the official PHP documentation.
As you might have noticed with the method _get_items() we have already laid
down the stepping-stones for pagination. While this might not be an issue in RSS readers and on your PC or
Mac, it might be an issue on mobile devices; in fact, even if we listed only the titles of each articles that might
easily be simply too long and not very comfortable on some devices. Paginating at a reasonable number of
articles per page will make it much easier to use and faster to load.
Since mobiForge only provides the excerpt of articles we do not have to worry too much about the size of
mobiforge.com/…/a-simple-mobile-site-… 3/7
12/4/2010 A Simple Mobile Site with CodeIgniter |…
the page, as long as we keep a small number of articles per page, of course. Also, the RSS feed already
provides the "Read more" link, so it's a much easier job for us.
We are now going to add a check to see if the user is on a mobile device or a desktop computer, add icons
to switch to an alternative presentation and pagination. It seems like a lot of stuff, but it's not complicated. If
you want to see some icons that we designed you should see Mobile and Desktop Switching Icons
(http://www.mobiforge.com/designers/page/mobile-and-desktop-switching-icons) and I also recommend you
read A Very Modern Mobile Switching Algorithm - Part I (http://mobiforge.com/designing/story/a-very-
modern-mobile-switching-algorithm-part-i) and Part II (http://mobiforge.com/designing/story/a-very-modern-
mobile-switching-algorithm-part-ii) , if you haven't done so, yet.
<?php
f u n c t i o n feed_list($page=1) {
$this->load->library('user_agent');
$this->load->helper('url'); // useful to check, parse and clean URLs
$this->_feed_init(); //Init SimplePie
Since this is a mobile device and we have limited to 5 items per page, let's manage pagination and get some
details about the RSS feed so that we can display them on the final page:
mobiforge.com/…/a-simple-mobile-site-… 4/7
12/4/2010 A Simple Mobile Site with CodeIgniter |…
}
$data['start'] = $offset;
$data['length'] = $amount;
In order to be able to use nice URLs such as /feed/page/2 we need to create a function, in fact each
element of the URL is passed to the code and automatically turned into a specific call, the feed calls the
class, page is the function and 2 is the parameter. See on the CodeIgniter site to learn how to pass further
parameters or how you can call classes and methods in sub-directories.
/*
* Helper function to make the URLs nicer. Get the $page number and pass to feed_list
*/
f u n c t i o n page($page=1) {
// make sure $page IS an integer and 1 or more
$page = i n t v a l ( ht t p :/ / ww w . ph p .n e t /i n t va l ) ($page);
if (i
i n t v a l ( ht t p :/ / ww w . ph p . ne t /i n t va l ) ($page)<=1) {$page=1;}
$this->feed_list($page);
}
And then...
f u n c t i o n _get_items($offset, $amount) {
$blog_items = $this->simplepie->get_items($offset, $amount); //load $amount RSS items starting from $offset
$count = 0;
//Loop through the RSS items to create a nice associative array for display in a view
foreach($blog_items as $item) {
$data['blog_items'][$count]['title'] = $item->get_title();
$data['blog_items'][$count]['author'] = $item->get_author()->get_name();
$data['blog_items'][$count]['local_date'] = d a t e ( h tt p : // w ww . p hp . n et / da t e ) ("Y-m-d", s t r t o t i m e
if ($this->agent->is_mobile() || $this->agent->is_robot()) {
// if it's a mobile or a robot replace .com with .mobi so that clicking on "Read more" will go to the mobile site
$data['blog_items'][$count]['permalink'] = s t r _ r e p l a c e ( ht t p: / / ww w . ph p .n e t /s t r_ r e pl a c e)
$data['blog_items'][$count]['content'] = s t r _ r e p l a c e ( h t tp : // w w w. p h p. n et / s tr _ re p l ac e ) (
} else {
$data['blog_items'][$count]['permalink'] = $item->get_permalink();
$data['blog_items'][$count]['content'] = $item->get_content();
}
$count++;
}
return $data;
}
Notice how I "hacked" the permalink so that it points to the .mobi instead of the .com. I know that mobiForge
has specific presentation for .mobi domain that is mobile-friendly (of course!) and even if the User_Agent
class of CodeIgniter was wrong, it would still offer to switch to a desktop presentation, so this is the safest
approach.
<p>Welcome to a mobile-friendly version of the mobiForge RSS. You are seeing page < ? p h p e c h o ( ht t p :/ / ww w . ph p .n e t /e c h o)
< ? p h p if ($is_mobile === f a l s e ): ? >
<!-- this is not a mobile device -->
<p>You requested the mobile site, but you appear to have a desktop browser.
<img src="<?php echo $alternate_icon ?>" alt="desktop icon"/><a href="<?php echo "$alternate_url" ?>"
< ? p h p endif; ? >
<div c l a s s ="articles">
< ? p h p foreach($blog_items as $item) : ? >
<h1><a href="<?php echo $item['permalink'] ?>"><?php e c h o ( ht t p :/ / ww w . ph p .n e t /e c h o) $item['title'
<p><em>Author: < ? p h p e c h o ( ht t p: / / ww w . ph p .n e t /e c ho ) $item['author'] ? > On < ? p h p e c h o ( h tt p : // w ww . p hp . n et / ec h o )
<p><?php e c h o ( h tt p :/ / w ww . ph p . ne t / ec h o) $item['content'] ?></p>
< ? p h p endforeach; ? >
</div>
<div c l a s s ="page_links">
< ? p h p if ($total_pages > 1): ? >
<p>Go to page:<br />
< ? p h p if ($current_page > 1 ): ? >
<a href="1">First</a>
mobiforge.com/…/a-simple-mobile-site-… 5/7
12/4/2010 A Simple Mobile Site with CodeIgniter |…
< ? p h p endif; ? >
< ? p h p if ($prev_page !== f a l s e ): ? >
<a href="<?php echo $prev_page ?>"><</a>
< ? p h p endif; ? >
< ? p h p if ($next_page !== f a l s e ): ? >
<a href="<?php echo $next_page ?>">></a>
< ? p h p endif; ? >
< ? p h p if ($last_page !== f a l s e ): ? >
<a href="<?php echo $last_page ?>">Last</a>
< ? p h p endif; ? >
</p>
< ? p h p endif; ? >
</div>
< ? p h p if ($is_mobile === t r u e ): ? >
<div c l a s s ="mobile_switch">
<!-- this is a mobile device, but we still offer to the user the opportunity to switch -->
<p><img src="<?php echo $alternate_icon ?>" alt="desktop icon"/><a href="<?php echo "$alternate_url
</div>
< ? p h p endif; ? >
</body>
</html>
In this final step, as I said, I used the existing User_Agent class; it's based on
simple keywords, but this makes it good enough to detect standard mobile
devices, desktop browsers and even robots. Being based on simple text it's
quite easy to extend: if a new super-cool device comes out, you can add it
straight (or better extend the basic class with your own). If you look at the class
you will noticed that the iPhone and the iPod touch have special entries and it is
as easy as adding a key and a value to an array.
I also tried to keep the links to jump to the other pages as small as possible, but
you it is up to you what you want to provide to your users, for example for an
RSS feed I would be surprised if visitors went beyond the second page.
If the browser is not mobile I provided the link to switch up at the top, well visible, while if it's mobile I provided
it down at the bottom, just to make sure it would be available as an option, but not eating up useful space.
For this article I did not want to go into the trouble of the interstitial page as per James' tutorial, but this
should not stop you from aiming to perfection!
Conclusions
CodeIgniter is a very good framework to get started, the learning curve is really
low and provides all the basic functions you will need and that are needed in
modern web development such as nice URLs, templating system and of
course database abstraction, etc. The pre-installer User_Agent class is very
basic, but provides the minimum requirements to make a basic detection and it
can be VERY EASILY enhanced and customised. All in all it's a perfect
compendium to get started with a mobile site very quickly.
Make sure you download the attached package to get the final files, also
referencing our mobiForge mobile-specific CSS.
mobiforge.com/…/a-simple-mobile-site-… 6/7
12/4/2010 A Simple Mobile Site with CodeIgniter |…
syntax.php)
A Very Modern Mobile Switching Algorithm - Part I (http://mobiforge.com/designing/story/a-very-modern-
mobile-switching-algorithm-part-i)
and Part II (http://mobiforge.com/designing/story/a-very-modern-mobile-switching-algorithm-part-ii)
Attachment Size
RELATED
TAGS:
Andrea Trasatti is Director of Technology Strategy at dotMobi where among the other things he is leading the
design and development of DeviceAtlas (http://deviceatlas.com ) .
Prior to this Andrea has been a system administrator and a software developer for more than 10 years and a
consultant for more than 5 years.
In Italy Andrea started developing the first WAP sites in late 1999 when the first WAP phone was about to be
released. Since then he has been developing some of the most innovative services for mobile devices.
Andrea has also actively participated to W3C work first as an invited expert and later as author of one of the
documents that was published by the Device Descriptions Working Group.
mobiforge.com/…/a-simple-mobile-site-… 7/7