Welcome Guest, Not a member yet? Register   Sign In
News and statistics in the left column
#1

[eluser]Bikun[/eluser]
Could somebody point me to the solution?

I need to show news and statistics on every page of the web site. In my left_view.php I have the following code:
Code:
<p>
    <strong>News</strong>
</p>
&lt;?php if($news->num_rows > 0):?&gt;
    &lt;?php foreach($news->result() as $row):?&gt;

    <p>
        &lt;?=$row->date_time?&gt; | &lt;?=$row->news?&gt;
    </p>

    &lt;?php endforeach;?&gt;
&lt;?php endif;?&gt;

Where should I make a query to the DB to get news? Since task is very common, I believe there should be some wide known solution for that.
#2

[eluser]Ty Bex[/eluser]
Codeigniter is designed with a Model View Controller approach.

For your instance:

1.) Web request goes to a Controller.
2.) The Conroller would call the Model
3.) The Model will query your data and return the data in an array to the Controller
4.) The Contoller will call the View
5.) The View will display the array data.
#3

[eluser]Bikun[/eluser]
Right. I use that approach for separate pages.

In that case I call left_view.php already from template file, which is called from controller. I could query news and statistics in every function before calling left_view.php, but that would be insane. That's why I'm trying to find a common place where to call staff which will be used commonly on every page of the web site.
#4

[eluser]got 2 doodle[/eluser]
I think what you are looking for is to extend the controller class.

If you look in /system/libraries/Controller.php this is the main controller class.

If you extend the controller in /system/application/libraries/MY_controller.php

Then if you build (extend) all of your controllers based on MY_controller.php then any changes made to MY_controller.php will affect all of your controllers.

Take a look at BackendPro (search the forum) it does this extensively.
Code:
class Site_Controller extends Controller
    {
        var $_container;
        function Site_Controller()
        {
            // Call parent constructor
            parent::Controller();
        
            // Display page debug messages if needed
            /*if ( is_superadmin() AND $this->preference->item('page_debug'))
            {
                $this->output->enable_profiler(TRUE);
            }*/
            
            // Set site meta tags
            //$this->page->set_metatag('name','content',TRUE/FALSE);
            $this->page->set_metatag('content-type','text/html; charset=utf-8',TRUE);
            $this->page->set_metatag('robots','all');
            $this->page->set_metatag('pragma','cache',TRUE);
            
            log_message('debug','Site_Controller Class Initialized');
        }
    }

This does some default stuff..
Quote: class Public_Controller extends Site_Controller
{
function Public_Controller()
{
// Call parent constructor
parent::Site_Controller();........

Quote: class Home extends Public_Controller
{
function Home()
{
parent:Tongueublic_Controller();
}

function index()

And so it goes...
...like an onion.

hope this helps
doodle
#5

[eluser]Bikun[/eluser]
Schema is more or less clear. I made such structure. Now I can query news in the main controller, but how could I show news in a view which is called by a child controller?
#6

[eluser]got 2 doodle[/eluser]
Controller (parent controller)
Code:
// get some news //
    $this->load->model('some_great_news');
// add it to your data array //
    $data['great_news']= $this->some_great_news->get_the_news();

in view
Code:
print_r($great_news);

this should get you going

doodle
#7

[eluser]Bikun[/eluser]
Here is what I have.

MY_Controller.php in an application/libraries
Code:
class SiteController extends Controller {
    var $_container;
    
    function SiteController() {
        // Call parent constructor
        parent::Controller();

        $data['news'] = $this->db->get('news');
        echo $data['news']->num_rows(); // This returns 2 as it should
    }
}

AuthorsPage controller which extends SiteController:
Code:
class AuthorsPage extends SiteController{

    function AuthorsPage(){
        parent::SiteController();

        //$this->load->scaffolding('entries');
        $this->load->helper('url');
        $this->load->helper('form');
    }

    function index(){}

    function authors_page(){        
        /* Make title and header */
        $data['title'] = "Author's page";
        $data['header'] = "Author's page";

        /* Build page structure */
        $data['header_view'] = "header_view";
        $data['left_view'] = "left_view";
        $data['content_view'] = "authors_page_view";
        
        $this->load->view('template', $data);
    }
}

And here is a left_view which is called by child controller:
Code:
<p>
    <strong>News</strong>
</p>
&lt;?php if($news->num_rows > 0):?&gt; - this is a line 44
    &lt;?php foreach($news->result() as $row):?&gt;

    <p>
        &lt;?=$row->date_time?&gt; | &lt;?=$row->news?&gt;
    </p>

    &lt;?php endforeach;?&gt;
&lt;?php endif;?&gt;

And here is a result I get:
Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: news

Filename: views/left_view.php

Line Number: 44
Code:
A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: views/left_view.php

Line Number: 44
#8

[eluser]got 2 doodle[/eluser]
your right it doesn't work.
I really thought it would. You might look into the CI super object
I always just grab data from each controller.

Please post if you find a solution.
doodle.
#9

[eluser]got 2 doodle[/eluser]
try looking into
Code:
$this->load->vars($array)
#10

[eluser]Bikun[/eluser]
Good suggestion. By some reason $this->load->vars($array) didn't want to work, but the following has worked out and solved my issue:

SiteController:
Code:
$GLOBALS['news'] = $this->db->get('news');

View file:
Code:
&lt;?php if($GLOBALS['news']->num_rows > 0):?&gt;
    &lt;?php foreach($GLOBALS['news']->result() as $row):?&gt;

    <p>
        &lt;?=$row->date_time?&gt; | &lt;?=$row->news?&gt;
    </p>

    &lt;?php endforeach;?&gt;
&lt;?php endif;?&gt;

Maybe it's not the most optimal solution, but it works. Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB