开发者

How can I call a controller method in a view or another controller?

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();
    }

 开发者_C百科   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?


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">


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!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜