Giving session a unique name?

Hey guys,

I’ve coded this page to handle the shopping basket on my site, but the idea is to have more than one shop running on the same site, so I need separate baskets for each. Obviously an instant problem with that is they share the same session id’s. Below i’ve made an example of what in my ideal world is how it’d work by defining:

$_SESSION['cart_id_$storeid'] = $sessionvalue;

But that obviously doesn’t work, how would I go about doing this? :S

Here’s my full source:

<?php
session_start();
include("dbstore.php"); 

$storeurl = addslashes($_GET['url']);

$getstore = mysql_query("SELECT * FROM `store` WHERE `url` = '$storeurl'") or die(mysql_error()); 
$store = mysql_fetch_array($getstore);
$storeid = "$store[id]";
$current = time(); //Current timestamp 


// Grab shopping cart session if it exists

if(isset($_SESSION['cart_id_set'])) {

$getbasket = mysql_query("SELECT * FROM `store_basket` WHERE `id` = '".$_SESSION['cart_id']."'") or die(mysql_error()); 
$basket = mysql_fetch_assoc($getbasket);

$get_basket_items = mysql_query("SELECT * FROM `store_basket_items` WHERE `basketid` = '$basket[id]'") or die(mysql_error());
$total_items = mysql_num_rows($get_basket_items);

$basket_total = mysql_query("SELECT SUM(price) AS price FROM store_basket_items WHERE basketid = '$basket[id]'");
$total = mysql_fetch_array($basket_total);

$total_price_format = number_format($total[price], 2, '.', ',');

echo"<div id=\\"basket\\">$total_items Items - £$total_price_format   <u>Check Out</u></div>";



// Create shopping cart session if it does not exist yet..

} else {

$current2 = time(); //Current timestamp 
$countcarts = mysql_query("SELECT MAX(id) AS maximumId FROM `store_basket") or die(mysql_error()); 
$totalCarts = mysql_fetch_object($countcarts); 
$new_totalCarts = ($totalCarts->maximumId+1); 

$createcart = mysql_query("INSERT INTO `store_basket` (`id`, `timestamp`) VALUES('" . mysql_real_escape_string($new_totalCarts) . "', '" . $current2 . "')") or die(mysql_error()); 
$basketid = mysql_insert_id(); 

$sessionvalue = $basketid; 

$_SESSION['cart_id'] = $sessionvalue; 
$_SESSION['cart_id_set'] = 'yes'; 

echo"<div id=\\"basket\\">0 Items - £0.00   <u>Check Out</u></div>";

}


?>

Thanks for any help provided :slight_smile:

Try

$SESSION["cart_id$storeid"] = $sessionvalue;

Have you tried the single quote -----------> [/COLOR]store_basket[COLOR=DarkOrange] etc

to 'store_basket?[COLOR=#000000][COLOR=#dd0000]

[/COLOR][/COLOR](I have hit the nail once in the past.)[COLOR=#000000][COLOR=#dd0000]

[/COLOR][/COLOR]

Just done this and they seem to be setting fine, it must be something to do with this SQL query, with using the double quotes?

$getbasket = mysql_query("SELECT * FROM `store_basket` WHERE `id` = '".$_SESSION["cart_id_$storeid"]."'") or die(mysql_error()); 
$basket = mysql_fetch_assoc($getbasket);

Did you try and print_r the $_SESSION content to see what is happening?

Another option you might consider is to have a shopping cart php class that stores items and prices and quantities for each item.

You could then create a new instance of the cart for each store on your website and store it in a separate session variable using serialise(). This way you won’t need to make calls to the database every time the web page is called.

Do an echo of the query to see if it’s exactly as you expect it to be.

Thanks for the suggestion but it does what it did in the first place, each time the page is loaded it seems to assume that it hasn’t been set at all.