Access array variable in session (CodeIgniter) - codeigniter

I have an array called config. I'm trying to echo a variable from the array in the session.
I've tried:
echo $this->session->userdata('config['item']');
but it doesn't work. What's wrong with my syntax here? I've print_r'd my session and the items are in the config array. I've also tried:
echo $this->session->userdata("config['item']");
I get no errors this time, but no data either.

If config is an array . And item is string name of what you want to get from config then
echo $this->session->userdata($config['item']);
or
echo $_SESSION[$config['item']];
If config is an array inside session you should first get it.
$tmp = $this->session->userdata('config');
echo $tmp['item'];
or
echo $_SESSION['config']['item']
Sorry for my english.

If you want to use the session array, use the variable, not the function:
echo $this->session->userdata['user_data']['item'];
If you want to write:
$this->session->userdata['user_data']['item'] = 'value';
$this->session->userdata['other_data']['other'] = 'value2';
$this->session->sess_write();
This allows you to edit values in array just like you do with $_SESION['user_data']['avatar'] = $avatar, with 'only' one extra line and only using CI library.

Always escape your string it should be this way:
echo $this->session->userdata('config[\'item\']');

Related

Remove part of file path in Ruby

I am are receiving an array as a variable
Is an example
["/a/b/01_Sources/02_Transferred/06_CPAS/Redbull/from_MediaHouse/Transcripts/MI201711200143.xlsx", "/a/b/01_Sources/02_Transferred/06_CPAS/Redbull/from_MediaHouse/Transcripts/MI201703030110.pdf"]
The following statement creates this list:
<%= var(file_list_array).map{|file| "<li>#{File.basename(file)}</li>"}.join("\n")%>
MI201711200143.xlsx
MI201703030110.pdf
The following statement creates this list
<%= var(file_list_array).map{|file| "<li>#{file}</li>"}.join("\n")%>
/a/b/01_Sources/02_Transferred/06_CPAS/Redbull/from_MediaHouse/Transcripts/MI201711200143.xlsx
/a/b/01_Sources/02_Transferred/06_CPAS/Redbull/from_MediaHouse/Transcripts/MI201703030110.pdf
But what I would really like:
/Redbull/from_MediaHouse/Transcripts/MI201711200143.xlsx
/Redbull/from_MediaHouse/Transcripts/MI201703030110.pdf
What do I need to change to get that ?
Assuming you have your array of file paths in an array you could do.
file_paths.map{|path| path.gsub(/.*(\/Redbull\/.*)/, $1) }
This will replace each item with whatever is below the "Redbull" directory
Alternatively if you didn't want to preprocess that list you could just put it in your display code, but that will make it less clear as to what you need to send the displaying logic.
<%= var(file_list_array).map{|file| "<li>#{file.gsub(/.*(\/Redbull\/.*)/, $1)}</li>"}.join("\n")%>
Try this
file_list_array[0].split("06_CPAS")[1]
assuming you want to split from "06_CPAS" . You can pass it as a variable too like this
split_str = "06_CPAS"
index = 0
file_list_array[index].split(split_str)[1]

Variable substitution in Shell script

I have declared one variable IS_abc=false, on basis of certain condition I am changing value to IS_abc=true
IS_abc=false
declare -a my_arr
my_arr = ('abc' 'pqr' 'xyz')
....
.... // some operation
IS_abc=true
for i in "${my_arr[#]}"
do
//here i want to access value of $IS_abc as true
//how to do this
done
I have tried accessing using $IS_'$i' , but it raising error as invalid substitution
Tell me if I am doing anything wrong here?
You can use indirect var reference:
my_arr=('abc' 'pqr' 'xyz')
IS_abc=true
var="IS_${my_arr[0]}"
echo "${!var}"
Output:
true
I'm doing it like this:
value=`eval echo \\${IS_${i}}`
There's probably a better way but this should work.

Printing this variable in smarty

I'm having an array $customPre. I want to print the element of the array "Please specify which fund". I am doing like this:
{$customPre.Please specify which fund}
But it's not working.
In this case you need to use PHP-like syntax that is similar to PHP: {$variable['key']}.
If In PHP you have:
$smarty->assign('customPre', array ('Please specify which fund' => 'This is value'));
In Smarty you need to use:
{$customPre['Please specify which fund']}
And the output for this will be:
This is value
I believe you cannot use in this case dot syntax ( {$customPre.Please specify which fund}) because it's probably looks for whitespaces in keys. Even adding quotes won't help.

Magento registry variables in controller

I have stored a variable in register by Mage::register('captcha', $var); in helper. And in the controller i tried to retrieve the variable by using Mage::registry('captcha'); But i dont getting any values here. Please help me to solve this.
In your helper file create a function like below :
public function getCaptcha(){
$var = 'myValue123';
Mage::register('varun', $var);
return Mage::registry('varun');
}
In your controller function:
$registryValue = Mage::helper('yourModule')->getCaptcha();
echo registryValue ; //prints myValue123
Hope it helps !!!
It's look like syntax is right.
Please first try to set some static value like $var="test"
Mage::register('captcha', $var);
after that got this value in controller.
Mage::registry('captcha');
if you got this value test then i think you have problem with $var in your helper.
Let me know if you have any problem
'captcha' is already in use, so magento never set your data in registry. Change the name, for example 'captcha1'
Mage::register('captcha1', $var);

Updating session vaules in CodeIgniter

Have a little problem, how to update single value in CI session.
I have
$data['jezik'] = $this->uri->segment(1);
$this->session->userdata('jezik',$data['jezik']);
$data['jezik']= $this->session->userdata('jezik');
But it does not change the value. Always the same!
Try:
$this->session->set_userdata('jezik',$data['jezik']);
You need to use set_userdata rather than userdata. There are two ways to do it. You can do it in a key/value type way
$this->session->set_userdata('jezik', $data['jezik']);
Or you can pass an array
$sessionData = array('jezik' => $data['jezik'])
$this->session->set_userdata('jezik',$data['jezik']);
You can read more about CodeIgniter sessions here
Like #Pattle said
$this->session->set_userdata('jezik', $data['jezik']);
Though I think he meant to pass in the array, like so:
$sessionData = array('jezik' => $data['jezik']);
$this->session->set_userdata($sessionData);

Resources