Question : creating a session array for simple cart

I don't really know if I have the right idea here but I want to store users choices in a session.

So, lets say there are 3 items. They click on one to find out more about it. Here they can click on add to cart. Once they click on add cart it must add that item ID to a session. But then if they add a different item must it be stored in the same session? At the moment the add to cart link is not using post but get. So, on the cart page I have the below but I think that as soon as they click on another add to cart, the session value will change.

$_SESSION = $_GET['DVDID'];

Answer : creating a session array for simple cart

It is more complicated than it appears so I have attached some code that will do it (see below) as a PHP class. This is only a very simple basket but it can be easily extended.

It is used like so

$b = new myBasket("basketName");    // You can run several baskets, just use unique names

$b->addItem( $productCode );  // Adds 1 item

$b->addItem( $productCode2, 10 );   // Adds a quantity of 10 for productCode2


To display the basket

$items = $b->fetchAllItems();
$qty    = $b->fetchAllQtys();

foreach( $items  as $index=> $aProductCode ) {

     echo "Product code $aProductCode has {$qty[$index]} items<br/>";
}
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
<?php

class myBasket {

     protected $basket;                 // Variable for working with the basket
     protected $qty;                    // Number of items in a given basket entry

     private   $sessName;               // Session variable name
     private   $sessQty;                // Session variable for quantities


     // ----- Constructor -----------------------------------------------------


     // Parameters: $name - The basket must have a name to store the data against
     //
     function myBasket( $name ) {

          $this->sessName = $name . "_bskt";
          $this->sessQty  = $name . "_qty";

          $this->initialise();
     }





     // ----- Private methods -------------------------------------------------


     // Store the current array in a session variable
     //
     private function updateBasket() {
          $_SESSION[$this->sessName] = serialize($this->basket);
          $_SESSION[$this->sessQty]  = serialize($this->qty);
     }



     // Initialise the basket
     //
     private function initialise() {

          // Check if session is in progress and if so retrieve the existing basket
          //
          if ( isset($_SESSION [$this->sessName]) && isset($_SESSION [$this->sessQty]) ) {
               $this->basket = unserialize( $_SESSION[$this->sessName] );
               $this->qty    = unserialize( $_SESSION[$this->sessQty] );
          }
          else {
               $this->basket = array();
               $this->qty    = array();
               $this->updateBasket();
          }

     }



     // ----- Public methods --------------------------------------------------


     // Empty the basket out
     //
     function clear() {

          unset( $_SESSION [$this->sessName] );
          unset( $_SESSION [$this->sessQty ] );
          $this->initialise();
     }



     // Add an object to the end of the list
     //
     // Parameters: $item - an object to be stored
     //             $num - a quantity to be stored, defaults to 1
     //
     function addItem( $item, $num=1 ) {
          // Ensure the indexes stay in step by retrieving the highest
          // existing index and incrementing it
          //
          $ctr = count( $this->basket );

          if ( $ctr > 0 ) {
               $lastKey = end(array_keys($this->qty));
               $ctr = $lastKey + 1;
          }

          $this->basket [$ctr] = $item;
          $this->qty    [$ctr] = $num;

          $this->updateBasket();
     }



     // Remove an item from the list - identify by the basket number.
     // NOTE THAT REMOVING AN ITEM DOES NOT RESET THE BASKET NUMBERS in the arrays.
     //
     // Parameters: $basketNumber - the index of the array entry to remove from the array
     //
     function rmvItem( $basketNumber ) {

          if ( count($this->basket) > 0 ) {

               // 001 Check that an item exists before attempting its removal
               //
               if ( isset( $this->basket [$basketNumber]  ) ) {
                    unset( $this->basket [$basketNumber] );
                    unset( $this->qty [$basketNumber] );
                    $this->updateBasket();
               }
          }
     }



     // Fetch an item
     //
     // Parameters: $basketNumber - the index of the array entry to retrieve from the array
     //
     function fetchItem( $basketNumber ) {
          if ( count($this->basket) > 0 ) {
               return $this->basket [$basketNumber] ;
          }
     }



     // Fetch a quantity for an item
     //
     // Parameters: $basketNumber - the index of the qty to retrieve from the array
     //
     function fetchItemQuantity( $basketNumber ) {
          if ( count($this->qty) > 0 ) {
               return $this->qty [$basketNumber] ;
          }
     }


     // Change a given item in the list
     //
     // Parameters: $basketNumber - the index of the object to update
     //             $anItem - an object
     //
     function chgItem( $basketNumber, $anItem ) {
          if ( $basketNumber >= 0 && count($this->basket) > 0 ) {
               // 001 Check that an item exists before attempting its removal
               //
               if ( isset( $this->basket [$basketNumber]  ) ) {
                    $this->basket[$basketNumber] = $anItem;
                    $this->updateBasket();
               }
          }
     }



     // Change a given items quantity in the list
     //
     // Parameters: $basketNumber - the index of the object to update
     //             $qty - The new quantity
     //
     function chgQty( $basketNumber, $qty ) {
          if ( $basketNumber >= 0 && count($this->qty) > 0 ) {

               // 001 Check that an item exists before attempting its removal
               //
               if ( isset( $this->basket [$basketNumber]  ) ) {
                    $this->qty[$basketNumber] = $qty;
                    $this->updateBasket();
               }
          }
     }



     // Get a list of all the items in the basket
     //
     function fetchAllItems() {
          return $this->basket;
     }



     // Get a list of all the item quantities stored in the basket
     //
     function fetchAllQtys() {
          return $this->qty;
     }



     // Returns the count of the number of items in the basket
     //
     function countBasket(){
          return array_sum( $this->qty );
     }



} // End of class myBasket
Random Solutions  
 
programming4us programming4us