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(){
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(){
function index(){
    $this->data['londontours'] = $this->tourdata->counttours('LONDON');
    $this->load->view('publicuser/viewindex', $this->data);

The View

Number Tours This Year : <%=$londontours%>


  1. http://lkanan says

    Great and simple, thanks!

  2. Thanks.

    Good and simple explanation.

  3. http://renu says


  4. http://Muhammad%20Babsail says

    Good work…

  5. http://tamphong says

    Thanks, finally.