Jump to content

shopping cart with products from 2 sources


lional

Recommended Posts

I am trying to write a shopping cart with firstly a predefined supply of supplements that hets added to the cart which is defined with this code:

$query_aa = "SELECT * from client_supps WHERE client_id = '$client_id_out'";
$result_aa = mysql_query($query_aa, $conn);
while ($row_aa = mysql_fetch_assoc($result_aa)){
  $supp_id_out = $row_aa["supp_id"];
$qty_out = $row_aa["qty"];

     if ($qty_out > 0) {  
      print <<<PRICES
      <input type="hidden" name="product[$supp_id_out]" value="$qty_out">
PRICES;
}
}

and a list of supplements that the client cannot choose quantities but can select which supplements they require, co I wanted to do it with checkboxes for selecting the supplements which this is the code for that section

$query = "SELECT * FROM supplements WHERE supp_id = '$supp_id_out'";
		$result = mysql_query($query) or die(mysql_error());
		while ($row = mysql_fetch_assoc($result)){
	$optional_supplement_out = $row['supplement'];	
	$optional_conversion_out = $row['conversion'];
	$optional_assay_out = $row['assay'];
	$optional_list_price_out = $row['price'];
	//$optional_list_price_out = number_format($optional_list_price_out, 2, '.', '');
	$optional_dosage = (($qty_out / $optional_conversion_out)/$optional_assay_out) * 30;
	$optional_dosage = round($optional_dosage, 2);
	//$optional_cap = ($total_optional_dosage / $cap_optional_weight_out) * 0.1;
	$optional_line_total = $optional_list_price_out * $optional_dosage;
	$optional_line_total = $optional_line_total *1.3;
	$optional_line_total = number_format($optional_line_total, 2, '.', '');

       
      print <<<PRICES
  <tr>
      <td><input type="checkbox" name="product[$supp_id_out]" value="$opt_qty_out"></td>
  <td><font face="arial" size="2" >$optional_supplement_out</td>
  <td align="right" width="120"><font face="arial" size="2" >R $optional_line_total</td>
  </tr>
PRICES;
}}

My problen is that I do not know how to add the section where they choose supplements to the cart. I thought that if the checkbox was unchecked the value would be 0 and I could work on value > 0.

Here is the code for my view cart


    
foreach($_POST['product'] as $key => $value)
if ($value > 0) {
{
$qty1 = $value;
$pid = $key;


	// add to the cart session variable
	$_SESSION['cart'][$pid] = $qty1;
}
}


if (isset($_POST['submit'])) {
		foreach ($_POST['product'] as $key => $value) {
		 if (($value == 0) AND (is_numeric($value))) {
				unset ($_SESSION['cart'][$key]);
				} elseif (is_numeric($value) AND ($value > 0)) {
					$_SESSION['cart'][$key] = $value;
				}
			}

			}

		// check if the shopping cart is empty
		$empty = TRUE;
		if (isset($_SESSION['cart'])) {
			foreach($_SESSION['cart'] as $key => $value) {
				if (isset($value)) {
					$empty = FALSE;

				}
			}
		}
	// Display the cart if it is not empty
	if (!$empty) {
	print <<<TOP
		 <table width="790" align="center" bgcolor="white" cellpadding="0" cellspacing="0">
          <tr><td height="450">
					<table summary="" width="560" align="center" bgcolor="white">
<tr>
<td nowrap align="center"><font face="arial" size="2"><b>Your Metabolic Type Supplements</b></font></tr></table>
					<table summary="" align="center" width="560" border="1">
					<tr>
					<td valign="top">
					<table border="0" width="560" cellpadding="3"bgcolor="white">
					<tr>
					<td align="left" width="250" valign="top"><font face="arial" size="2" ><b>Description</b></font></td>


					<td align="right" width="120"><font face="arial" size="2" ><b>Cost</b></font></td>
					</tr>
			<form action="view_cart.php" method="post">		
TOP;
    include 'includes/conn_db.php';
// pull weight and price for capsules
$query_cap = "SELECT * from capsules";
		$result_cap = mysql_query($query_cap, $conn);
		while ($row_cap = mysql_fetch_assoc($result_cap)){
		$cap_amino_weight_out = $row_cap["amino_weight"];
		$cap_mineral_weight_out = $row_cap["mineral_weight"];
		$cap_vitamin_weight_out = $row_cap["vitamin_weight"];
		$cap_other_weight_out = $row_cap["other_weight"];
		$cap_price_out = $row_cap["price"];
		$cap_labour_out = $row_cap["labour"];
		$cap_packaging_out = $row_cap["packaging"];
		}
					// Retrieve all of the information for the products in the cart
		$query = 'SELECT * FROM supplements WHERE supp_id IN ('; 
		foreach ($_SESSION['cart'] as $key => $value) {
		$query .= $key . ',';
		}
		$query = substr ($query, 0, -1) . ') AND  supp_cat = "1"';
		$result = mysql_query($query) or die(mysql_error());
		// create a table and a form
		// Print each item
		$total = 0; // total cost of the order
		while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
		/*
		if ($row['specials_discount'] > 0) {
		$list_price_out = $row['price'] / $row['specials_discount'];
		} else {
      $list_price_out = $row['price'];
      }
      */
  $supplements_out = $row['supplement'];
      $amino_conversion_out = $row['conversion'];
  $amino_assay_out = $row['assay'];
      $amino_list_price_out = $row['price'];
  
  $amino_list_price_out = number_format($amino_list_price_out, 2, '.', '');
  $amino_dosage = (($_SESSION['cart'][$row['supp_id']] / $amino_conversion_out)/$amino_assay_out) * 30;
  $amino_dosage = round($amino_dosage, 2);
  $total_amino_dosage = $total_amino_dosage + $amino_dosage;
  $amino_cap = ($total_amino_dosage / $cap_amino_weight_out) * 0.1;
  $amino_line_total = $amino_list_price_out * $amino_dosage;
  $amino_line_total = number_format($amino_line_total, 2, '.', '');
  $amino_acids_subtotal = $amino_acids_subtotal + $amino_line_total;
  print <<<ROW
					<tr>

					<td align="left"><font face="arial" size="2">$supplements_out</font></td>


            

<td align="right" width="120"><font face="arial" size="2">R $supplement_price</font></td>
</tr>
ROW;
   }

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.