views:

53

answers:

2

I have main controller, that print main page.

<?

class Main extends Controller {

    function Main()
    {
        parent::Controller();

        $this -> load -> helper('date');
    }

    function index()
    {
        $this -> load -> view('header');
        $this -> load -> view('main');
        $this -> load -> view('footer');
    }
}

?>

And I have controller of articles, that print 6 last articles.

<?php

class Articles extends Controller
{
    function Articles()
    {
        parent::Controller();
    }

    function top()
    {
        $this -> db -> limit(0, 6);
        $query = $this -> db -> get('articles');

        $data['articles'] = $query -> result();

        $this -> load -> view('articles-top', $data);
    }

?>

Header view looks like this:

<html>
    <head>
    ...
    </head>
    <body>

        <div id="last-articles">
            <!-- Here I want print last 6 articles -->
        </div>

        <div id="content">

How can I print last articles in header view?

A: 

You are doing it all wrong.

Your controllers need to have functions named like this...

public function action_youractionname($value='')
{
   $array_of_data_for_view = array( 'data' => $some values );
   echo View::factory( 'viewfilename', $array_of_data_for_view )->render();
}

Your view can use the $data variable.

Hope this helps, but I would suggest this site.. http://kerkness.ca/wiki/doku.php

It's been invaluable to me.

good luck!

brennanag
A: 

The array keys in your $data array become variables in the view.

The Code Igniter User Guide is very useful.

You can do something like this in your view:

<html>
<head>
...
</head>
<body>

    <div id="last-articles">
        <?php 
            foreach($articles as $article)
            {
                echo $article.title;    //obviously these would be the real fields
                echo $article.content;  //from your article table.
            }
        ?>
    </div>

    <div id="content">
msergeant