开发者

Passing a value from a model to a view in Kohana 3

I have a model that fetches from my database a list that I want to be able to show in my view.

The model looks like this:

Class Model_services extends Model
{
    public function get_services_list() {
    $result = DB::select('*')->from('services')
            ->execute()
            ->as_array();
            return $result;
    }
}

My controller looks something like this:

public function action_index()
{
    $this->template->title    = "services";
    $this->template->header   = View::factory('header'); 
    $services                 = Model::factory('services');
    $this->templat开发者_JAVA百科e->content  = View::factory('services')
                                      ->bind('result',$result)
    $this->template->footer   = View::factory('footer');         
 }

How do I render at the view the variables from the model?


You haven't actually called the model method or passed the variable from your controller class to the view.

Change this:

$services = Model::factory('services');
$this->template->content = View::factory('services')
                                  ->bind('result',$result);

To this:

$services = Model::factory('services')->get_services_list();
$this->template->content = View::factory('services')
                                  ->bind('result', $services);

The changes here are:

  • calling the method used to extract the relevant rows from the database.
  • using the $services variable and binding it to $result which you'll be using in the view.

In your view you'll then be able to extract the values when you reference the $result variable.

To see what you've got from the model, test this in your view:

echo Debug::vars($result);


In services.php use code as below ;)

foreach($result as $item) {
    echo Debug::vars($item);
    // print_r($item); //alternatively you can try this also, if Debug::vars() causes pain
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜