Class Cart {//start shopping cart class function Check_item ($table, $session, $product) { /* Check items (table name, session, item) */ $query = SELECT * from $table WHERE session= ' $session ' and product= ' $product '; /* Take a look at the ' watch ' in the ' shopping cart ' for the ' product ' That is, the product has not been put into the shopping cart */ $result = mysql_query ($query); if (! $result) { return 0; } /* Query failed */ $numRows = mysql_num_rows ($result); if ($numRows = = 0) { return 0; /* If not found, return 0 */ } else { $row = Mysql_fetch_object ($result); return $row->quantity; /* If found, returns the number of items There is a need to explain the Mysql_fetch_object function (which is also used below): "Mysql_fetch_object () and mysql_fetch_array () are similar, with a little difference--returning an object instead of an array. 】 The above sentence is excerpted from the PHP manual, it should be very clear to say it ~ To put it simply, take a field from a record and use "->" instead of the subscript as an array. */ } } function Add_item ($table, $session, $product, $quantity) { /* Add new items (table name, session, items, quantity) */ $qty = $this->check_item ($table, $session, $product); /* Call the above function and check to see if the item has been put in the car. */ if ($qty = = 0) { $query = INSERT into the $table (session, product, quantity) VALUES; $query. = (' $session ', ' $product ', ' $quantity '); mysql_query ($query); /* If the car does not, then like to add the item in the car * * } else { $quantity + + $qty; If so, increase the quantity on the original basis $query = UPDATE $table SET quantity= ' $quantity ' WHERE session= ' $session ' and; $query. = product= ' $product '; mysql_query ($query); /* and modify the database */ } } function Delete_item ($table, $session, $product) { /* Delete items (table name, session, item) */ $query = DELETE from $table WHERE session= ' $session ' and product= ' $product '; mysql_query ($query); /* Delete the item in the Cart */ } function modify_quantity ($table, $session, $product, $quantity) { /* Modify the number of items (table name, session, items, quantity) */ $query = UPDATE $table SET quantity= ' $quantity ' WHERE session= ' $session '; $query. = and product= ' $product '; mysql_query ($query); /* Modify the quantity of the item to the value in the parameter */ } function Clear_cart ($table, $session) { /* Empty the shopping cart (nothing to say) */ $query = DELETE from $table WHERE session= ' $session '; mysql_query ($query); } function Cart_total ($table, $session) { /* Total price of goods in car */ $query = SELECT * from $table WHERE session= ' $session '; $result = mysql_query ($query); /* Get all the items out of the car first. */ if (mysql_num_rows ($result) > 0) { while ($row = Mysql_fetch_object ($result)) { /* If the number of items is >0, determine the price and calculate */ $query = SELECT Price from inventory WHERE product= ' $row->product '; $invResult = mysql_query ($query); /* Find the price of the item from the Inventory (inventory) table */ $row _price = mysql_fetch_object ($invResult); $total + + ($row _price->price * $row->quantity); /* Total Price = The quantity of the item (People should be able to see it:) ) */ } } return $total; Return the total Price } function display_contents ($table, $session) { /* Get more information about all the items in the car */ $count = 0; /* Item Quantity Count Note that this variable is not just about counting the number of items, but more importantly, it will be used as the subscript in the return value array to distinguish each item! */ $query = SELECT * from $table WHERE session= ' $session ' ORDER by ID; $result = mysql_query ($query); /* Take all the items in the car first. */ while ($row = Mysql_fetch_object ($result)) { /* Take detailed information on each item separately */ $query = SELECT * from inventory WHERE product= ' $row->product '; $result _INV = mysql_query ($query); /* Find information about the item from the Inventory (inventory) table */ $row _inventory = mysql_fetch_object ($result _inv); $contents [product][$count] = $row _inventory->product; $contents [price][$count] = $row _inventory->price; $contents [quantity][$count] = $row->quantity; $contents [total][$count] = ($row _inventory->price * $row->quantity); $contents [description][$count] = $row _inventory->description; /* Put all the details about the item into $contents array $contents is a two-dimensional array The first set of subscript is to distinguish each item different information (such as item name, price, quantity, etc.) The second set of subscripts is to distinguish between different objects (this is the role of the $count variables defined earlier) */ $count + +; Item quantity plus one (ie next item) } $total = $this->cart_total ($table, $session); $contents [Final] = $total; /* At the same time, call the Cart_total function above to calculate the total price and put it in $contents array. */ return $contents; /* Returns the array to the */ } function Num_items ($table, $session) { /* Returns the total number of items (that is, two of the same things as a kind of nonsense--!) */ $query = SELECT * from $table WHERE session= ' $session '; $result = mysql_query ($query); $num _rows = mysql_num_rows ($result); return $num _rows; /* Take out all the items in the car and get the number of database rows affected by the operation, that is, the total number of items (nothing to say) */ } function Quant_items ($table, $session) { /* Returns the total number of items (that is, two of the same things are also two items--#) */ $quant = Total amount of 0;//items $query = SELECT * from $table WHERE session= ' $session '; $result = mysql_query ($query); while ($row = Mysql_fetch_object ($result)) { /* Take each item out individually */ $quant + + $row->quantity; The quantity of the item is added to the total amount. } return $quant; Return Total } } |