Search code examples
phpkohana

display result from a query


I'm learning kohana 3.3. I'm working on the model part. Basic query. But i don't know how to display the results i got from a query.

Here's the model.

APPPATH/classes/model/people.php

class Model_People extends Model {

    public function show_data() {
        $query = DB::query(Database::SELECT, 'SELECT * FROM people');
        return $query;
    }

}

APPPATH/classes/controller/people.php

class Controller_People extends Controller {

    public function action_index() {
        $model = Model::factory('people');
        $view = View::factory('base_template');
        $model->user = $model->show_data();
        $this->response->body($view);
    }

}

APPPATH/views/base_template.php

<?php 
    foreach($user as $row) {
        echo "<h2>".$row['Name']."</h2>";
    } 
?>

I don't want to use ORM I'm using QUERY BUILDER. When I run the code it says variable user not defined. How do I display the results correctly? thanks.


Solution

  • Try this

    class Controller_People extends Controller {
    
        public function action_index() {
            $model = Model::factory('people');
    
            $view = View::factory('base_template');
            $view->user = $model->show_data();
    
            $this->response->body($view);
        }
    }
    

    then loop in view

    <?php 
        foreach($user as $row) :
            echo "<h2>".$row['Name']."</h2>";
        endforeach; 
    ?>