views:

77

answers:

3

Hi, First off I am pretty new to this whole thing so feel free to tell me to bugger off.

I am trying to load the views for a set of 'modules' for a user who has selected any number of available 'modules'. I can get the name of the module (or any column) from the database and load->view($name . '_view'); but can't seem to figure a way to load the data for the view based on the 'module' name.

//Loads the rows (selected modules) I need for this user into an array
$modules['modulearr'] = $this->module_model->getModuleUser();

    for($i = 0; $i < count($modules['modulearr']); $i++){ 

            //Get the variable from the array
            $name = $modules['modulearr'][$i]->mod_name;

             //The below works.
            $this->load->view($name.'_view');

            //The below would not work. (this is the crux of my problem)
            $data = $this->$name.'_model'->get();
            $this->load->view($name.'_view', $data);
    }

There is also an issue with loading the models in the controller based on the fact I can't change $this->load->THIS_PART dynamically.

I am new to everything so there may be a basic concept I am not grasping, If you could point me in the right direction that would be awesome. I could do a whole bunch of if statements but that seems messy, surely there is a better way.

Thanks.

A: 

maybe you wanted

$data = $this->$name.'_model'->get();

you forgot to concatenate the strings

Galen
Ah, my bad, I missed that in the question, it still produces a parse error with a concatenated string.
Allansideas
A: 

but can't seem to figure a way to load the data for the view based on the 'module' name.

The module name seems to be defined from the line

$name = $modules['modulearr'][$i]->mod_name;

if this works...

$this->load->view($name.'_view');

maybe you want to do this?

$data = $name.'_model'->get();

instead of $this->name ?

If that doesn't work (i don't really know what you have going on) try echoing $this->name and making sure the output makes sense attached to '_model'

Galen
A: 

It was a capitalization issue: The fields from the database (in array form)

$name = $modules['modulearr'][$i]->mod_name;

were sometimes in capitals. I fixed it by using

$name = strtolower($name);  
$nameMod = $name.'_model';

then

//it doesn't seem to like combinations of things where nameMod is below.
$data[$name.'_result'] = $this->$nameMod->get() 
$this->load->view($name.'_view', $data);

I don't know why it originally worked in the view loading part and not the loading data by calling model function part, but it does now. I am using it for a main landing (after sign up or login) page function that selects and displays the modules added by each user to their profile, I am probably going about this in a completely gammy way, but I am learning heaps by making mistakes, Thanks for the help answerers you definitely put me on the right track

Allansideas