0

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?

5
  • What do you mean by "nothing works"? Commented Mar 13, 2012 at 19:25
  • 1
    php.net/manual/en/function.unserialize.php Commented Mar 13, 2012 at 19:25
  • when I echo $_SESSION['items']['name'] i get 'a' when it should be 'Product 3' Commented Mar 13, 2012 at 19:26
  • Who is assigning values to session ? why dont you set session properly!! Commented Mar 13, 2012 at 19:29
  • @zod Would you please explain the problem with assigning values to $_SESSION? Commented Mar 13, 2012 at 19:30

4 Answers 4

5

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

Sign up to request clarification or add additional context in comments.

3 Comments

It looks like it might have to be echo $items['1_']['price'] after unserializing.
I bow before your parsing eyes - and changed my answer accordingly. ;-)
Great. So now your answer's just like mine was 5 minutes earlier ;)
1

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);

Comments

1

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.

2 Comments

Awesome, just what I was looking for :)
Glad to help. Remember to checkmark this answer (in the upper left).
0

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> "; 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.