View Single Post
  #2 (permalink)  
Old 08-02-2007, 01:19
mephisto's Avatar
mephisto mephisto is offline
Senior Member
 
Join Date: Feb 2007
Location: Darlington
Posts: 101
Send a message via ICQ to mephisto Send a message via MSN to mephisto
Default

Your error is because you are declaring $db inside the AddItem function in kelly.php, but not in UpdateItem or RemoveItem. Personally I'd use a class.

PHP Code:
<?php
    
class Kelly {
        private 
$db;
        public function 
__construct() {
            
$this->db = new mysqli('localhost''root''''test');
            
$this->db->select_db('test');
        }
        
        public function 
AddItem($itemID$qty) {
            
$query "select count(*) from cart where cookieId = '" GetCartId() . "' and itemId = $itemId";
            
$result $this->db->query($query);
            
$row $result->fetch_assoc();
            
$numRows $row[0];
            if(
$numRows == 0) {

                
// This item doesn't exist in the users cart,
                // we will add it with an insert query

                
$query="insert into cart(cookieId, itemId, qty) values('" GetCartId() . "', $itemId, $qty)";
                
$result=$this->db->query($query);
            }
            else {
                
// This item already exists in the users cart,
                // we will update it instead

                
$this->UpdateItem($itemId$qty);
            }
        }

        public function 
UpdateItem($itemID$qty) {
            
$query="update cart set qty = $qty where cookieId = '" GetCartId() . "' and itemId = $itemId";
            
$result=$this->db->query($query);
        }

        public function 
RemoveItem($itemId){
            
$query="delete from cart where cookieId = '" GetCartId() . "' and itemId = $itemId";
            
$result=$this->db->query($query);
        }


        public function 
ShowCart(){
            
$query="select * from cart inner join items on cart.itemId = items.itemId where cart.cookieId = '" GetCartId() . "' order by items.itemName asc";
            
$result=$this->db->query($query);
            
$output "";
            while(
$row $result->fetch_assoc()) {
                
// Increment the total cost of all items
                
$totalCost += ($row["qty"] * $row["itemPrice"]);
                
$itemName $row["itemName"];
                
$itemID $row["itemId"];
                
$formatedPrice number_format($row["itemPrice"], 2"."",");
                
$output. = <<< EOT
<td width="55%" height="25">
<font face="verdana" size="1" color="black">
{$itemName}
</font>
</td>
<td width="20%" height="25">
<font face="verdana" size="1" color="black">
{$formatedPrice}
</font>
</td>
<td width="10%" height="25">
<font face="verdana" size="1" color="black">
<a href="cart.php?action=remove_item&id=
{$itemID}">Remove</a>
</font>
</td>
</tr>
EOT;
            }
            
            
$formatedTotal number_format($totalCost2"."",");
            
            
$output .= <<< EOT
<tr>
<td width="100%" colspan="4">
<hr size="1" color="red" NOSHADE>
</td>
</tr>
<tr>
<td width="70%" colspan="2">
<font face="verdana" size="1" color="black">
<a href="products.php"><< Keep Shopping</a>
</font>
</td>
<td width="30%" colspan="2">
<font face="verdana" size="2" color="black">
<b>Total: 
${$formatedTotal}</b>
</font>
</td>
</tr>
EOT;
        }
    }
?>
Note, all I have done here is took your functions and put them in a class, so that they will use the same database connection. There are still plenty of improvments that could be made, but that's another issue.
Reply With Quote