How do i call a view from within a view in codeigniter? - codeigniter

hi this is my controller :
public function index()
{
$data['menu'] = $this->load->view('frontend/menu/main_menu_view');
$this->load->view('frontend/header/header_view',$data);
$this->load->view('frontend/home/index_view');
}
and this is the code from the index_view view :
echo $menu;
but is not working, what should I do ? thx

The third parameter of $this->load->view() doesn't load the view but returns it as 'data', to do with it what you please, muwahhahah! Sorry.
$data['menu'] = $this->load->view('frontend/menu/main_menu_view' , '', true);
You can still pass $data into that view.
So from inside of
$this->load->view('frontend/header/header_view',$data);
you will echo $menu
Scroll to the bottom of the docs

You can call
$this->load->view('frontend/menu/main_menu_view');
from inside any view file and it will load and display for you.

In the view, you have to write just this :
<?php
$this->view('frontend/menu/main_menu_view');
?>

Don't know if you copied this from your code
or made a typo while typing your question, here, but shouldn't it be
echo $menu;
instead of
echo menu;
?
edit: Also, haven't worked with Codeigniter in a while,
but why pass $data to the header_view and call it from the index_view instead ?

Related

CodeIngiter: Load a view inside of another view

I'm using CodeIgniter. I want to load views inside of other views. How can I do this?
Example:
Let's say I have a "view" called "CommentWall". In CommentWall, I want a bunch of "Comment" views. I use the view for "comment" all over my site!
How can I do this? It seems CodeIgniter only allows me to load views sequentially, which is sort of strange considering I use reusable views INSIDE of other views!
Can I do a $this->load->view('comment'); inside of my view for CommentWall? Or is there some other way to have reusuable views contained inside a view?
You can do it easily, just load the main view, for example CommentWall from the controller
$this->load->view('CommentWall');
To add child views in CommentWall view you can add following line inside your CommentWall view
$this->view('Comment');
For example, if you load CommentWall view from your controller like this
$data['comments'][] = 'Comment one';
$data['comments'][] = 'Comment two';
// load the parrent view
$this->load->view('CommentWall', $data);
Now in the CommentWall (parent view), if you put this
foreach ($comments as $comment) {
$this->view('Comment', array('comment' => $comment));
}
And in your Comment (child view) if you have this
echo $comment . '<br />';
Then you should get output something like this
Comment one
Comment two
Update : Alos, check this answer.
Try
class Main extends CI_Controller {
function __construct()
{
parent::__construct();
$data->comments =$this->load->view('comment');
$this->load->vars($data);
}
And in every view try
echo $comments;
Just load the "Comment" vies as string in controller and pass it to "CommentWall" view.
You can do it like this:
//Controller:
public function load_comment_wall($param) {
$comments_view = ""; //String that holds comment views
//here load the comments for this wall as follows:
//assuming $comment_ids is array of id's of comment to be put in this wall...
foreach($comment_ids as $comment_id) {
$temp = $this->load->view("comment",array('comment_id'=>$comment_id),TRUE); //Setting last parameter to TRUE will returns the view as String
$comments_view = $comment_views.$temp;
}
$data['comments'] = $comments_view;
//load comment wall
$this->load->view('comment_wall',$data);
}
//In Comment wall View, add the following line
echo $comments;

JToolbar::save() redirection

I'm going through the Joomla 2.5 tutorial to build a custom component. Now I'm facing an issue on the redirection after using JToolbar::save() or JToolBarHelper::cancel for that matter. By default Joomla wants to redirect to the default layout (from the edit layout). However I don't want it to do that. I want it to redirect back to another view. In Joomla 1.5 I would have done this through adding the function into the controller - something like
function cancel()
{
//redirects user back to blog homepage with Cancellation Message
$msg = JText::_( 'COM_BLOG_POST_CANCELLED' );
$this->setRedirect( 'index.php?option=com_jjblog&view=jjblog', $msg );
}
Now that works beautifully for the cancel function, however for save this is a much more complex thing. If I want to overwrite the url do I have to redirect the controller to the model and then write in all the code for the model interaction? Because that seems slightly excessive just for a url redirection like you would in Joomla 1.5?
Hope you have added the save toolbar code with the proper controller name like this
JToolBarHelper::save('controllerName.save');
Create a save function in appropriate controller.
Add the task in the form
Finnally make sure you have added form action withthe corresponding component name.
You can try this-
In the controller firstly you call the parent save function than redirect to url.
function save(){
parent::save();
$this->setredirect('index.php?option=com_mycomponent');
}
OK it didn't need to $this->setRedirect at all. Just needed me to change the value to
protected $view_list = 'jjBlog';
which then sets the redirects of everything back to that list view.
Source link for this is here.
Thanks for all the responses though!!
view.html.php
protected function addToolbar ()
{
JRequest::setVar ('hidemainmenu', false);
JToolBarHelper::title (JText::_ ('Configuration'), 'configuration.gif');
JToolBarHelper::save($task = 'save', $alt = 'JTOOLBAR_SAVE');
}
controller.php
public function save()
{
$mainframe = JFactory::getApplication();
$mainframe->enqueueMessage (JText::_ ('COM_SOCIALLOGIN_SETTING_SAVED'));
$this->setRedirect (JRoute::_ ('index.php', false));
}
I think you can use
global $mainframe;
$mainframe->redirect("index.php?option=com_user&task=activate&activation=".$activation);
If you are overriding joomla's default save function in your custom component like
function save( $task = 'CustomSave', $alt = 'Save' ) // or even same name Save
Inside your controller you can use the CustomSave as the task and use $mainframe for redirect.
or
$mainframe = &JFactory::getApplication();
$mainframe->redirect("index.php?option=com_user&task=activate&activation=".$activation);
Hope this may help you..

codeigniter : using flash data but no new page load?

Usually, I just set a $feedback var or array and then check for that to display in my views.
However, it occurred to me I should perhaps use flashdata instead.
The problem is sometimes - for say an edit record form, I may simply want to reload the form and display feedback - not redirect. when i use flashdata, it shows but then it shows on the next request as well.
What would be the best practice to use here?
CodeIgniter supports "flashdata", or session data that will only be available for the next server request, and are then automatically cleared.
u use hidden field for that
I would use the validation errors from the Form validation class and load those directly to the view in its 2nd argument.
$this->form_validation->set_error_delimiters('<p>', '</p>');
$content_data = array();
if (!$this->form_validation->run()) {
$content_data['errors'] = validation_errors();
}
$this->load->view('output_page', $content_data);
Then check in your view whether $errors isset.
Controller:
$data['message'] = 'some message you want to see on the form';
$this->load->view('yourView', $data);
View:
if (isset ($message)) : echo $message; endif;
...

how to load view into another view codeigniter 2.1?

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
...
?>

Showing all session data at once?

I have tried the following but it is giving me errors:
print_r($this->session->userdata());
How can I show all session data in CodeIgniter?
print_r($this->session->userdata);
or
print_r($this->session->all_userdata());
Update:
As of version 3.1.11 the above methods are deprecated. Use the below method to get all session data,
print_r($this->session->userdata());
echo "<pre>";
print_r($this->session->all_userdata());
echo "</pre>";
Display yet formatting then you can view properly.
For print session data you do not need to use print_r() function every time .
If you use it then it will be non-readable format.Data will be looks very dirty.
But if you use my function all you have to do is to use p()-Funtion and pass data into it.
//create new file into application/cms_helper.php and load helper cms into //autoload or on controller
/*Copy Code for p function from here and paste into cms_helper.php in application/helpers folder */
//#parram $data-array,$d-if true then die by default it is false
//#author Your name
function p($data,$d = false){
echo "<pre>";
print_r($data);
echo "</pre>";
if($d == TRUE){
die();
}
}
Just remember to load cms_helper into your project or controller using $this->load->helper('cms'); use bellow code into your controller or model it will works just GREAT.
p($this->session->all_userdata()); // it will apply pre to your sesison data and other array as well
here is code:
<?php echo '<pre>' . print_r($_SESSION, TRUE) . '</pre>'; ?>

Resources