CodeIgniter MVC – How To Display Single Database Value In View

by Liz Jamieson on April 17, 2008

in Code Igniter

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

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 : <%=$londontours%>
  • Digg
  • StumbleUpon
  • Technorati
  • del.icio.us
  • Blogosphere News
  • Facebook
  • Twitter

{ 3 trackbacks }

Some CodeIgniter Tutorial links « Afruj’s Weblog
May 2, 2008 at 3:49 am
CodeIgniter Tutorial Links « Brandontruong’s Weblog
June 27, 2008 at 7:27 am
CodeIgniter Tutorial Collection
February 13, 2009 at 6:36 pm

{ 5 comments… read them below or add one }

lkanan January 2, 2010 at 7:55 pm

Great and simple, thanks!

Emil Bryggare January 1, 2010 at 4:37 pm

Thanks.

Good and simple explanation.

renu June 15, 2009 at 11:30 am

thx..

Muhammad Babsail March 18, 2009 at 4:20 am

Good work…

tamphong October 10, 2008 at 3:55 am

Thanks, finally.

Leave a Comment

CommentLuv Enabled

Previous post:

Next post: