Page 1 of 1

cart - adding sub total

Posted: Wed Aug 11, 2004 5:22 am
by C_Calav
hi guys,

how can i go about adding in another total feild for EACH product that changes as my qty changes. this would also change the Total to. so i guess its a sub-total.

i dont know where to start, any pointers would be much apprecated!

thanx.


Code: Select all

<?php 
session_start(); 

// This page contains the connection routine for the 
// database as well as getting the ID of the cart, etc 
 $db = mysql_pconnect('db.iserve.net.nz', 'chris', 'cessna') or die ("Could not connect to database");

    if (!$db)
    {
     echo 'Error: Could not connect to database.  Please try again laterrrrrr.';
     exit;
    }

    mysql_select_db('models') or die ("Could not select database!"); 

function ConnectToDb($server, $user, $pass, $database) 
{ 
// Connect to the database and return 
// true/false depending on whether or 
// not a connection could be made. 
$s = @mysql_connect($server, $user, $pass); 
$d = @mysql_select_db($database, $s); 
if(!$s || !$d) 
return false; 
else 
return true; 
} 
function GetCartId() 
{ 
// This function will generate an encrypted string and 
// will set it as a cookie using set_cookie. This will 
// also be used as the cookieId field in the cart table 
if(isset($_COOKIE["cartId"])) 
{ 
return $_COOKIE["cartId"]; 
} 
else 
{ 
// There is no cookie set. We will set the cookie 
// and return the value of the users session ID 
setcookie("cartId", session_id(), time() + ((3600 * 24) * 30)); 
return session_id(); 
} 
} 




    $db = mysql_pconnect('db.iserve.net.nz', 'chris', 'cessna') or die ("Could not connect to database");

    if (!$db)
    {
     echo 'Error: Could not connect to database.  Please try again laterrrrrr.';
     exit;
    }

    mysql_select_db('models') or die ("Could not select database!"); 


   //include("db.php"); 
        
   switch($_GET["action"]) 
   { 
      case "add_item": 
      { 
         AddItem($_GET["id"], $_GET["qty"]); 
         ShowCart($rs_cart); 
         break; 
      } 
      case "update_item": 
      { 
         UpdateItem($_GET["id"], $_GET["qty"]); 
         ShowCart($rs_cart); 
         break; 
      } 
      case "remove_item": 
      { 
         RemoveItem($_GET["id"]); 
         ShowCart($rs_cart); 
         break; 
      } 
      default: 
      { 
         ShowCart($rs_cart); 
      } 
   } 

   function AddItem($itemId, $qty) 
   { 
      // Will check whether or not this item 
      // already exists in the cart table. 
      // If it does, the UpdateItem function 
      // will be called instead 
        
      // Get a connection to the database 
        
      // Check if this item already exists in the users cart table 
      $result = mysql_query("SELECT count(*) FROM cart WHERE cookieId = '" . GetCartId() . "' AND itemId = $itemId"); 
      $row = mysql_fetch_row($result); 
      $numRows = $row[0]; 
        
      if($numRows == 0) 
      { 
         // This item doesn't exist in the users cart, 
         // we will add it with an insert query 

         @mysql_query("INSERT INTO cart(cookieId, itemId, qty) VALUES('" . GetCartId() . "', $itemId, $qty)"); 
      } 
      else 
      { 
         // This item already exists in the users cart, 
         // we will update it instead 
          
         UpdateItem($itemId, $qty); 
      } 
   } 
    
   function UpdateItem($itemId, $qty) 
   { 
      // Updates the quantity of an item in the users cart. 
      // If the qutnaity is zero, then RemoveItem will be 
      // called instead 


      if($qty == 0) 
      { 
         // Remove the item from the users cart 
         RemoveItem($itemId); 
      } 
      else 
      { 
         mysql_query("UPDATE cart SET qty =  $qty WHERE cookieId = '" . GetCartId() . "' AND itemId = $itemId"); 
      } 
   } 
    
   function RemoveItem($itemId) 
   { 
      // Uses an SQL delete statement to remove an item from 
      // the users cart 


      mysql_query("DELETE FROM cart WHERE cookieId = '" . GetCartId() . "' AND itemId = $itemId"); 
   } 
    
   function ShowCart() 
   { 
      // Gets each item from the cart table and display them in 
      // a tabulated format, as well as a final total for the cart 
        

      // Get a connection to the database 
        
      $totalCost = 0; 
      $result = mysql_query("select * from cart inner join planes on cart.itemId = planes.itemId where cart.cookieId = '" . GetCartId() . "' order by planes.P_Name asc");   
      ?>         





<table width="100%" border="0" cellpadding="0" cellspacing="0">
              <!--DWLayoutTable-->
              <tr> 
                <td width="78" height="21" valign="top"><img src="Website_Pics/Cart_Qty.gif" width="33" height="19"></td>
                <td colspan="2" valign="top"><img src="Website_Pics/Cart_Name.gif" width="56" height="19"></td>
                <td width="85" valign="top"><img src="Website_Pics/Cart_Price.gif" width="56" height="19"></td>
                <td width="167" valign="top"><!--DWLayoutEmptyCell-->&nbsp;</td>
              </tr>
              <tr> 
                <td height="19" colspan="5" valign="top"><hr size="1" color="#666666" NOSHADE> 
                </td>
              </tr>
              <? 
            	 while($row = mysql_fetch_array($result))
      			 { 
      		 	// Increment the total cost of all planes 
    		    $totalCost += ($row["qty"] * $row["P_Price"]); 
    		   ?>
              <tr> 
                <td height="24" valign="middle"><font face="verdana" size="1" color="black"> 
                  <select name="<?php echo $row["itemId"]; ?>" onChange="UpdateQty(this)">
                    <?php 
      for($i = 1; $i <= 20; $i++) 
      { 
      echo "<option "; 
      if($row["qty"] == $i) 
      { 
      echo " SELECTED "; 
      } 
      echo ">" . $i . "</option>"; 
      } 
      ?>
                  </select>
                  </font> </td>
                <td colspan="2" valign="middle"><font face="verdana" size="1" color="black"> 
                  <?php echo $row["P_Name"]; ?> </font></td>
                <td valign="middle"><font face="verdana" size="1" color="black"> 
                  $<?php echo number_format($row["P_Price"], 2, ".", ","); ?> 
                  </font> </td>
                <td align="center" valign="middle"><font face="verdana" size="1" color="black"> 
                  <a href="cart.php?action=remove_item&id=<?php echo $row["itemId"]; ?>">Remove</a> 
                  </font> </td>
              </tr>
              <tr> 
                <td height="19" colspan="5" valign="top"><hr size="1" color="#666666" NOSHADE></td>
                <? 
                 } //You have to have this read as php 
                ?>
              </tr>
              <tr> 
                <td height="19" colspan="2" valign="middle"><font face="verdana" size="1" color="black"><a href=1st_flight.php>continue 
                  shopping</a></font></td>
                <td width="62" valign="middle"><img src="Website_Pics/Cart_Total.gif" width="62" height="19"></td>
                <td colspan="2" valign="middle"><b><font face="verdana" size="1" color="black"> 
                  $<?php echo number_format($totalCost, 2, ".", ","); ?> </font> 
                  </b> </td>
              </tr>
              <tr> 
                <td height="0"></td>
                <td width="107"></td>
                <td></td>
                <td></td>
                <td></td>
              </tr>
            </table>



?>

Posted: Wed Aug 11, 2004 11:26 am
by feyd
normally, you'd have a products table, where the fields would be something like:
id, name, manufacturer, manufacturer sku, price, qty available

then you'd have a row for each product, not a field.