View Single Post
  #23 (permalink)  
Old 06-24-08, 09:27 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
This is a revised version of ShoppingCart.php with all the junk removed.

ShoppingCart.php
PHP Code:

<?php
class ShoppingCart {
    private 
$DBConnect "";
    private 
$DBName "";
    private 
$TableName "";
    private 
$ItemCount 0;
     public 
$Orders = array();
     public 
$count 0;

    public function 
__construct() {
       
$this->DBConnect = @new mysqli("localhost""root""");
        if(
mysqli_connect_errno()) {
           die(
"<p>Unable to connect to the database server.</p><p>Error code ".mysqli_connect_errno().": ".mysqli_connect_error())."</p>";
        }
    }

    public function 
__destruct() {
       
$this->DBConnect->close();
    }

    public function 
setDatabase($Database) {
       
$this->DBName $Database;
       @
$this->DBConnect->select_db($this->DBName) or die("<p>Unable to select the database.</p><p>Error code ".mysqli_errno($this->DBConnect).": ".mysqli_error($this->DBConnect))."</p>";
    }

    public function 
setTable($Table) {
       
$this->TableName $Table;
    }

    public function 
getProductList() {
       
$SQLstring "SELECT * FROM $this->TableName";
       
$QueryResult $this->DBConnect->query($SQLstring) or die("<p>Unable to perform the query.</p><p>Error code ".mysqli_errno($this->DBConnect).": ".mysqli_error($DBConnect))."</p>";
       echo 
"<table width='100%' border='1'><tr><th>Product</th><th>Description</th><th>Price Each</th><th>Select Item</th></tr>";
       
$Row $QueryResult->fetch_row();
       while(
$Row) {
          echo 
"<tr><td style='padding-left:5px;'>{$Row[1]}</td><td style='padding-left:5px;'>{$Row[2]}</td>";
          
printf("<td align='center'>$%.2f</td>"$Row[3]);
          echo 
"<td align='center'><a href='ShowCart.php?PHPSESSID=".session_id()."&operation=addItem&productID=".$Row[0]."'>Add</a></td></tr>";
          
$Row $QueryResult->fetch_row();
       }
       echo 
"</table>";
    }

    public function 
addItem() {
       
$ProdID $_GET["productID"];
       for(
$i=0;$i<count($this->Orders);$i++) {
          if(
$this->Orders[$i][0]==$ProdID) {
             exit(
"<div style='width:385px;padding:5px;border:2px solid #ff0000;color:#cc0000;font-zixe:20px;font-weight:bold;'>You already selected that item!  Please choose a category.</div>");
          }
       }
       
$this->Orders[$this->ItemCount][0] = $ProdID;
       
$this->Orders[$this->ItemCount][1] = $this->TableName;
       
$this->ItemCount++;
    }

    public function 
__wakeup() {
       
$this->DBConnect = @new mysqli("localhost""root""");
       if(
mysqli_connect_errno()) {
          die(
"<p>Unable to connect to the database server.</p><p>Error code ".mysqli_connect_errno().": ".mysqli_connect_error())."</p>";
       }
       @
$this->DBConnect->select_db($this->DBName) or die("<p>Unable to select the database.</p><p>Error code ".mysqli_errno($this->DBConnect).": ".mysqli_error($this->DBConnect))."</p>";
    }

    public function 
showCart() {
       
$sw 0;
       for(
$i=0;$i<count($this->Orders);$i++) {
          
$Order $this->Orders[$i];
          
$Table_Name $Order[1] ? $Order[1] : "";
          
$ProdID $Order[0] ? $Order[0] : "";
          
$OrderQTY 2;
          if(
$Table_Name && $ProdID) {
             if(
$sw == 0) {
                echo 
"<table width='100%' border='1'><tr><th>Remove Item</th><th>Product</th><th>Quantity</th><th>Price Each</th></tr>";
                
$sw 1;
             }
             
$SQLstring "SELECT * FROM ".$Table_Name." WHERE productID='".$ProdID."'";
             
$QueryResult = @mysqli_query($this->DBConnect$SQLstring) or die("<p>Unable to perform the query.</p><p>Error code ".mysqli_errno($this->DBConnect).": ".mysqli_error($this->DBConnect))."</p>";
             
$Row mysqli_fetch_row($QueryResult);
             
$Total += $Row[3] * $OrderQTY;
             
$Total_total += $Total;
             
$Total 0;
             echo 
"<tr>
                   <td align='center'><a href='ShowCart.php?PHPSESSID="
.session_id()."&operation=removeItem&productID=".$Row[0]."'>Remove</a></td>
                   <td style='padding-left:10px;'>
{$Row[1]}</td>
                   <td align='center'>
$OrderQTY</td>";
                   
printf("<td align='center'>$%.2f</td></tr>"$Row[3]);
             
$found TRUE;
             
$this->count++;
          }
       }
       if(!
$found){echo "<table width='100%' border='1'><tr>";}
       else {
          
$link $Total_total "<a href='ShowCart.php?PHPSESSID=".session_id()."&operation=emptyCart'><strong>Empty Cart</strong></a>" "";
          echo 
"<tr><td align='center'>$link</td>";
       }
       echo 
"<td colspan='2'><strong>&nbsp;&nbsp;&nbsp;Your shopping cart contains ".$this->count." product(s).</strong></td>";
       
printf("<td align='center'><strong>Total: $%.2f</strong></td></tr>"$Total_total);
       echo 
"</table>";
       
$this->count 0;
    }

    public function 
removeItem() {
       
$ProdID $_GET['productID'];
       for(
$i=0;$i<count($this->Orders);$i++) {
          if(
$this->Orders[$i][0] == $ProdID) {
             
$this->Orders[$i][0] = "";
             
$this->Orders[$i][1] = "";
          }
       }
    }

    public function 
emptyCart() {
       for(
$i=0;$i<count($this->Orders);$i++) {
          
$this->Orders[$i][0] = "";
          
$this->Orders[$i][1] = "";
       }
    }
}
?>
__________________
Jerry Broughton
Reply With Quote