Session Help

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
teejayy1013
Forum Newbie
Posts: 2
Joined: Mon Apr 14, 2008 1:54 pm

Session Help

Post by teejayy1013 »

Hi guys,

I'm using two session variables $_SESSION[$sku] where $sku is retrieving something from a database
and $_SESSION[$price].

To display the contents of $_SESSION[$sku] i HAVE to use a foreach loop consisting of the following:

Code: Select all

 
foreach($_SESSION as $key=>$value){
            echo "<tr><td>$value</td><td>$key</td><td></td>";
                echo "<td>$_SESSION[$price]</td><td></td>";
    }
what i want to be displayed is a table that looks like this

Code: Select all

quantity          sku                   price                total
10             ipod_sock                 12.00              120.00
 
HOWEVER, because of the foreach displaying each $_SESSION, it also echo's the $_SESSION[$price] which i dont want it to do and adds the following line to my table for an output of :

Code: Select all

quantity          sku                   price                total
10             ipod_sock                 12.00              120.00
12.00            12.00                   12.00
now i know its because of the foreach() loop i have going. I tried specifying only the $_SESSION[$sku] session in the foreach but it comes up with this error:
Warning: Invalid argument supplied for foreach() in /home/users2/quinntk/public_html/morning/store/ShoppingCart.php on line 24
anyone know how I can go about doing this? I'm REALLY confused.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Session Help

Post by onion2k »

Instead of $_SESSION[$sku] use something like $_SESSION['session_cart_skus'][$sku]. Then you can foreach() through $_SESSION['session_cart_skus']. Obviously you'll have to change the code that puts the values into the session as well as the place that reads them.
teejayy1013
Forum Newbie
Posts: 2
Joined: Mon Apr 14, 2008 1:54 pm

Re: Session Help

Post by teejayy1013 »

onion2k wrote:Instead of $_SESSION[$sku] use something like $_SESSION['session_cart_skus'][$sku]. Then you can foreach() through $_SESSION['session_cart_skus']. Obviously you'll have to change the code that puts the values into the session as well as the place that reads them.
even if i foreach($_SESSION['session_cart_skus']) im still getting the error.

this is my current code:

Code: Select all

<?
    if(isset($_GET['submittedFormTest'])){
        $price=$_GET['price'];
        $sku= $_GET['sku'];
        $quantity = $_GET['quantity'];
        $_SESSION[$sku] =$_SESSION[$sku] + $quantity;
        $_SESSION['SKU'][$sku];
        print_r($_SESSION);
?>
        <table border=1>
            <tr><td>Quantity</td><td>sku</td><td>Description</td><td>Price Each</td><td>Total</td><tr>
<?  
        foreach($_SESSION['SKU'] as $key=>$value){
            echo "<tr><td>$value</td><td>$key</td><td></td><td></td>";
            echo"<td>$price</td><td></td></tr> <br />";
        #echo "$sku $quantity ".$_SESSION[$sku];
        }   
    }
?>
also, Im new to the whole PHP programming so I didn't really understand what you wanted me to do
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Session Help

Post by onion2k »

How about actually thinking about it for more than 19 minutes? Try a few things. See what happens. Work through it logically.
Post Reply