user979331
user979331

Reputation: 11961

php getting values from session

Hey guys I have a quick question I have a session and when I do a print_r of the session this is what I get...

Array ( [items] => a:1:{s:2:"1_";a:5:{s:2:"id";s:1:"1";s:4:"name";s:9:"Product 3";s:5:"price";s:5:"20.00";s:6:"option";N;s:3:"qty";N;}} ) 

I am trying to echo out the price and the name (price = 20.00, name = Product 3)

I've tried..

<?php echo $_SESSION['price']; ?>

and

<?php echo $_SESSION['items']['price']; ?>

nothing works...any ideas?

Upvotes: 0

Views: 7529

Answers (4)

Jay Mee
Jay Mee

Reputation: 55

you have to define it first use session start and then you can use something along the lines of this sort of code below

     $data = mysql_query("select * FROM tablename where id = $id ") 
     or die(mysql_error()); 
     echo "<table border cellpadding=3>"; 
     while($info = mysql_fetch_array( $data )) 

and then you can echo the row you wish to by using $info example below

 echo "<th>User:</th> <td>".$info['user_name'] . "</td> "; 

Upvotes: 0

Basti
Basti

Reputation: 4042

The contents of $_SESSION['items'] seems to be serialized. Try

$items = unserialize($_SESSION['items']);

echo $items['1_']['price'];

var_dump($items);

http://php.net/manual/en/function.unserialize.php

Upvotes: 5

webbiedave
webbiedave

Reputation: 48887

Looking at your serialized data, it appears you have to:

$items = unserialize($_SESSION['items']);

$items ['1_']['price'];

Not sure why there's a 1_ but it's in there.

Upvotes: 1

PFY
PFY

Reputation: 336

It looks like the data is being serialized somehow. You could unserialize the data like this

$items = unserialize($_SESSION['items']);

however any changes you make to the items array will have to be updated to the session to be properly handled by whatever the script is doing with the data later with

$_SESSION['items'] = serialize($items);

Upvotes: 1

Related Questions