tamagraphics Posted March 23, 2007 Share Posted March 23, 2007 I am having an issue getting a variable to carry over into a class for a shopping cart system I am tring to work on. I have a variable called $cartid with is develped and stored into a cookie as a session variable. I then call for the start of the class called cart When i run the function called additem($product, $quantity) which is located inside of the class cart I cannot seem to have access to the variable $cartid. If you look at the first function of the script i have it assigning $this->cartid = $cartid; Shouldn't this assign the session variable to the $this->cartid inside the class??? Here is the code below... hope someone can help... if (!isset($_COOKIE['cartid'])) { // If no cookie exists create a new one with a rand integer that is unique and md5 encrpyted for security $cartid = md5(uniqid(rand())); setcookie("cartid", $cartid, time() + 14400); // Set to expire in 1 minute for test purpose - set to 4 hours when live = 14400 3600 is 1 hour } else { // If cookie does exist then grab the id number from the cookie for user data $cartid = $_COOKIE['cartid']; } // Start of shopping Cart class class cart { var $host = "hostname"; var $user = "username"; var $password = "password"; var $data = "databasename"; var $invtable = "inventory"; // Name of the table holding the products information var $carttable = "orders"; // Name of the table holding the information for the shopping carts // Database connection function function cart($cartid) { $this->dblink = mysql_connect($this->host, $this->user, $this->password); mysql_select_db($this->data, $this->dblink); $this->cartid = $cartid; } // Start of function to add to the shopping cart function additem($product, $quantity) { echo $cartid; echo $this->cartid; $qty = $this->checkitem($product); // echo $this->cartid; if ($qty == 0) { $query = "insert into " . $this->carttable . " (session, product, quantity, id) values ('" . $this->cartid . "', '$product', '$quantity', '') "; mysql_query($query, $this->dblink); } else { $quantity += $qty; $query = "update " . $this->carttable . " set quantity = '$quantity' where (session = '" . $this->cartid . "' and product = '$product') "; mysql_query($query, $this->dblink); } return true; } function checkitem($product) { echo $cartid; echo $this->cartid; $query = "select quantity from orders where (session = '" . $this->cartid . "' and product = '$product') "; $result = mysql_query($query, $this->dblink); if (!$result) { return 0; } $numrows = mysql_numrows($result); // echo $numrows; if ($numrows !=0) { $row = mysql_fetch_object($result); return $row->quantity; } } Thank you. Link to comment https://forums.phpfreaks.com/topic/44006-class-and-variable-issue-please-help/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.