Page 1 of 1

global variable - value doesn't change [SOLVED]

Posted: Mon Oct 01, 2007 3:32 am
by crazytopu
The rules are:

1. A product entry must start with a startProduct() call.
2. A product entry must end with a closeProduct() call.
3. In between can go other functions such as setPrice(20.00) or setTitle("Pillow")

I am a bit puzzled. Hope someone can help. In my test page I am including a closeProduct() (see description below) call to test if the error message is being echoed when there is no productStart() call prior to closeProduct() call.

At the very top of the programme I have a global variable called $productStart whose initial value is set to 0. This indicates no productStart() function has been called yet in the test page.

In closeProduct(), I am checking the value of $productStart and if it is still 0 means no startProduct() call is made and hence this is an error.

Now the problem I am facing is even when I am making valid use of startProduct() followed by closeProduct() I still get the error.

I understand I am correctly changing the value inside startProduct() from 0 to 1 and hence shouldnot get any error.

$productStart = 1;

And when there is a closeProduct() call I am assigning 0 to the global variable. Why does the variable always have a value 0?

Code: Select all

<?php


///////////////////////////////////////////////////////////////////////////////////////////////////////////
// Tinycart API version 2007topu1.0
// Author : CrazyTopu
// Purpose: This version of Tinycart API includes all necessary functions for TinyCart Shopping Application
///////////////////////////////////////////////////////////////////////////////////////////////////////////


/* Declare all the global variables that are needed through out the API */

$tinyCartVersion;
$productStart=0;
$productTerminated='true';
$productIdent='0';
$productCode='-1';
$productID;
$productName;
$productDetials;
$price;
$img;
$catalogueRef;
$description;
$newPrice;
$attributes;
$currencyUsed;


		
// include the file that contain the data required to make connection to the database server
include 'db.php';
 
//open a connection to the database and select database; this connection will be used in all the API functions

connectDB();

// ************** function to start a product entry  ************** //
//**************  This function starts a product entry and sets the productID as supplied by the admin **************  //
function startProduct($productId){
		
			
			global $productCode;
			global $productID;
			global $productStart;
			$productID=$productId;		
			$productCode= $productId;
	        
			$productStart = 1;
			//echo "product start in startProduct =".$productStart;
			
			echo "<form method=Get action=\"addToCart.php\"  name=\" add to cart\"> ";
			//mark that a product entry has started
			
			
			//check is made to see if the right version of Tinycart API is included
			checkTinyCartApiInit();
}


// ************** function to close a product entry  ************** //
//**************  This function close a product entry and sets a few values to null **************  //		
function closeProduct(){
		
			global $productStart;
			global $productID;
			global $productName;
			global $attributes;
			
			//echo "product start =".$productStart;
			
			if($productStart==0){
			
			echo "<SCRIPT LANGUAGE=\"javascript\">";
			echo "beAlert(\" Error! No productStart() call is made\")";
			echo "</SCRIPT>";
			
			echo "<div class=errorBoxSmall><span class=error>Error! No productStart() call is made</span></div>";
			
			}
			
			else{
			
			echo "</form>";
			$productStart=0;
			$productID = "";
			$productName = "";
			$attributes = "";
					
			}
}
?>

Posted: Mon Oct 01, 2007 4:33 am
by crazytopu
I've solved the issue using two functions:

setProductStatus() and getProductStatus()