session values not passing from one controller to another in code igniter - php

I have stored the session varible in one controller and getting the value in another controller but the value is not passing
here is one controller
function control1 {
$this->session->set_userdata(array(
'value1' => $this->input->post('value1'),
'value2' => $this->input->post('value2'),
);
echo $this->session->userdata('value1'); //it returns value
}
function control2 {
echo $this->session->userdata('value1'); //it returns empty value
}
What may be the reason for this

You should check if the session has been set in the second controller like this:
function control2 {
if (!isset ($this->session->userdata('value1'))){
redirect('control1');
} else {
echo $this->session->userdata('value1'); //it returns empty value
}
If you haven't run through the control1 first, there won't be any session set yet, you see.

$this->session->set_userdata( 'values', array(
'value1' => $this->input->post('value1'),
'value2' => $this->input->post('value2'),
) );
$a = $this->session->userdata('values'); //it returns value
print_r( $a );
Try this code.

How the way you go into control2()? I mean, did you call control1() first to set the sessions value before goes to control2()? If so then the sessions value should be passed.

Related

save data in codeigniter with array

I have an array recieved by the code
$logged_in=$this->session->userdata('logged_in');
In this I have an element gid. I want to set value in this session with gid=2. How can I set this ??
use set_userdata() function as follows
$this->session->set_userdata('gid', 1);
if you want to set more than one value in session, use array method as follows
$session_array = array('gid' => 2, 'name' => 'john') ;
$this->session->set_userdata($session_array);
Using session
$this->load->library('session');
$this->session->set_userdata('gid', 2);
To get session value
echo $this->session->userdata('gid'); //Output is 2
$data=array('user_name'=>$this->input->post('user_name'),
'user_email'=>$this->input->post('user_email'),
'user_password'=>md5($this->input->post('user_password')),
'product'=>implode(",", $this->input->post('product')),
'compney_name'=>$this->input->post('compney_name'),
'category'=>$this->input->post('category'),
'compney_type'=>$this->input->post('compney_type'),
'city'=>$this->input->post('city'),
'state'=>$this->input->post('state'),
'phone'=>$this->input->post('phone'),
'service_id'=>implode(",", $this->input-
>post('service_id')),
'image'=>$this->session->userdata('images_business')
);
$id = $this->session->userdata('lastid');
$this->db->where('user_id', $id)->update('user', $data);
$this->session->unset_userdata('lastid');

CakePHP - passing multiple values from a view element to the controller

I want to pass some values to my controller from the view.
Actually I'm using the following code:
<?
echo $this->element('produtos-categoria', array(
'categoria_id' => $produtos['Produto']['categoriasproduto_id'],
'produto_id' => $produtos['Produto']['id']
));
?>
But I'm not able to get the second value in my controller, just the first value is coming:
public function list_categories($categoria_id = null, $produto_id = null ) {
pr($produto_id); exit; //empty
}
Anyone can help how to get the second value?
I donno what are you trying to achieve but to get the second variable you would need to set the second parameter to your method.
// If you want to set variable from a function and get it from an other function
public function an_other_function(){
$this->listacategorias(22, 333);
}
public function listacategorias($categoria_id = null, $produto_id = null ) {
var_dump($categoria_id);
var_dump($produto_id);
}
// If you want to set and get variable from url
host_or_domain_name/controller_name/listacategorias/22/33

Passing settings from view to element with default values

I'm trying to pass settings like this:
$mySettings = array(
'settingOne' => 'someValue',
'settingTwo' => 5,
'settingThree' => true
);
from the view into an element like so:
echo $this->element('foobar', $mySettings);
How would I set the default values for them inside of the element?
Or is it better to set them somewhere else? If so, where and how?
Thank you.
Write default code in appsController like below
$mySettings = array(
'settingOne' => 'someValue',
'settingTwo' => 5,
'settingThree' => true
);
$this->set('foobar', $mySettings);
And If you want to Modify $mySettings then you have to write same code in Controller that you want to change from.
You have to use foobar variable in element like
$foobar['settingOne'];
$foobar['settingTwo'];
$foobar['settingThree'];
if the setting entries are dynamic, you can try this:
function element($entry, $settings, $default = null) {
if (isset($settings[$entry])) {
return $settings[$entry];
} else {
return $default;
}
}
if the setting entries are static, you'd better define a class, each entry as a property.
The keys you pass in are transformed into variables
So just do
if (!isset($theConfigKey)) {
$theConfigKey = ...
}

Best way to ensure a PHP variable is an array

I have a javascript client passing a parameter to a server function (I'm using ExtJS Direct). Sometimes the client sends a single object, sometimes it sends an array of objects.
Currently I'm using this EnsureArray function to ensure the parameter is an array, and then I do foreach:
// Wraps a non array variable with an array.
function EnsureArray( &$aVar )
{
if ( !is_array( $aVar ) )
$var = array( $aVar );
}
function Destroy( $aRecords )
{
// $aRecords might be a single object or an array of objects. Ensure it's wrapped as an array.
$this->EnsureArray( $aRecords );
foreach ( $aRecords as $aRecord )
{
sql( "UPDATE Groups SET deleted = true WHERE id = $aRecord->id LIMIT 1" );
}
return array(
'success' => true,
);
}
Is there a trick, neater way, one line that can do the same?
Edit: Since posting this question, I've found that ExtJS has an option to send all records wrapped in array.
You could try the following, instead of the function:
$aRecords = is_array($aRecords) ? $aRecords : array($aRecords);
That's probably the best way tbh, if you're not going to enforce that you're always being sent an array.
I would make the function Destroy require arrays as its parameter:
function Destroy(array $aRecords) { ... }
The client should then also always send arrays. If, for whatever reason, that is not possible, the code that is receiving the (non-)array from the client and is passing it on to Destroy() needs to be responsible for passing it along as an array, because it's the interface between the (non-compliant) client and the standardized function.
There's probably going to be one endpoint for each possible action the client can call, so you don't even need to figure out whether the data is an array or not, you simply know. I.e.:
// someaction.php
include 'destroy.php';
$data = json_decode($_POST['data']);
Destroy($data);
but:
// anotheraction.php
include 'destroy.php';
$data = json_decode($_POST['data']);
Destroy(array($data));
If the client erratically sends different formats to the same action, fix the client.
Simply typecast the variable to an array:
function Destroy( $aRecords )
{
foreach ( (array)$aRecords as $aRecord )
{
sql( "UPDATE Groups SET deleted = true WHERE id = $aRecord->id LIMIT 1" );
}
return array(
'success' => true,
);
}
See http://php.net/manual/en/language.types.type-juggling.php

How to load return array from a PHP file?

I have a PHP file a configuration file coming from a Yii message translation file which contains this:
<?php
return array(
'key' => 'value'
'key2' => 'value'
);
?>
I want to load this array from another file and store it in a variable
I tried to do this but it doesn't work
function fetchArray($in)
{
include("$in");
}
$in is the filename of the PHP file
Any thoughts how to do this?
When an included file returns something, you may simply assign it to a variable
$myArray = include $in;
See http://php.net/manual/function.include.php#example-126
Returning values from an include file
We use this in our CMS.
You are close, you just need to return the value from that function.
function fetchArray($in)
{
if(is_file($in))
return include $in;
return false
}
See example 5# here
As the file returning an array, you can simply assign it into a variable
Here is the example
$MyArray = include($in);
print_r($MyArray);
Output:
Array
(
[key] => value
[key2] => value
)

Categories