Jump to content

Session Variables Problem


Samza

Recommended Posts

Hi,

 

I am following the PHP Academy tutorial on youtube, and I'm on part 4 (http://www.youtube.com/watch?v=cJJKQ8MXGrE).

However, I am running into a bit of a problem as I have other session variables for my user login and when I am using the foreach statement I dont want to include the login session variables if you understand, only the product_ variables

 

function cart() {
foreach($_SESSION as $name => $value) {
if ($value>1) {
if (substr($name, 0, =='product_'){
$id = substr($name, 8, (strlen($name)-);
echo '<br />'.$id;
}
}
else {
echo "Your cart is empty! <br />";
echo $name.': has a value of :'.$value;
}
}
}

 

and this is displayed:

 

output.jpg

 

As you can see it's also using the login session variables which is causing some issues for me because it's displaying that the cart is empty when it is not!

So i'm a bit stuck as to what to do :S

 

Sam-

Edited by Samza
Link to comment
Share on other sites

In your php.ini file, find the error handling section and make sure the following lines are in it, and appear only once without a leading semicolon:

 

error_reporting = -1

display_errors = On

 

Then restart Apache and see if you get any errors when you run the script. If you find more than one php.ini file, put phpinfo(); die(); in a script and run it. You can also verify your settings that way.

Link to comment
Share on other sites

Add this line to your script and see if you get an error: $notvalid = $_SESSION['notvalid']; It should produce an 'undefined index' error. If it doesn't, we need to figure out why.

 

I receive an no errors :)

Edited by Samza
Link to comment
Share on other sites

The tutorial you found is, what would be a polite phrase, 'technically challenged'.

 

The cart stored in the session should be stored in a unique session variable like $_SESSION['cart'], not in $_SESSION. This would separate any data for the cart from data for other purposes.

Link to comment
Share on other sites

The tutorial you found is, what would be a polite phrase, 'technically challenged'.

 

The cart stored in the session should be stored in a unique session variable like $_SESSION['cart'], not in $_SESSION. This would separate any data for the cart from data for other purposes.

 

They get stored as a session named as " product_'productid' " so how can I specifically target sessions beginning with 'product_' ?

Link to comment
Share on other sites

how can I specifically target sessions beginning with 'product_' ?

 

If you make the change I suggested, you don't have to target specific keys. All the 'cart' related keys/values will be in $_SESSION['cart']. All the references to $_SESSION that are part of the 'cart' logic just need to be changed to $_SESSION['cart'].

 

Edit: Also, I would add that the definition of that cart, where you are using " product_'productid' " is overly complicated. You have probably 2-3 times more code than needed for every function that references the cart.

 

Just use the product id as the key and store the quantity as the value, and if the quantity is zero, just remove the entire key/value from the cart.

Edited by PFMaBiSmAd
Link to comment
Share on other sites

Sample code that demonstrates how simple your 'cart' and code should/can be -

 

<?php
session_start();
// if the cart does not exist, create an empty cart
if(!isset($_SESSION['cart'])){
   $_SESSION['cart'] = array();
}

// fake adding items to the cart for demo purposes
$_SESSION['cart'][1] = 3; // id = 1, qty = 3
$_SESSION['cart'][2] = 1; // id = 2, qty = 1

// lists only the id/qty in the cart
function list_cart($cart){
   if(empty($cart)){
       echo "Your cart is empty! <br />";
   } else {
       foreach($cart as $id=>$qty){
           echo "Product Id: $id, Quantity: $qty<br />";
       }
   }
}

list_cart($_SESSION['cart']); // example usage, pass the actual cart into the function as a call-time parameter

 

Some notes about this code -

 

1) The 'cart' should be passed into the function, as shown, as a call-time parameter so that the 'cart' can come from anywhere, a session, a database query, a cookie, xml, ...

 

2) I named the function list_cart because that's all it does. To actually display all the information about the contents in the cart, you would extract the id's (using array_keys), use ONE query, not in a loop, to get the product information for all those product id's, store that product information into an array, again using the product id as the key to that array, then as you loop through the contents of the 'cart' you would use the product id from the cart to reference the product information for that id.

 

3) The variable names and array index names indicate the meaning of the data in the variables.

Edited by PFMaBiSmAd
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.