how to get session variable value in php? - php

Inside my code i can see one session and once i printed that session i got following things in view source
Array
(
[Config] => Array
(
[time] => 1406983421
[timeout] => 10
)
[loggedIn] => 1
[user] => Array
(
[User] => Array
(
[id] => 424
[correspondence_email] =>
[terms] => Y
[old_facebook] =>
[[old_twitter] =>
[gmail_email] =>
[login_count] => 9
[last_secdeg_updated] => 2014-08-01 08:47:35
)
)
)
then i tried this line to get the value of "login_count"
echo "value".$this->Session->read("login_count");
but am not able to see this the "login_count" in echo.
am getting only this result in viewsource
value
How do i get "login_count" vale to a variable.?

echo $this->Session->read('user.User.login_count');
I hope this helps you.

Without seeing the code that writes the session, I am suspecting that this is what you're looking for:
echo $this->Session->read('user.User.login_count');

You dont have to create any codes to perfom session operations.They are already built in the cake php api.You just need to read and write the sessions from the link i given below.
http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html Used in Controllers
http://book.cakephp.org/2.0/en/core-libraries/helpers/session.html Used in Views

Related

Accessing data in a php multi-dimensional array

I have a multi-dimensional array and php seems to be returning an array instead of a value when I attempt to access the values directly. What am I doing to cause this?
The array looks like (via print_r):
Array (
[12] => Array ( [2016] => 93083.00 [2015] => 85367.00 [2014] => 69726.00 )
[11] => Array ( [2016] => 66730.00 [2015] => 65548.00 [2014] => 77936.00 )
[10] => Array ( [2016] => 84602.00 [2015] => 112070.00 [2014] => 102104.00 )
)
I'm trying to access values using $arrayname[12][2016] but it is returning Array[2016] instead of 93083.
Is this a simple syntax mistake? Or am I missing part of the concept here? I've been trying to work this problem for hours so maybe I'm missing a simple explanation.
EDIT: the syntax above is actually correct, the issue was in the data entry: I was trying to access a key that didn't exist. I tried to delete the post, but can't since it has been answered.
$arrayname[12] = [2016=>93083.00, 2015=> 85367.00 ]
...
...
echo $arrayname[12][2015] ; // prints 85367
i think your array has one more level. try $arrayname[12][2016][2016] .

How can I access ID array of its value

I am analyzing someone's else code where I found during debugging the type and values of a class variable during run time:
echo print_r($this->_out);
Array
(
[id] => -1
[fieldErrors] => Array
(
)
[error] =>
[data] => Array
(
)
[row] => Array
(
[DT_RowId] => row_177
[id] => 177
[last_name] => sdfdsf
[first_name] => dsf
[homeaddr] => sdfdsfsdfdsfdsfdsfdsf
[email] => s#jj.com
[officeaddr] => wwwwwwwwwwwwwwwwwwwwwwww
[mobile] => 11111111
[age] => 11
[chargeamt] => 11
[start_date] => 11/11/2011
)
)
1{"row":{"DT_RowId":"row_177","id":"177","last_name":"sdfdsf","first_name":"dsf","homeaddr":"sdfdsfsdfdsfdsfdsfdsf","email":"s#jj.com","officeaddr":"wwwwwwwwwwwwwwwwwwwwwwww","mobile":"11111111","age":"11","chargeamt":"11","start_date":"11\/11\/2011"}}
I am a newbie in PHP and would like to know how I can access [id] => 177 value i.e. value 177.
I tried out many ways
$this->_out['row']['id'][0]
It gave me below result:
1{"row":{"DT_RowId":"row_177","id":"177","last_name":"sssss","first_name":"ss","homeaddr":"sssssssssssssssssssss","email":"ss#ww.com","officeaddr":"sssssssssssssssssssssssssssss","mobile":"11111111","age":"11","chargeamt":"11","start_date":"01\/01\/2001"}}
while
I tried out many ways
$this->_out['row']['id']
It gave me below result:
177{"row":{"DT_RowId":"row_177","id":"177","last_name":"sssss","first_name":"ss","homeaddr":"sssssssssssssssssssss","email":"ss#ww.com","officeaddr":"sssssssssssssssssssssssssssss","mobile":"11111111","age":"11","chargeamt":"11","start_date":"01\/01\/2001"}}
and others but its just not giving me the expected.
How can I access the value as desired?
You are doing it right. $this->_out['row']['id'] will return desired result (check why you get also JSON string that is not part of print_t($this->_out).
This will return result 177:
$this->_out['row']['id'];
And since in PHP you can access string characters as array, this will returns first character in string (that is 1):
$this->_out['row']['id'][0];
And this will throw error as there is no such index (string length is 3, so last index is 2):
$this->_out['row']['id'][5];
The print_r result is an array with more arrays on it. So first of all you must find the index of the main array which index represents the sub-array with the value you are looking for. And then you must use this index to access the sub array values.

How to access array value in smarty when key starts with a period?

I am using a CMS that uses Smarty, I am not that familiar with Smarty. I can access array values like this: {{$data.video.title}}
I need to access video information but came across this issue:
[formats] => Array
(
[.mp4] => Array
(
[postfix] => .mp4
[dimensions] => Array
(
[0] => 1280
[1] => 720
)
[duration] => 330
[duration_string] => 5:30
[duration_array] => Array
(
[minutes] => 5
[seconds] => 30
)
[file_size] => 51928676
[file_size_string] => 49.52 Mb
[timeline_screen_amount] => 0
[timeline_screen_interval] => 0
[file_name] => 5.mp4
[file_path] => 8ad883ae4989f1aaf1da077bf56d9495/0/5/5.mp4
[timeline_directory] =>
)
)
I would like to know how I can access the [.mp4] values, since it begins with a period it causes problems. I have tried many variations such as:
{{$data.formats..mp3.file_size_string}}
{{$data.formats[.mp3].file_size_string}}
{{$data.[formats][.mp3][file_size_string]}}
etc...
Any help would be greatly appreciated!
I asked the CMS support for a solution, they provided the following in case anyone else needs to know how to do this:
You have to assign an extra variable:
{{assign var="postfix" value=".mp3"}}
{{$data.formats[$postfix].file_size_string}}
You can use the following syntax to achieve that:
{{$data.formats['.mp4'].file_size_string}}
You don't need to create extra variable.

Adding array to existing array without invoking a new key

I'm building a 3 page submission form, and I'd quite like all of the $_POST results to be stored in a single session variable.
So page 1 starts by setting up the array and adding the first lot of post data:
$_SESSION['results'] = array();
$_SESSION['results'] = $_POST // first lot of post data
This works great and returns an array like:
Array
(
[name] => bob
[address] => 1 foobar way
[age] => 100
)
So when I get the resuts from page 2, I want to simply append them to the existing array without invoking a new array+key
array_push($_SESSION['results'], $_POST); //second lot of post data
To get something like this:
Array
(
[name] => bob
[address] => 1 foobar way
[age] => 100
[job] => rubbish php dev
[salary] => 1000
)
But instead I get:
Array
(
[name] => bob
[address] => 1 foobar way
[age] => 100
[0] => Array
(
[job] => rubbish php dev
[salary] => 1000
)
)
Even more annoying is that I'm sure I had this working properly before I tweaked the code. What am I doing wrong?
You can also use the + operator:
$combined = $_SESSION['results'] + $_POST;
array_merge() is the function you're after.
array_merge() is your answer see http://php.net/manual/en/function.array-merge.php
You have to use array_merge(), look at this: array_merge()

How to get the required value displayed from the given array?

From below array i want the value of "_sql" to be displayed or we can I want to echo the "_sql" value, so what should be the syntax to display it in PHP code?
JTableMenu Object
(
[lft] =>
[rgt] =>
[home] =>
[_tbl] => #__menu
[_tbl_key] => id
[_db] => JDatabaseMySQL Object
(
[name] => mysql
[_nullDate] => 0000-00-00 00:00:00
[_nameQuote] => `
[_sql] => INSERT INTO `jos_menu` ( `id`,`menutype`,`name`,`alias`,`link`,`type`,`published`,`componentid`,`parent`,`ordering`,`checked_out`,`checked_out_time`,`brow............
[_errorNum] => 0
)
)
In the code the above array is stored in $row, so i have written an echo statement to display the required value like this: echo $row['_db]['_sql']; please suggest where and why I am getting wrong Why I cannot get the value in "_sql" index?
I got the Solution I did this
echo $row->_db->_sql;
And I got the output...
Thanks Pranav

Categories