Jump to content

Some sort of session issue...?


Chaos Creator

Recommended Posts

First of all, I hope I'm posting this in the right category. Feel free to move if not...

 

Okay. I've built a shopping cart system based on a flat file database (well, actually, I built it from a bit of code I found called PHPCart created by Ben Yanis, www.barnyardbbs.com). I've modified a lot of the original script and have run into a bit of a problem.

 

Here's the deal:

The system uses a function to add items to the customer's cart, but when I add multiple items to it, the second item shows the quantity, but no other information. So, I'm guessing there's an issue with some portion of my session.

 

Link: Here (Ignore the layout, everything is still in testing. The only link in the categories that works is the first one, Jars. That page is still under construction, but if you scroll to the bottom, there are two 'products' with links to add them to the cart. Next, you have to click on the 'My Shopping Cart' link in the header navigation. This will display the cart and it's imperfections. :) )

 

Here is the code for the three relative pages:

 

include/functions.php (contains all of the functions, except the one necessary to draw the data from the individual category files)

<?
define("JARS_FILE_PATH", "include/jars.txt");
define("STASHTINS_FILE_PATH", "include/stashtins.txt");
define("POUCHES_FILE_PATH", "include/pouches.txt");
define("KRYPTO_FILE_PATH", "include/krypto.txt");
define("SMOKESTOPPERS_FILE_PATH", "include/smokestoppers.txt");
define("PIPES_FILE_PATH", "include/pipes.txt");
define("PIPECLEANERS_FILE_PATH", "include/pipecleaners.txt");
define("PAPERS_FILE_PATH", "include/papers.txt");
define("FLASKS_FILE_PATH", "include/flasks.txt");
define("GRINDERS_FILE_PATH", "include/grinders.txt");
define("SCALES_FILE_PATH", "include/scales.txt");
define("VAPORIZERS_FILE_PATH", "include/vaporizers.txt");
define("INCENSE_FILE_PATH", "include/incense.txt");
define("INCENSEBOTTLES_FILE_PATH", "include/incensebottles.txt");
define("HOOKAHS_FILE_PATH", "include/hookahs.txt");
define("POKERS_FILE_PATH", "include/pokers.txt");
define("CONTAINERS_FILE_PATH", "include/containers.txt");
define("ASHTRAYS_FILE_PATH", "include/ashtrays.txt");
define("CANDLES_FILE_PATH", "include/candles.txt");
define("SUPPLIMENTS_FILE_PATH", "include/suppliments.txt");
define("DETOX_FILE_PATH", "include/detox.txt");
define("POSTERS_FILE_PATH", "include/posters.txt");
define("GAMES_FILE_PATH", "include/games.txt");
define("GIFTS_FILE_PATH", "include/gifts.txt");
define("CLOTHING_FILE_PATH", "include/clothing.txt");
define("HENNA_FILE_PATH", "include/henna.txt");
define("HACKEYSACKS_FILE_PATH", "include/hackeysacks.txt");
define("HEMPTWINE_FILE_PATH", "include/hemptwine.txt");
define("PENDANTS_FILE_PATH", "include/pendants.txt");
define("STONEJEWELRY_FILE_PATH", "include/stonejewelry.txt");
define("GLASSJEWELRY_FILE_PATH", "include/glassjewelry.txt");
define("FILE_DELIMITER", "|");

define("CURRENCY_SYMBOL", "$");
define("ID_COLUMN", 0);
define("NAME_COLUMN", 1);
define("PRICE_COLUMN", 2);
define("DESCRIPTION_COLUMN", 3);
define("QUANTITY_COLUMN", 4);

define("CART", "cart");
define("TEXTMODEPADWIDTH", 30);

session_start();

// Enable this only for testing, when you want to nuke the cart on every page load
//unset($_SESSION[CART]);

if(!isset($_SESSION[CART])) {
// Load it from the file
LoadProductsFromFile();
$_SESSION["carttoken"] = "";
}

//Conditional logic, based on parameters
if (isset($_GET["cartaction"])) {
if (empty($_GET["carttoken"]) || $_GET["carttoken"] != $_SESSION["carttoken"]) {
	// It's fresh, rather than a reload

	switch($_GET["cartaction"]) {
		case "add" :
			if (strlen($_GET["cartitem"]) > 0) {
				$_SESSION[CART][$_GET["cartitem"]][QUANTITY_COLUMN]++;
			}
			break;
		case "subtract" :
			if (strlen($_GET["cartitem"]) > 0) {
				$_SESSION[CART][$_GET["cartitem"]][QUANTITY_COLUMN]--;
			}
			break;
		case "clear" :
			ClearCart();
	}		
	$_SESSION["carttoken"] = $_GET["carttoken"];
}
}

function ClearCart() {
unset($_SESSION[CART]);
LoadProductsFromFile();
}

function ReturnCartHTML($IncludeAddRemove = 0) {
$items_in_cart = 0;
$running_total = 0;

if ($IncludeAddRemove == 1) {
	$result = "<div id=\"Cart\"><table class=\"CartWithItems\"><tr><th>Product ID</th><th>Product Name</th><th>Product Description</th><th>Price Each</th><th>Quantity in Cart</th><th>Quantity Price</th><th>Add / Remove</th></tr>";
}
else {
	$result = "<div id=\"Cart\"><table class=\"CartWithItems\"><tr><th>Product ID</th><th>Product Name</th><th>Product Description</th><th>Price Each</th><th>Quantity in Cart</th><th>Quantity Price</th></tr>";
}

if (isset($_SESSION[CART])) {
	foreach($_SESSION[CART] as $Product)
	{
		// Show only items that have a quantity > 0
		if (Nsz($Product[QUANTITY_COLUMN], 0) > 0) {
			$running_total += ($Product[PRICE_COLUMN] * $Product[QUANTITY_COLUMN]);
			if ($IncludeAddRemove == 1) {
				$result .= "<tr><td>" . $Product[iD_COLUMN] . "</td><td>" . $Product[NAME_COLUMN] . "</td><td>" . $Product[DESCRIPTION_COLUMN] . "</td><td>" . CURRENCY_SYMBOL . number_format($Product[PRICE_COLUMN],2) . "</td><td>" . $Product[QUANTITY_COLUMN] . "</td><td>" . CURRENCY_SYMBOL . number_format($Product[PRICE_COLUMN] * $Product[QUANTITY_COLUMN],2) . "</td><td><a href=\"" . $_SERVER["PHP_SELF"] . "?cartaction=add&cartitem=" . $Product[iD_COLUMN] . "&carttoken=" . mt_rand() . "\">Add</a> <a href=\"" . $_SERVER["PHP_SELF"] . "?cartaction=subtract&cartitem=" . $Product[iD_COLUMN] . "&carttoken=" . mt_rand() . "\">Remove</a></td></tr>";
			}
			else {
				$result .= "<tr><td>" . $Product[iD_COLUMN] . "</td><td>" . $Product[NAME_COLUMN] . "</td><td>" . $Product[DESCRIPTION_COLUMN] . "</td><td>" . CURRENCY_SYMBOL . number_format($Product[PRICE_COLUMN],2) . "</td><td>" . $Product[QUANTITY_COLUMN] . "</td><td>" . CURRENCY_SYMBOL . number_format($Product[PRICE_COLUMN] * $Product[QUANTITY_COLUMN],2) . "</td></tr>";
			}
			$items_in_cart++;
		}
	}	
}
if ($items_in_cart > 0) {
	$result .= "</table><div class=\"CartTotal\">Total: " . CURRENCY_SYMBOL . number_format($running_total, 2) . "</div></div>";
}
else {
	$result = "<div id=\"Cart\"><div class=\"CartWithoutItems\">Your shopping cart is currently empty.</div></div>";
}
return $result;
}

function ReturnAddLinkHTML($ProductID, $LinkText) {
$result = "<a href=\"" . $_SERVER["PHP_SELF"] . "?cartaction=add&cartitem=" . $ProductID . "&carttoken=" . mt_rand() . "\">" . $LinkText . "</a>";
return $result;
}

function ReturnMultiItemAddFormHTML($ProductIDs) {
// Returns the HTML for a multiple-item drop down, with add button.  This is helpful for multiple grouped items (such as an item with colors or sizes)
// Call it with a semi-colon delimited list, like "SHIRT1;SHIRT2;"

$result = "";

if (strlen($ProductIDs) > 0) {	
	$result = "<form method=\"get\" action=\"" . $_SERVER["PHP_SELF"] . "\"><select name=\"cartitem\"><option value=\"\">Please Select</option>";

	$ProductIDArray = explode( ";", $ProductIDs);
	foreach($ProductIDArray as $ProductID) {
		if (strlen($ProductID) > 0) {
			$result .= "<option value=\"" . $ProductID . "\">" . ReturnProductDescriptionFromID($ProductID) . "</option>";
		}
	}
	$result .= "</select><input type=\"hidden\" name=\"cartaction\" value=\"add\" /><input type=\"hidden\" name=\"carttoken\" value=\"" . mt_rand() . "\" /> <input type=\"submit\" value=\"Add To Cart\" /></form>";
}

return $result;
}

function ReturnProductDescriptionFromID($ProductID) {
// Returns the description for the supplied ID
$result = "No Product Found: " . $ProductID;
if (isset($_SESSION[CART])) {
	foreach($_SESSION[CART] as $Product)
	{
		if ($Product[iD_COLUMN] == $ProductID) {
			$result = $Product[DESCRIPTION_COLUMN];
			break;
		}
	}	
}	
return $result;
}

function ReturnConditionalClearLinkHTML($HeaderHTML, $FooterHTML, $LinkText) {
// Returns a link, only if the cart isn't empty
$result = "";
if (ReturnCartEmpty() == false) {
	$result = $HeaderHTML . "<a href=\"" . $_SERVER["PHP_SELF"] . "?cartaction=clear&carttoken=" . mt_rand() . "\">" . $LinkText . "</a>" . $FooterHTML;
}	
return $result;
}

function ReturnCartEmpty() {

$result = true;

if (isset($_SESSION[CART])) {
	foreach($_SESSION[CART] as $Product)
	{
		if (Nsz($Product[QUANTITY_COLUMN], 0) > 0) {
			$result = false;
			break;
		}
	}	
}
return $result;
}

function ReturnCartText() {

$result = "";
$running_total = 0;

if (isset($_SESSION[CART])) {
	foreach($_SESSION[CART] as $Product)
	{
		// Show only items that have a quantity > 0
		if (Nsz($Product[QUANTITY_COLUMN], 0) > 0) {
			$running_total += ($Product[PRICE_COLUMN] * $Product[QUANTITY_COLUMN]);
			$result .= "Product ID: " . $Product[iD_COLUMN] . "\n" . "Product Name: " . $Product[NAME_COLUMN] . "\n" . "Product Description: " . $Product[DESCRIPTION_COLUMN] . "\n" . "Product Price (Each): " . CURRENCY_SYMBOL . number_format($Product[PRICE_COLUMN],2) . "\n" . "Product Quantity: " . $Product[QUANTITY_COLUMN] . "\n" . "Quantity Price: " . CURRENCY_SYMBOL . number_format($Product[PRICE_COLUMN] * $Product[QUANTITY_COLUMN],2) . "\n\n";
		}
	}	
}
return $result;
}

function ReturnCartItemCount() {
$items_in_cart = 0;
if (isset($_SESSION[CART])) {
	foreach($_SESSION[CART] as $Product){
		// Only items that have a quantity > 0
		if (Nsz($Product[QUANTITY_COLUMN], 0) > 0) {
			$items_in_cart++;
		}
	}	
}
return $items_in_cart;
}

function Nsz($value, $value_if_not_set) {
// Works similarly to NullZero, returns second parameter value if the first parameter isn"t set
if (isset($value)) {
	return $value;
}
else {
	return $value_if_not_set;
}
}
?>

 

jars.php (there's a tiny bit of HTML at the bottom that I've omitted, but it's not relevant)

<?
include('include/functions.php');

function LoadProductsFromFile() {

   if (file_exists(JARS_FILE_PATH)) {
	//Read it
	$Products_array = file(JARS_FILE_PATH);
	foreach($Products_array as $value)
	{
	    $Product = explode(FILE_DELIMITER,$value);
		$cartitem = array($Product[iD_COLUMN], $Product[NAME_COLUMN], $Product[PRICE_COLUMN], $Product[DESCRIPTION_COLUMN], 0);
		$items[$Product[iD_COLUMN]] = $cartitem;
	}

	$_SESSION[CART] = $items;		
}
}

include 'header.php';
include 'categories.php';
?>
<td class="main" align="center">
						<tr>
							<td>Jars</td>
						</tr>
						<tr>
							<td><img src="images/jars/img01.jpg"></td>
							<td>$13.49<br />
							    6 @ $12.99<br />
								12 @ $12.49</td>
							<td><?php echo ReturnAddLinkHTML("JR-GJ4", "Add to Cart"); ?></td>
						</tr>
						<tr>	
							<td><img src="images/jars/img02.jpg"></td>
							<td>$14.99<br />
								6 @ $14.29<br />
								12 @ $13.75</td>
							<td><?php echo ReturnAddLinkHTML("JR-GJ2", "Add to Cart"); ?></td>
</table>

</td>
<!--end main content-->
</tr>
</table>
<?
include 'footer.php';
?>

 

And finally, the cart.php (again, I've omitted the irrelevant HTML closing tags at the bottom)

<?
include('include/functions.php');

function LoadProductsFromFile() {

   if (file_exists(PRODUCTS_FILE_PATH)) {
	//Read it
	$Products_array = file(PRODUCTS_FILE_PATH);
	foreach($Products_array as $value)
	{
	    $Product = explode(PRODUCTS_FILE_DELIMITER,$value);
		$cartitem = array($Product[iD_COLUMN], $Product[NAME_COLUMN], $Product[PRICE_COLUMN], $Product[DESCRIPTION_COLUMN], 0);
		$items[$Product[iD_COLUMN]] = $cartitem;
	}

	$_SESSION[CART] = $items;		
}
}

include 'header.php';
include 'categories.php';
?>
<!--begin main content-->
<td class="main" align="justified">
<h2>Your Shopping Cart</h2>
<?php
	// Call with parameter 1 to have add/remove links displayed
	echo ReturnCartHTML(1);
?>
<h2>Checkout Link</h2>
<p>
	<?php
		if (ReturnCartItemCount()>0) {
			echo "<a href=\"checkout.php\">Click here to check out</a>";
		}
		else {
			echo "Checkout link isn't displayed now, as your cart is empty.";
		}
	?>
</p>
</td>
<!--end main content-->
</tr>
</table>
<?
include 'footer.php';
?>

 

Any suggestions or ideas?

 

Link to comment
https://forums.phpfreaks.com/topic/105589-some-sort-of-session-issue/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.