CodeIgniter MVC -How To Display Single Database Value In View

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%>
Written by Liz Jamieson
  • Digg
  • MisterWong
  • Reddit
  • SphereIt
  • StumbleUpon
  • Technorati
  • del.icio.us

Related Posts

Tags: , ,

This entry was posted on Thursday, April 17th, 2008 at 10:49 am and is filed under Code Igniter. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

2 Responses to “CodeIgniter MVC -How To Display Single Database Value In View”

Some CodeIgniter Tutorial links « Afruj’s Weblog May 2nd, 2008 at 3:49 am

[...] How To Display Database Value In View [...]


 

 

Leave a Reply