Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
using Model (database) result in View
  • I am a little confused using fuelPHP 1.7.

    The controller

        class Controller_Website extends Controller
        {
            public function action_index()
            {
                // http://fuelphp.com/docs/general/views.html
        
                $data = Website::get_results();

                //var_dump($data) // (data is found here);
        
                $views = array();
                $views['head'] = View::forge('common/head', $data);
                $views['header'] = View::forge('common/header', $data);
                $views['sidebar'] = View::forge('common/sidebar', $data);
                $views['content'] = View::forge('common/content', $data);
                $views['footer'] = View::forge('common/footer', $data);
        
                // return the rendered HTML to the Request
                return View::forge('website', $views)->render();
             }
        }

    The model

        class Website extends \Model
        {
            public static function get_results()
            {
                // Database interactions
                $result = DB::select('menu', 'url', 'title', 'text')
                    ->from('aaa_website')
                    ->where('id', '=', 1035)
                    ->and_where('visible', '1')
                    ->execute();

                return $result;
            }
        }

    All well sofar. Data is queried and found in the controller. What I am trying to accomplish is to use the data in my:

    (nested) view

        <html>
        <head>
            <?php echo $head; ?>
        </head>
        <body>
        <header>
            <div class="container">
                <?php echo $header; ?>
            </div>
        </header>
        <div class="row">
            <div class="container">
                <div class="col-md-4">
                    <?php echo $sidebar; ?>
                </div>
                <div class="col-md-8">
                    <?php echo $content; ?>
                </div>
            </div>
        </div>
        <footer>
            <div class="container">
                <?php echo $footer; ?>
            </div>
        </footer>
        </body>
        </html>

    Head view (nested):

        <title><?php echo $title; ?></title>

    Content view (nested):

        <h1><?php echo $title; ?></h1>
        <div class="welcome_user"><?php echo $text; ?></div>

    And so on.

    The
    variables in the view in this example are not available because they
    are not explicitly set in the controller. Do they have to be set
    explicitly or is passing the data object also possible? If so, how do I
    access this objects data in the right way? FuelPHP is lacking good
    examples here and I am stuck now.

    How do I do it?
  • A View is a separate entity, so nothing from your controller (or anywhere else for that matter) is available in your views unless you pass it.

    You pass $data to your view, and expect to have $title available as a variable in your view. This means $data must be an array, and $data['title'] must be set. Is this the case?

    From what I can see, $data is the result of the DB query, so it's not an array, it's a DB result object, containing zero or more rows.

    So to make your current views work, you need to extract row 1 from the result object, make sure it's an array (like array('title' => 'ttt', 'text' => 'xxx')) and pass that to the views.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion