Welcome Guest, Not a member yet? Register   Sign In
loading and using methods on demand
#1

[eluser]Antonio Fernández[/eluser]
Good day to you all,

I'm making a site that have a sidebar and on that sidebar there are some blocks that need to be shown or not depending on the main page we are looking at.

Basically it loads a model and then invoke the show() function for get the contents. This is the code that I'm writing.

Code:
if (array_key_exists('blocks', $data['page_data'])) {
    $blocks = $data['page_data']['blocks'];
    $this->load->view('sidebar_init');
                
    foreach ($blocks as $block)
    {
        $this->load->model($block);
        $this->$block->show();  // here is the problem, how I can refer to the model?
                                // $block have the model name
    }
    $this->load->view('sidebar_end');
                
} else {
    $this->load->view('sidebar_init');
    $this->load->view('sidebar_end');
}

Are there something like $this->model('name_of_the_model')->function() to work with?

Thanks in advance. I'm sure that someone got a problem similar. Probably it's on the documentation but I haven't found it after several reads.

-- Antonio
#2

[eluser]n0xie[/eluser]
Code:
$this->load->model($block);
        $this->$block->show();
Isn't that working?
#3

[eluser]Antonio Fernández[/eluser]
[quote author="n0xie" date="1269446217"]
Code:
$this->load->model($block);
        $this->$block->show();
Isn't that working?[/quote]

Nope, it drops me this error:

Fatal error: Call to a member function show() on a non-object in
#4

[eluser]n0xie[/eluser]
Weird it works here.

Does $block contain 'modelname' or ' modelname_model' ?

Else maybe try this:
Code:
$this->{$block}->show();
#5

[eluser]Antonio Fernández[/eluser]
Umm, I have tryed enclosing it on {} without results.

This is the definition:

Code:
$data['page_data']['blocks'] = array('m_block_login', 'm_block_stats');


m_block_login and m_block_stats are models on /application/models/
#6

[eluser]Antonio Fernández[/eluser]
[quote author="fernandezvara" date="1269447437"]Umm, I have tryed enclosing it on {} without results.

This is the definition:

Code:
$data['page_data']['blocks'] = array('m_block_login', 'm_block_stats');


m_block_login and m_block_stats are models on /application/models/[/quote]

I have wrote some text before so I force to codeigniter to show the page and then I get this error:

Code:
A PHP Error was encountered
Severity: Notice

Message: Undefined property: Master::$m_block_login

Filename: models/master.php

Line Number: 82

So, it looks like it translates the array value with an $.
#7

[eluser]Antonio Fernández[/eluser]
As workaround I have put the code on show() to the __construct() and there are no need to invoke show after.

Anytime I think it could be useful in a near future, so I will try to investigate it deeper.

Thanks a lot n0xie for your time. If someone sees how to get it working with the other style I will be so gratitude with it.
#8

[eluser]InsiteFX[/eluser]
Take the $ off of $this->block->show();

Enjoy
InsiteFX
#9

[eluser]Antonio Fernández[/eluser]
[quote author="InsiteFX" date="1269458527"]Take the $ off of $this->block->show();

Enjoy
InsiteFX[/quote]

Thanks InsiteFX but if I make that codeigniter looks for the class called 'block' to invoke the method show(), but what I want is to use a model called as the actual value od the $block variable.




Theme © iAndrew 2016 - Forum software by © MyBB