Welcome Guest, Not a member yet? Register   Sign In
How to load data from a view file?
#1

[eluser]benners[/eluser]
I have a template.php View file and if $side_content is not set I want to load default content but this content requires a call to a modal to get the data to build it. How can a I call or load this from within the template.php View?

Code:
<?php if (isset($side_content)) {
$this->load->view($side_content);
} else {
// Get side panel data
$side_quiz_gallery = $this->quiz_model->get_live_quizes();
$data['side_quiz_gallery'] = $side_quiz_gallery;
$data['side_content'] = 'side_quiz_gallery';
} ?>
#2

[eluser]xtremer360[/eluser]
Inside your view file just call it with the $side_content variable.
#3

[eluser]TWP Marketing[/eluser]
[quote author="benners" date="1333561448"]I have a template.php View file and if $side_content is not set I want to load default content but this content requires a call to a modal to get the data to build it. How can a I call or load this from within the template.php View?

Code:
<?php if (isset($side_content)) {
$this->load->view($side_content);
} else {
// Get side panel data
$side_quiz_gallery = $this->quiz_model->get_live_quizes();
$data['side_quiz_gallery'] = $side_quiz_gallery;
$data['side_content'] = 'side_quiz_gallery';
} ?>
[/quote]

Instead, pass the data to the view using the $data array. I assume the view file is named 'template'.
Code:
<?php
$data = array(); // declare array (may not be needed if already declared
if (isset($side_content)) {
// use previously defined content
$data['side_content'] = $side_content;
} else {
// Get default side panel data
$side_quiz_gallery = $this->quiz_model->get_live_quizes();
$data['side_quiz_gallery'] = $side_quiz_gallery;
$data['side_content'] = 'side_quiz_gallery';
}
$this->load_view('template', $data); // load the view with data in array
?>
#4

[eluser]benners[/eluser]
Sorry but I'm just not getting it.

The data needed for the View 'side_quiz_gallery' is collected from a Model. If I call it from a Controller it's fine e.g.

Code:
// Get side panel data
$side_quiz_gallery = $this->quiz_model->get_live_quizes();
$data['side_quiz_gallery'] = $side_quiz_gallery;
$data['side_content'] = 'side_quiz_gallery';

$this->load->view('includes/template', $data);

...but how do I call if from within the view if $side_content is not set?
#5

[eluser]Unknown[/eluser]
After $data['side_content'] is passed from the controller to a view, it strips $data[''] portion of it away and it simply becomes $side_content. The rest is essentially thrown away.
#6

[eluser]benners[/eluser]
Yes I realise that. I have no problems passing the data from the Controller but is there a way to get it from within the View?
#7

[eluser]benners[/eluser]
OK, I've found this loads a model in a view and works for my needs.

Code:
<?php if (isset($side_content)) {
$this->load->view($side_content);
} else {
$this->load->model('quiz_model');
$data['side_quiz_gallery'] = quiz_model::get_live_quizes();
$this->load->view('side_quiz_gallery', $data);
} ?>

I realise loading Models in Views is not recommended but most, but I suggest it's OK when I want to load default content if the variable is not set. It saves me adding extra code all over my Controllers.
#8

[eluser]Samus[/eluser]
[quote author="benners" date="1333626065"]OK, I've found this loads a model in a view and works for my needs.

Code:
<?php if (isset($side_content)) {
$this->load->view($side_content);
} else {
$this->load->model('quiz_model');
$data['side_quiz_gallery'] = quiz_model::get_live_quizes();
$this->load->view('side_quiz_gallery', $data);
} ?>

I realise loading Models in Views is not recommended but most, but I suggest it's OK when I want to load default content if the variable is not set. It saves me adding extra code all over my Controllers.
[/quote]
You could simply enough do that from within your controller.
#9

[eluser]benners[/eluser]
Yes but I have 15 Controllers and probably call the template View file from 10 different functions in each.
#10

[eluser]TWP Marketing[/eluser]
[quote author="TWP Marketing" date="1333566485"][quote author="benners" date="1333561448"]I have a template.php View file and if $side_content is not set I want to load default content but this content requires a call to a modal to get the data to build it. How can a I call or load this from within the template.php View?

Code:
<?php if (isset($side_content)) {
$this->load->view($side_content);
} else {
// Get side panel data
$side_quiz_gallery = $this->quiz_model->get_live_quizes();
$data['side_quiz_gallery'] = $side_quiz_gallery;
$data['side_content'] = 'side_quiz_gallery';
} ?>
[/quote]

Instead, pass the data to the view using the $data array. I assume the view file is named 'template'.

IN THE CONTROLLER:
Code:
<?php
$data = array(); // declare array (may not be needed if already declared
if ( isset( $side_content ) ) {
// use previously defined content
$data['side_content'] = $side_content; // Is this a string, a column title or is it html code?
$data['side_quiz_gallery'] = ""; // empty string since there is no quiz by default
} else {
// Get default side panel data
$data['side_quiz_gallery'] = $this->quiz_model->get_live_quizes(); // Does the model output HTML code, containing the quiz itself?
$data['side_content'] = 'side_quiz_gallery'; // simple string. Is this the column title?
}
$this->load_view('template', $data); // load the view with data array
?>
[/quote]

@benners,
The code above should run in the controller, NOT in the view.
It creates the values to be displayed and then passes them to the view.
In the view, just echo the two vars; $side_content and $side_quiz_gallery, at the appropriate locations
IN THE VIEW:
Code:
...
<?php echo $side_content;?>
...
<?php echo $side_quiz_gallery;?>
...
This is good MVC practice. Massage your data in the controller. Only content is sent to the view.

If you see a need to massage the same data in each of your controllers, move that code to a model method and avoid repeating it.




Theme © iAndrew 2016 - Forum software by © MyBB