How to display a mixed session array?

Using any kind of loop, how can i echo what's inside my session ar

Page 1 of 1

6 Replies - 6092 Views - Last Post: 11 February 2009 - 08:20 AM Rate Topic: -----

#1 bl9d3s  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-October 08

How to display a mixed session array?

Posted 03 February 2009 - 05:55 PM

I used array_push to put the follow variables in my session array:

array_push($_SESSION['product'],$_POST['item']);
	array_push($_SESSION['product'],$_POST['price']);
	array_push($_SESSION['product'],$_POST['quan']);
	array_push($_SESSION['product'],$sub);



I am wondering now, how can I display all the stuff inside my session array???


Thanks in advance to all your answers...

Is This A Good Question/Topic? 0
  • +

Replies To: How to display a mixed session array?

#2 lemonacode  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 02-February 09

Re: How to display a mixed session array?

Posted 03 February 2009 - 08:18 PM

Ive found an easier way, but there is probably a shorter much easier way than this, firstly i am using variables to start an array to make the variable a variable array, you would go $var[] putting brackets next to $var sets $var as an array. for example
$mypostman[0] = $_POST['item'];

What is that doing?
Well the above example is setting an array called 'mypostman' ('[0]'represents the first value in the array) which equals your posted item.

To add more values to your array, copy the array name again so $mypostman, increase the value inside the brackets and $mypostman[1] = something and so on.
$mypostman[0] = $_POST['item'];
$mypostman[1] = 'Value 2';
$mypostman[2] = 'Value 3';
$mypostman[3] = 'Value 4';


Now that we have values in the array, we would like to add these to a session named 'product', so firstly start the session, create your array with values, then make session 'product' equal a value in the array.

session_start();
$mypostman[0] = $_POST['item'];
$mypostman[1] = 'Value 2';
$mypostman[2] = 'Value 3';
$mypostman[3] = 'Value 4';
$_SESSION['product'] = $mypostman[0];
echo 'Value = '$_SESSION['product'];

What does this do?
That will register session 'product' as the first value in the array and echo the value out, now all you need to do is echo each value in the array as session 'product'.

I will give you a hint, use a for loop to increase the value inside the brackets.

syntax for a 'for loop'
for (init; cond; incr)
{
  code to be executed;
}


If you would like to learn about for loops or looping in general click here and scroll to for loops, hope this helps.
Was This Post Helpful? 0
  • +
  • -

#3 Auzzie  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 43
  • View blog
  • Posts: 573
  • Joined: 20-January 09

Re: How to display a mixed session array?

Posted 04 February 2009 - 03:53 AM

If you mean display the stuff in your session array then
echo("<pre>" . $_SESSION . "</pre>");

Would work. If you want to see the contents of any array just replace $_SESSION with the array variable
Was This Post Helpful? 1
  • +
  • -

#4 Rezert  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 71
  • Joined: 17-August 08

Re: How to display a mixed session array?

Posted 04 February 2009 - 05:40 AM

foreach($_SESSION as $key => $val) {
echo $val." = ".$key."<br />";
}


That is, if I understand correctly what you are trying to do. For example, if I were to say:

$_SESSION['cat'] = "Black";
$_SESSION['car'] = "Blue";
$_SESSION['milk'] = "White";


It would look like:

cat = Black
car = Blue
milk = White

This post has been edited by Rezert: 04 February 2009 - 05:41 AM

Was This Post Helpful? 1
  • +
  • -

#5 lemonacode  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 02-February 09

Re: How to display a mixed session array?

Posted 04 February 2009 - 06:35 AM

Interesting $_SESSION can be used to echo each session name and value out as an array :P
we learn something everyday, Auzzie and Rezert :^:
Awesome :)
This below is the for loop code
session_start();
$mypostman[0] = $_POST['item'];
$mypostman[1] = 'Value 2';
$mypostman[2] = 'Value 3';
$mypostman[3] = 'Value 4';
for($i = 0; $i < 4; $i++){
$_SESSION['product'] = $mypostman[$i];
echo 'Value = '$_SESSION['product'];
}


Was This Post Helpful? 0
  • +
  • -

#6 thehat  Icon User is offline

  • awake ? web();
  • member icon

Reputation: 106
  • View blog
  • Posts: 951
  • Joined: 28-February 08

Re: How to display a mixed session array?

Posted 04 February 2009 - 07:20 AM

if you just want the entire array writing to the screen for debug purposes and don't care about styling, use this:

print_r($_SESSION['product']);


Was This Post Helpful? 0
  • +
  • -

#7 bl9d3s  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-October 08

Re: How to display a mixed session array?

Posted 11 February 2009 - 08:20 AM

sorry for the late reply to all, i happen to attend something personal that's why i was not around for a while... :D


thanks to all, especially to you lemonacode...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1