sessions. my brain hurts.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
phpnovice
Forum Commoner
Posts: 25
Joined: Mon Feb 02, 2004 6:47 am
Location: england, london

sessions. my brain hurts.

Post 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
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Post 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.
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post 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 :D
User avatar
phpnovice
Forum Commoner
Posts: 25
Joined: Mon Feb 02, 2004 6:47 am
Location: england, london

Post 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?
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Or store them as an array in the first place.
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post 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
(
    &#1111;1] =&gt; 2
    &#1111;2] =&gt; 2
    &#1111;3] =&gt; 1
    &#1111;8] =&gt; 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).
User avatar
phpnovice
Forum Commoner
Posts: 25
Joined: Mon Feb 02, 2004 6:47 am
Location: england, london

Post by phpnovice »

Oh thats a beauty.
Thanks guys, think im starting to get the hang of this now.
User avatar
phpnovice
Forum Commoner
Posts: 25
Joined: Mon Feb 02, 2004 6:47 am
Location: england, london

Post 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?
User avatar
phpnovice
Forum Commoner
Posts: 25
Joined: Mon Feb 02, 2004 6:47 am
Location: england, london

Post 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
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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
    }
}
?>
Post Reply