CodeIgniter MVC – How To Display Single Database Value In View

How To Display A Singleton Select Value in CodeIgniter

When you are new to MVC it is not always obvious how to get data out of the database and into a view. This example shows how to display the result of a singleton select – i.e a query that always returns one row, and in this case, only one value.

The Model

class Tourdata extends Model {
function Tourdata(){
parent::Model();
}
function counttours($tourcode){
    $query = $this->db->query("SELECT COUNT(*) tourcount
      FROM tourdate WHERE tourcode = '$tourcode'");
    return $query->row()->tourcount;
}
}

The Controller

class Publicuser extends Controller {
var $data;
function Publicuser(){
    parent::Controller();
    $this->load->database();
    $this->load->model('tourdata');
}
function index(){
    $this->data['londontours'] = $this->tourdata->counttours('LONDON');
    $this->load->view('publicuser/viewindex', $this->data);
}

The View

Number Tours This Year :

Code Igniter – The MVC Model and Me

It’s early days, but at the moment I think I made the right decision to choose Code Igniter.

The forum on the CI site is friendly and very helpful. The site and the people involved get straight to the point. Everyone says the CI documentation is excellent – here I beg to differ a little. I find it clearly written but sparse. But as I become more proficient I suspect I will start to appreciate that aspect of it. I have also found that code examples are limited (unless they are lying around somewhere and I just haven’t found them).

I’ve spent some time lurking in the CI forum trying to map my confusions to those of people that went before me. I have had limited success retro-fitting my questions to existing questions but I feel this learning curve is necessary to avoid asking completely stupid questions on the forum myself. I figure that I am having problems getting started because I have never done any real OO programming, nor had I had any MVC before last week.

. . . . . .

I started writing this post a couple of days ago, and now I feel slightly less confused as I am starting to pick up the basics just by reading the forums.

What follows sums up how I feel about Code Igniter at the moment.

Grateful - I am grateful that someone (Rick Ellis) decided to write CI, making it as loosely coupled as it is, thus still leaving the coder some freedom to make decisions, and to make it as nonsense-free as he has.

Happy - I am delirious that Code Igniter took about 25 seconds to install with no edits to php.ini, no DLLs to download, no services to start, stop or resurrect, no digging around in Google attempting to track down exotic installation problems.

Excited - I am looking forward to having CI drag me into MVC methodology, however loosely.

Choosing a PHP Framework – symfony, CakePHP, Code Igniter

Which PHP Framework?

I code PHP flat – I mean by that that every time I write an application I start from scratch and this is now becoming tiresome, especially the database access. Now I do have some common functions that I re-use but I can’t help but notice that other PHP coders out there, especially with the advent of PHP 5, are using one of the many frameworks available. But which one for me? Having read around the web it seems there is no best one – it depends on what you are already good at, what you need, how complex your application etc etc.

CakePHP, CodeIgnitor and symfony all caught my eye for various reasons. I liked the look of CodeIgnitor as it looked easy, CakePHP because so many people seem to like it, and symfony because it has been adopted by some of the the most prestigious applications on the web (read delicious). To help decide a good place to start is this page listing all the major features of each PHP framework.

The important features for me were

  • fast execution
  • not to force the use of templating (I hate Smarty and the like)
  • help with AJAX (yuk Javascript)
  • easy to use with short learning curve
  • great documentation

That left me deciding to look at symfony.

Now I develop on Windows (Vista on my laptop and XP on my desk), and host on Linux. But the symfony website does not appear to address the difficulties if any, of installing on Vista. There is a link out to external blog where others have advice on setting up on Windows Vista, but that was all I could find. But after a day just trying to get the test environment running in symfony I was completely disheartened. It is really far too involved and too complicated.

What does a lone developer (who already has to be a jack of all trades – web designer, database designer and coder, artist, coder), need with a framework that requires a PhD in good luck to get going? I went through all that manoeuvring just to get as far as the test installation (sandbox). What was the real installation going to be like?

Already feeling jaded, I move onto the CakePHP web site when cute nomenclatures like CakeForge, the Bakery and numerous plays on the cookery theme, start to get on my nerves before I have read a single piece of documentation. OK – that’s not fair. The web site is user-friendly and CakePHP is clearly a mature product with a big following, but I want a product that quickly gets to the point. Time to go. I hear Code Igniter calling.