Showing all session data at once? - codeigniter

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>'; ?>

Related

Unable to update session variable in CodeIgniter

I am in trouble using the CodeIgniter 2.2.6 session variables. I have a view, where some data is inputed by the user. Then, the user can press a submit button and this data goes to a web service, is processed and returned to the same view (using the controller and its model).
Bellow is the view code (v_index), where I save the data in the CodeIgniter session variables.
<?php
// loads the variables that will be used in print
$this->load->library('session');
$array_items = array(
'numberVehiclesUsed' => $numberVehiclesUsed,
'shortestRoute' => $shortestRoute,
'VRPSolution' => $VRPSolution,
);
$this->session->set_userdata($array_items);
?>
So, when I run the code bellow, I can see all the session data and everything is ok all the times. My application calls the web server, get the return and saves in the session variables everytime.
<?php
echo "<pre>";
print_r($this->session->all_userdata());
echo "</pre>";
?>
So, I have another screen where I want to use the session data, called "Imprimir" (it means print). The controller code is bellow. The print_r is the for debuging proposes, offcourse.
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class C_imprimir extends CI_Controller
{
function __construct()
{
parent::__construct();
//load the session library
$this->load->library('session');
}
public function index()
{
$dados['numberVehiclesUsed'] = $this->session->userdata('numberVehiclesUsed');
$dados['shortestRoute'] = $this->session->userdata('shortestRoute');
$dados['VRPSolution'] = $this->session->userdata('VRPSolution');
echo "<pre>";
print_r($this->session->all_userdata());
echo "</pre>";
$this->load->view('v_imprimir', $dados);
}
}
?>
When I run the code for the first time, it saves the data in the session variables and I am able to load in the controller "Imprimir" as the code above. The print_r shows that all the data is there and ok.
The problem is when I run the first view (v_index) again with new data, the session variables are updated but, the controller "Imprimir" loads only the first (and old) data, and never updates it.
I have no idea what I aḿ doing wrong. The others Stackoverflow questions about problems with CodeIgniter session variables did not help me.
Any suggestion?

How do i call a view from within a view in 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 ?

How to display error or success message in a template file?

I want to show message on some condition in a template file(custom module template file).I am having following code.
<?php
if(count($collection)): ?>
<?php foreach($collection as $coll): ?>
some calculations
<?php endforeach; ?>
<?php else: ?>
<?php $message = $this->__('There is no data available'); ?>
<?php echo Mage::getSingleton('core/session')->addNotice($message);?>
<?php endif;?>
But this is not working properly. The message is displayed on other pages not on the same page.
If you really need to implement that right in the template, you may use the code below:
<?php echo $this->getLayout()->createBlock('core/messages')->addNotice('My Message')->toHtml(); ?>
But the solution described by Amit Bera sounds like a better way to resolve it.
Muk,According to your code
Mage::getSingleton('core/session')->addNotice($message);
add an notice to magento.This is code set notice to session and which is reflecting on page refresh according php session ,a session variable set value is reflecting after page refresh.
If you already added this notice on other page before came to your file then ,you need to add core/session to custom module template file controllers file.
Code is $this->_initLayoutMessages('core/session');
In controller you need below code in controller.
/* load the layout from xml */
$this->loadLayout();
$this->_initLayoutMessages('core/session');
/* rendering layout */
$this->renderLayout();
Read more at inchoo blog
I found that $this->loadLayout() is what reads and clears messages from the session, therefore if you add messages before calling $this->loadLayout() then those messages should be displayed on the current page.
Example:
public function chooseFileAction() {
// a quick and dirty way to find the larger out of post_max_size and upload_max_filesize
$post_max_size = ini_get('post_max_size'); $post_max_size_int = str_replace('K', '000', str_replace('M', '000000', str_replace('G', '000000000', $post_max_size)));
$upload_max_filesize = ini_get('upload_max_filesize'); $upload_max_filesize_int = str_replace('K', '000', str_replace('M', '000000', str_replace('G', '000000000', $upload_max_filesize)));
$maxsize = $post_max_size_int < $upload_max_filesize_int ? $post_max_size : $upload_max_filesize;
// display max file size to user in a message box
$msg = 'Max file size: ' . $maxsize;
Mage::getSingleton('core/session')->addNotice($msg);
$this->loadLayout();
$this->_title($this->__('Catalog'))->_title($this->__('File Upload'));
$this->_setActiveMenu('catalog/customfileupload');
$block = $this->getLayout()->createBlock('customfileupload/adminhtml_customfileupload_choosefile');
$this->_addContent($block);
$this->renderLayout();
}
Caveat: this is tested in Magento 1.9, not 1.7.

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

Resources