Page 1 of 1
sessions. my brain hurts.
Posted: Mon Feb 16, 2004 1:58 pm
by phpnovice
im trying to store a list of numbers in a session for a shopping cart. ie the id of the item. i can load them in there using .= and a space but im not sure how to read them out 1 at a time. am i doing this the hard way or what? i looked at some other tutorials but they dont really answer my questions.
Cheers
James
Posted: Mon Feb 16, 2004 2:47 pm
by liljester
when you save them to a session var, are you using a delimiter? if you are you can then explode the session var like this:
Code: Select all
$items_array = explode("<your delimiter>", $_SESSION[cart]);
then you have the cart in an array that you can loop through.
Posted: Mon Feb 16, 2004 5:05 pm
by DuFF
You said you are using a space in between them. If so, I hope to expand on the previous post.
Code: Select all
<?php
$_SESSION['yoursessionvar'] = "3 8 7 2 4"; //this is just for an example
$items_array = explode(" ", $_SESSION['yoursessionvar']);
echo $items_array[0]; //outputs: 3
echo $items_array[1]; //outputs: 8
//etc, etc
?>
Hope that helps

Posted: Mon Feb 16, 2004 6:29 pm
by phpnovice
your the man! that works a treat! do you think i could use a loop to see how many of one type there was. ie 1 1 2 2 3 into 2*1 2*2 1*3?
Posted: Mon Feb 16, 2004 7:46 pm
by McGruff
Or store them as an array in the first place.
Posted: Mon Feb 16, 2004 7:51 pm
by DuFF
Ok, lets expand my example again:
Code: Select all
<?php
$_SESSION['yoursessionvar'] = "1 1 2 2 3 8"; //this is just for an example
$items_array = explode(" ", $_SESSION['yoursessionvar']);
//count the array values
$array_count = array_count_values($items_array);
//output this array
print_r($array_count);
?>
This will output:
Code: Select all
Array
(
ї1] => 2
ї2] => 2
ї3] => 1
ї8] => 1
)
So you can access how many times a value was called by going:
Code: Select all
<?php
//what we are searching for
$value = 8;
//Example output:
echo "The value " . $value . " was found " . $array_count[$value] . " time(s)";
//Notice the $array_count[$value_count], this is what returns the times the variable was found
?>
This will produce the following if used in the example:
The value 8 was found 1 time(s).
Posted: Tue Feb 17, 2004 5:23 am
by phpnovice
Oh thats a beauty.
Thanks guys, think im starting to get the hang of this now.
Posted: Tue Feb 17, 2004 6:29 am
by phpnovice
ok now i have realised that i cant loop through the array with only the ids i have selected. so i get a list of all items not just the ones that were selected and how many of them. also i get an extra value at the beggining ie arrray[]=>1 cause of the space at the beggining i believe. can i chomp the left value off before?
Posted: Tue Feb 17, 2004 6:43 am
by phpnovice
So i now have a foreach loop which outputs fine. but it doesnt like me using ltrim on an array even though its a session variable and still a string. i get this error message.F atal error: Call to undefined function: array() in /home/bj253/public_html/viewbasket.php on line 6
from using this code
Code: Select all
<?php
//start the session
session_start();
//trim white space from cart
ltrim($_SESSION("cartcontents"));
//load item ids into an array
$cartarray = explode(" ", $_SESSION['cartcontents']);
//count the array values
$itemquan = array_count_values($cartarray);
//print an output using key values and number of
foreach($itemquan as $key => $value)
{
echo"<table><tr><td>the item id is :" . $key . "</td><td>Quan: " . $value . "</td></tr></table>";
}
?>
is this annoying or what. maybe i can skip the first value in my foreach loop?
James
Posted: Tue Feb 17, 2004 9:37 am
by McGruff
Or:
Code: Select all
<?php
/*
param (int) $item_id - am assuming this IS an integer (a primary db key should be used)
*/
function addToCart($item_id)
{
if(!isset($_SESSION['cart'][$item_id]))
{
$_SESSION['cart'][$item_id] = 1;
} else {
$_SESSION['cart'][$item_id]++;
}
}
function printCartRows()
{
foreach($_SESSION['cart'] as $item_id=>$quantity)
{
//etc
}
}
?>