lets say that i hv this view (main)
<body>
lorem epsim
<div table></div>
lorem epsim
</body>
in controller control1.php i do
$this->load->view('header');
$this->load->view('main',$data);
$this->load->view('footer');
Now i need to load content of div=table from another view (tbl.php),which is called from another controller
control2.php
function load_table(){
$data['x']=1;
$this->load->view('tbl.php',$data);
}
tbl.php view
<ul>$x</ul>
how can i do that ?
i tried to load controler 2 from controller 1 and assign the function load_table to variable and pass that to main view, but it didnt work cuz load->view is executed instead of saving output to variable..
Reason:
i need to do this is that tbl.php view is a complex table that i need to refresh and load via ajax calls, so i need it to be on different view alone
so can some one explain to me how can i work this out ?
You can't call one controller method from another, separate controller. You can, however, get the output of the table view and use that:
// main.php
<body>
lorem epsim
<div table><?php echo $table_content; ?></div>
lorem epsim
</body>
.
// control1.php
$table_data['x'] = 1;
$data['table_content'] = $this->load->view('tbl.php', $table_data, TRUE);
$this->load->view('header');
$this->load->view('main',$data);
$this->load->view('footer');
So, you get the data to pass to the tbl.php view and pass that to the load->view method - also passing TRUE as the third parameter - which will return the contents of that view as a string (instead of outputting that to the browser). Now, you have a $data variable to pass to the main view with the table html included and you can just echo that out in the main view.
How you get the $data['table_content'] data from the view is up to you. You can create another controller method inside control1.php, you can create a helper file that can load the view into a string and return that, etc.
Maybe you can create a view with the table code within and then you can do inside your div for ajax
<div id="for_ajax">
<?php $this->load->view('table'); ?>
</div>
I've similar needs but mine its like a comments wall for issues.
Inside a view use following in a php block
$CI = &get_instance();
$CI->load->view('view_name');
Related
This code works well in the "images" view under images_model:
<?php if($images_model):?>
<?php foreach($images_model as $images):?>
<div class="col-sm-4">
<div class="well">
<img src="<?php echo base_url()."uploads/".$images->name;?>" alt="" class="img-thumbnail">
The code in images_model is the following:
function save_image($data){
$this->db->insert('images', $data);
}
When I try to use the same code in the "wall" view (main_controller) I get the severity notice error saying "images_model" is undefined; even though I load the images_model in the main_controller or auto-load the both models:
$autoload['model'] = array('main_model', 'images_model');
I originally questioned the "foreach" code in the "images" view, but I thought if it works in one view shouldn't it also work in another if I load the related model? It just doesn't seem to be reading the images_model.
I'm just getting to know codeigniter a bit and would appreciate any feedback.
Are you passing the $images_model variable to your view from your main_controller ?
// Set your variable
$data['images_model'] = $images_model;
// Pass variable to view
$this->load->view('main', $data);
You typically shouldn't have access to a model directly in the view (following MVC principals). Your controller should get what it wants from the model, then pass it to the view.
Is there a way to disable layout for certain controller method?
Im using something like $this->layout = null ,yet it still render the layout
The view im rendering obviously have a layout associate with it, i just wonder is it possbile to disable the layout from within controller method, without need to modify the blade file itself
Here is the controller:
class PurchaserController extends \BaseController
{
public function index()
{
$this->layout = null;
return View::make('purchasers.index');
}
}
The view:
#extends('layouts.master')
#section('content')
Content
#stop
Im using Laravel 4
Just remove
#extends('layouts.master')
from your view. That will prevent the view from loading.
Also - if you are using the #extends - then you dont actually need $this->layout() in your controller at all
Edit:
" i just wonder is it possbile to disable the layout from within controller method, without need to modify the blade file itself"
The idea is you do it either entirely from the controller, or entirely from the blade file. Not both together.
I am using CodeIgniter and I have a controller called contact which passes data to it's view which I have loaded in via the header so that it's on every page, however the data I pass to this view doesn't appear. It only appears if I go straight to the view via the url and I can only presume that this is caused because it is pulled in via another view which has a different controller? Is that right, if so how do I fix it?
For example:
<body>
<div id="header">
<h1>Hello there!</h1>
<?php echo $this->load->view('contact'); ?>
</div>
Are you parsing any data to the 'contact' view? If so, how?
CodeIgniter Userguide - Loading a View
function contact()
{
$data['someinfo'] = "Some Info";
$this->load->view('contact', $data);
}
the problem is calling the 'contact' view from another view doesn't mean the 'contact' controller is being called... that is why you are unable to access the data passed from the 'contact' controller!
To call controller from views you will need https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc
Ive been working with CI and I saw on the website of CI you can load a view as a variable part of the data you send to the "main" view, so, according the site (that says a lot of things, and many are not like they say ...ej pagination and others) i did something like this
$data['menu'] = $this->load->view('menu');
$this->load->view ('home',data);
the result of this is that I get an echo of the menu in the top of the site (before starts my body and all) and where should be its nothing, like if were printed before everything... I have no idea honestly of this problem, did anybody had the same problem before?
Two ways of doing this:
Load it in advance (like you're doing) and pass to the other view
<?php
// the "TRUE" argument tells it to return the content, rather than display it immediately
$data['menu'] = $this->load->view('menu', NULL, TRUE);
$this->load->view ('home', $data);
Load a view "from within" a view:
<?php
// put this in the controller
$this->load->view('home');
// put this in /application/views/home.php
$this->view('menu');
echo 'Other home content';
Create a helper function
function loadView($view,$data = null){
$CI = get_instance();
return $CI->load->view($view,$data);
}
Load the helper in the controller, then use the function in your view to load another one.
<?php
...
echo loadView('secondView',$data); // $data array
...
?>
I'm trying to display an image but is dependent on a dropdown list in Yii. I can get the image from the database and display it, but how to do it dynamically depending on the choice from the dropdown?
Here is the reference: http://www.yiiframework.com/wiki/24/creating-a-dependent-dropdown#hh0 but, let me show you how to do it.
First all all, we need a div where the image will be displayed; I'll create one whose id will be 'img'. Then, the ajax request is specified inside the dropdownlist() as follows:
<?php echo $form->labelEx($model,'attribue'); ?>
<?php echo $form->dropDownList($model,'attribute',
array(/*The options in the DropDownList*/),
array(
'ajax'=>array(
'type'=>'POST',
'url'=>CController::createUrl('YourController/actionWhichEchoesTheImage'),
'update'=>'#img',
)));
?>
<div id="img"> // <---- the result of the ajax call will be displayed here
</div>
In the 'url' attribute we specify the function which will be called when the ajax request triggers. In the 'update' attribute we specified the div where will be displayed the result of calling that function (the image).
Finally, we have to declare the action actionWhichEchoesTheImage(). Let's declare it in the current controller. It would look something like this:
public function actionWhichEchoesTheImage()
{
if(isset($_POST['ModelName']['attribute']))
/*Here goes your code to load the image*/
echo CHtml::image(//Check the reference to see how to set this function);
}
Check CHtml::image() here: http://www.yiiframework.com/doc/api/1.1/CHtml/#image-detail