Jump to content

Add discount depending on the product:


roldahayes
Go to solution Solved by mac_gyver,

Recommended Posts

Hi,

I hope somebody can point me in the right direction with this.

 

I have a basket page that I would like to automatically add discount to certain products depending on what make or brand they are,

 

The Make is stored in the database as "Product_Make"

 

So if a product is selected that has the make as "Sony"  it would reduce the basket by 15%.

 

I think that the relevant part of the code is here:

 

 

 <input name=productID type=hidden value=". $row['productID'] ."><tr class=stdtable>";
 
  echo("<td align=center> " . htmlspecialchars($row['Prod_Make']) .  "</td>");
 
  echo("<td align=center> " . htmlspecialchars($row['Prod_REF']) .  "</td>");
 
 
 
  echo("<td align=left>" . htmlspecialchars($row['Product_Desc']) . "</td>");
 
  //echo("<td align=center>". htmlspecialchars($row['Prod_REF']) ."</td>");
 
  // for the final column echo an hyperlink to delete the product entry
 
    //settype($row["Price_ExVat"], "integer");
 
  echo "<td align=center><input name=updateQuantity onchange=submit(); type=text size=2 value=". htmlspecialchars($row['quantity'])."></td><td align=center>". $currency . number_format(htmlspecialchars($row['Price_ExVat']), 2) ."</td><td align=center>". $currency . number_format(calcVAT($row["Price_ExVat"]), 2) ."</td><td align=center>". $currency . number_format((calcVAT (htmlspecialchars($row['Price_ExVat'] * $row['quantity']))), 2)  ."</td>";
 
  echo "<td align=center><a href=\"basket.php?delete=yes&productID=" . $row['productID'] . "\"><img src=2003/remove.gif border=0></a></td>";
 
  echo "</tr></form>";
 
  $counter ++;
 
  //get a cumulative value of the price as items are added to the basket and multiply by quantity as we go.
 
  $price = $price + (calcVAT (htmlspecialchars($row['Price_ExVat']))) * htmlspecialchars($row['quantity']);
  }
 
//assign subtotal and round to 2 decimal places
 
$subtotal = $price;
 
$total = $subtotal + $postagerate;
 
//pick overall postage type
 
if ($postagerate == 5)
 
$postage = 3;
 
else if ($postagerate == 9)
 
$postage = 2;
 
else if ($postagerate == 10)
 
$postage = 1;
 
}
 
//update shopper table with new/changed info
 
$sqlshopper = "SELECT * FROM shopper WHERE User_ID = '" . $userID . "'";
 
//echo "query: " . $sqlshopper;
 
$result = mysql_query($sqlshopper);
 
$rowCount2 = mysql_num_rows($result);
 
//add shopper
 
if ($rowCount2 == 0)
 
{
 
$sqladd = "INSERT INTO shopper" . $shopperFields . "VALUES ('" . $userID . "', '" . $total . "', '" . $postage . "')";
 
//echo "noshopper: " . $sqladd;
 
$shopadd = mysql_query($sqladd);
 
if (!$shopadd)
 
echo "<font class=error><p>Your basket has not been processed</p></font>";
 
}
 
else
 
{
 
//update details
 
$sqlupdate = "UPDATE shopper SET Basket_total = '" . $total . "', Postage = '" . $postage . "' WHERE user_ID = '" . $userID . "'";
 
//echo "shopper: " . $sqlupdate;
 
$shopupdate = mysql_query($sqlupdate);
 
if (!$shopupdate)
 
echo "<font class=error><p>Your basket has not been updated</p></font>";
 
}
 
// finish table

 

Thanks in advance,

Edited by roldahayes
Link to comment
Share on other sites

Hi, I'll have a crack at this see if I can point you in a direction.

 

When your pulling the rows out; put a IF statement or a switch statement in your loop for X% discounts:

 

Something along these lines (ONLY MOCK UP CODE!):

// LOOP

$price = $price + (calcVAT (htmlspecialchars($row['Price_ExVat']))) * htmlspecialchars($row['quantity']);
$i = $row['Prod_Make'];

switch ($i) {
    case 'Sony':
        $percentage = 15 / 100;
        $price = ($percentage x $price);
        echo '<OUTPUT>';
        break;
    case 'Kogan':
        // CODE
        break;
    case 'etc':
        // CODE
        break;
}


// END LOOP
Edited by Ansego
Link to comment
Share on other sites

Excellent! This was perfect for starting me off.

 

I used:

 

 

$price = $price + (calcVAT (htmlspecialchars($row['Price_ExVat']))) * htmlspecialchars($row['quantity']);
            $i = $row["Prod_Make"];
 
            switch ($i) {
    case "Sony":
        
($percentage =  15/100);
        $discount = ($percentage * $price);
        
        break;
    case "Etc":
        
 
($percentage =  15/100);
        $discount = ($percentage * $price);
        break;
    case "etc":
        // CODE
 
($percentage =  15/100);
        $discount = ($percentage * $price);
        break;
}
}
            

 

 

The only thing I now need to work out is getting that 15% discount to show per item line in the basket...

 

i.e  the fields of the cart will now read as:

 

Make      Ref No     Description       Quantity       Price ex vat       Price inc vat       Total Discount on this item (-)      

Link to comment
Share on other sites

Nice work man, you can run the output line within these discount lines so IF $i does NOT == 'sony' 'etc' then everything else.

 

 

 

TAKE NOTE OF DEFAULT AND 'NO' DISCOUNT ON THE OUTPUT: ( MOCK CODE ONLY )

	$price = $price + (calcVAT (htmlspecialchars($row['Price_ExVat']))) * htmlspecialchars($row['quantity']);
	$i = $row["Prod_Make"];

	switch ($i) {
			case "Sony":
			
				($percentage =  15/100);
				$discount = ($percentage * $price);
					echo 'Make | Ref No | Description | Quantity | Price ex vat | Price inc vat | Total Discount on this item (-)';  
			
			break;
			case "Etc":
			
				($percentage =  15/100);
				$discount = ($percentage * $price);
					echo 'Make | Ref No | Description | Quantity | Price ex vat | Price inc vat | Total Discount on this item (-)';  
			
			break;
			default:
					echo "$i is not equal to Sony or Etc";
					echo 'Make | Ref No | Description | Quantity | Price ex vat | Price inc vat | Total < NO > Discount on this item (-)';
			}

 

Hope this helps you out ;-).

 

Link to comment
Share on other sites

Hmm,  I seem to be stuck with this now.

 

I have the basket adding the discount to the relevant items and totaling the basket correctly but I need the total discount showing at the end of the items line.

 

I have attached an image to show what I am trying to do. 

 

I need the discount that has been created (i.e the 15% of the £89.95 would be £13.49)  to show up in the discount column which I have coloured red. and then show the total at the end of the line.

 

I hope this makes sense!

 

cart.jpg

Link to comment
Share on other sites

This might help, I had a bit of fun with it, hope you don't mind:

$price = 200;
$percentage =  15/100;
$discount = ($percentage * $price);
$totaldiscount =  $price - $discount;
echo "The item WAS: $$price but your pretty cool, so I gave you a discount of: $percentage% which took OFF: $$discount and your price is as low! low! low! as: $$totaldiscount</br></br></br>";
OUTPUT LOL: The item WAS: $200 but your pretty cool, so I gave you a discount of: 0.15% which took OFF: $30 and your price is as low! low! low! as: $170
Link to comment
Share on other sites

What I don't understand is, the table displaying the information:

echo "<td align=center><input name=updateQuantity onchange=submit(); type=text size=2 value=". htmlspecialchars($row['quantity'])."></td>
     <td align=center>". $currency . number_format(htmlspecialchars($row['Price_ExVat']), 2) ."</td>
 <td align=center>". $currency . number_format(calcVAT($row["Price_ExVat"]), 2) ."</td>
 
 <td align=center>"  THIS IS WHERE I NEED THE DISCOUNT TO SHOW!     "</td>
 <td align=center>". $currency . number_format((calcVAT (htmlspecialchars($row['Price_ExVat'] * $row['quantity']))), 2)  .
 
"</td>";
            
  echo "<td align=center><a href=\"basket.php?delete=yes&productID=" . $row['productID'] . "\"><img src=2003/remove.gif border=0></a></td>";
 
            
  echo "</tr></form>";

Is above the code we now have to work out the discount?

//Loop that will apply the TRADE DISCOUNT 
 
          $price = $price + (calcVAT (htmlspecialchars($row['Price_ExVat']))) * htmlspecialchars($row['quantity']);
            $i = $row["Prod_Make"];
 
            switch ($i) {
    case "Sony":
        
($percentage =  15/100);
        $discount = ($percentage * $price);
        $totaldiscount =  $price - $discount;
 
        break;
    case "Etc":
        
 
($percentage =  15/100);
        $discount = ($percentage * $price);
 
 
                        
        break;
 
}
}
 
 
// END the TRADE DISCOUNT LOOP

Would it not have to be the other way around??

Edited by roldahayes
Link to comment
Share on other sites

The example above I was giving above is an example of the percentage data that you can sort out within your switch statement.

 

Is this code inside a SWITCH STATEMENT and within the loop?

echo "<td align=center><input name=updateQuantity onchange=submit(); type=text size=2 value=". htmlspecialchars($row['quantity'])."></td>
     <td align=center>". $currency . number_format(htmlspecialchars($row['Price_ExVat']), 2) ."</td>
 <td align=center>". $currency . number_format(calcVAT($row["Price_ExVat"]), 2) ."</td>
 
 <td align=center>"  THIS IS WHERE I NEED THE DISCOUNT TO SHOW!     "</td>
 <td align=center>". $currency . number_format((calcVAT (htmlspecialchars($row['Price_ExVat'] * $row['quantity']))), 2)  .
 
"</td>";
            
  echo "<td align=center><a href=\"basket.php?delete=yes&productID=" . $row['productID'] . "\"><img src=2003/remove.gif border=0></a></td>";
 
            
  echo "</tr></form>";

If it is all you need to do is replace 'THIS IS WHERE I NEED THE DISCOUNT TO SHOW!' with 

$$discount";

OR

$percentage%

Depending what you want. Hope this helps. If it doesn't I'll have another look tomorrow, very late here, not enough coffee and have blurry eyes.

Edited by Ansego
Link to comment
Share on other sites

I dont think that it is within the loop which is where I am going wrong with it:

 

 

 <?php
 
}
 
else
 
//*************display contents of basket//////////////////////////////////////////////////////
 
{
 
// echo each header from array
 
//foreach ($dbFields as $headIndex)
 
// echo an extra blank header for the delete item column
 
// fetch each row as an associative array
 
$counter = 1;
 
$price = 0;
 
//set default postage value outside loop
$postagerate = 15.00;
 
while ($row = mysql_fetch_assoc($result))
{
  //decide which postage value is the highest and use that to calculate overall price
  //get the postage values for each product
  $sqlpostquery = "SELECT * FROM postage WHERE Post_ID = '" . htmlspecialchars($row['Post_ID']) . "'";
 
  //get the postage values from the database
  $postresult = mysql_query($sqlpostquery);
  $rowpost = mysql_fetch_assoc($postresult);
 
  // check if postage value was available
  if ($postresult || !(mysql_num_rows($postresult) == 0))
    {
    $rawpostage = htmlspecialchars($rowpost['Post_Cost']) ? htmlspecialchars($rowpost['Post_Cost']) : 0.00;
 
    //get the lowest postage rate.
    if ($postagerate > $rawpostage)
      {
        $postagerate = $rawpostage;
    }
  }
  else
  {
    $postagerage = 0.00;
  }
 
  //round postage rate of 2 decimal places
  $postagerate = $postagerate;
 
  //release the postage resultset array
 
  mysql_free_result($postresult);
 
  echo "<form action=basket.php method=get name=form".$counter.">
 
    <input name=update type=hidden value=yes>
 
    <input name=productID type=hidden value=". $row['productID'] ."><tr class=stdtable>";
 
  echo("<td align=center> " . htmlspecialchars($row['Prod_Make']) .  "</td>");
 
  echo("<td align=center> " . htmlspecialchars($row['Prod_REF']) .  "</td>");
 
 
 
  echo("<td align=left>" . htmlspecialchars($row['Product_Desc']) . "</td>");
 
 
 
 
  //echo("<td align=center>". htmlspecialchars($row['Prod_REF']) ."</td>");
 
  // for the final column echo an hyperlink to delete the product entry
 
    //settype($row["Price_ExVat"], "integer");
 
 
 
//assign subtotal and round to 2 decimal places
 
  echo "<td align=center><input name=updateQuantity onchange=submit(); type=text size=2 value=". htmlspecialchars($row['quantity'])."></td>
     <td align=center>". $currency . number_format(htmlspecialchars($row['Price_ExVat']), 2) ."</td>
 <td align=center>". $currency . number_format(calcVAT($row["Price_ExVat"]), 2) ."</td>
 
 
 <td align=center>". $currency . number_format((calcVAT (htmlspecialchars($row['Price_ExVat'] * $row['quantity']))), 2)  .
 
"</td>";
            
  echo "<td align=center><a href=\"basket.php?delete=yes&productID=" . $row['productID'] . "\"><img src=2003/remove.gif border=0></a></td>";
 
            
  echo "</tr></form>";
 
  $counter ++;
 
  //get a cumulative value of the price as items are added to the basket and multiply by quantity as we go.
 
 
 
 
//Loop that will apply the TRADE DISCOUNT 
// LEAVE THIS HERE INCASE I BREAK THINGS $price = $price + (calcVAT (htmlspecialchars($row['Price_ExVat']))) * htmlspecialchars($row['quantity']);
          $price = $price + (calcVAT (htmlspecialchars($row['Price_ExVat']))) * htmlspecialchars($row['quantity']);
            $i = $row["Prod_Make"];
 
            switch ($i) {
    case "Sony":
        
($percentage =  15/100);
        $discount = ($percentage * $price);
        $totaldiscount =  $price - $discount;
 
        break;
    case "Etc":
        
 
($percentage =  15/100);
        $discount = ($percentage * $price);
 
 
                        
        break;
 
}
}
 
 
// END the TRADE DISCOUNT LOOP
 
  
 
$subtotal = $price - $discount;
 
$total = $subtotal + $postagerate ;
 
//pick overall postage type
 
if ($postagerate == 5)
 
$postage = 3;
 
else if ($postagerate == 9)
 
$postage = 2;
 
else if ($postagerate == 10)
 
$postage = 1;
 
}
 
//update shopper table with new/changed info
 
$sqlshopper = "SELECT * FROM shopper WHERE User_ID = '" . $userID . "'";
 
//echo "query: " . $sqlshopper;
 
$result = mysql_query($sqlshopper);
 
$rowCount2 = mysql_num_rows($result);
 
//add shopper
 
if ($rowCount2 == 0)
 
{
 
$sqladd = "INSERT INTO shopper" . $shopperFields . "VALUES ('" . $userID . "', '" . $total . "', '" . $postage . "')";
 
//echo "noshopper: " . $sqladd;
 
$shopadd = mysql_query($sqladd);
 
if (!$shopadd)
 
echo "<font class=error><p>Your basket has not been processed</p></font>";
 
}
 
else
 
{
 
//update details
 
$sqlupdate = "UPDATE shopper SET Basket_total = '" . $total . "', Postage = '" . $postage . "' WHERE user_ID = '" . $userID . "'";
 
//echo "shopper: " . $sqlupdate;
 
$shopupdate = mysql_query($sqlupdate);
 
if (!$shopupdate)
 
echo "<font class=error><p>Your basket has not been updated</p></font>";
 
}
 
// finish table
 
?>
 
                        </td>
 
                      </tr>
 
                      <?php
 
?>

 

I Appreciate your help with this!

Link to comment
Share on other sites

Id do something like below; Code NOT TESTED you will need to debug it and tweak to get to work.  Hope it helps.

 

PS: Keep you code clean, tight and indented, will help you read it better.

<?php
 
} else {
//*************display contents of basket//////////////////////////////////////////////////////
 
// echo each header from array
//foreach ($dbFields as $headIndex)
// echo an extra blank header for the delete item column
// fetch each row as an associative array
 
$counter = 1;
$price = 0;

//set default postage value outside loop
$postagerate = 15.00;
 
while ($row = mysql_fetch_assoc($result))
{
  //decide which postage value is the highest and use that to calculate overall price
  //get the postage values for each product
  $sqlpostquery = "SELECT * FROM postage WHERE Post_ID = '" . htmlspecialchars($row['Post_ID']) . "'";
 
  //get the postage values from the database
  $postresult = mysql_query($sqlpostquery);
  $rowpost = mysql_fetch_assoc($postresult);
 
  // check if postage value was available
  if ($postresult || !(mysql_num_rows($postresult) == 0)){
    	$rawpostage = htmlspecialchars($rowpost['Post_Cost']) ? htmlspecialchars($rowpost['Post_Cost']) : 0.00;
 
    //get the lowest postage rate.
    if ($postagerate > $rawpostage){
        $postagerate = $rawpostage;
    }
  }
  else
  {
    $postagerage = 0.00;
  }
 
  //round postage rate of 2 decimal places
  $postagerate = $postagerate;
 
  //release the postage resultset array
  mysql_free_result($postresult);

		// Pecentage discount code - Needs testing:
		
		$price = $price + (calcVAT (htmlspecialchars($row['Price_ExVat']))) * htmlspecialchars($row['quantity']);
		$i = $row["Prod_Make"];
		
		switch ($i) {
		
		case "Sony":
			
			($percentage =  15/100);
			$discount = ($percentage * $price);
			$totaldiscount =  $price - $discount;
			break;
			
		case "Etc":
			($percentage =  15/100);
			$discount = ($percentage * $price);
			break;
			
		default:
			// Nothing
		}

  echo "<form action=basket.php method=get name=form".$counter.">

		<input name=update type=hidden value=yes>
		<input name=productID type=hidden value=". $row['productID'] ."><tr class=stdtable>
		<td align=center> " . htmlspecialchars($row['Prod_Make']) .  "</td>
		<td align=center> " . htmlspecialchars($row['Prod_REF']) .  "</td>
		<td align=left>" . htmlspecialchars($row['Product_Desc']) . "</td>
		<td align=center><input name=updateQuantity onchange=submit(); type=text size=2 value=". htmlspecialchars($row['quantity'])."></td>
		<td align=center>". $currency . number_format(htmlspecialchars($row['Price_ExVat']), 2) ."</td>
		<td align=center>". $currency . number_format(calcVAT($row["Price_ExVat"]), 2) ."</td>
		<td align=center>". $currency . number_format((calcVAT (htmlspecialchars($row['Price_ExVat'] * $row['quantity']))), 2)  . "</td>
		
		<td align=center>" . $totaldiscount . "</td> 
		";
			
	//REMOVE ITEM 
	echo "<td align=center><a href=\"basket.php?delete=yes&productID=" . $row['productID'] . "\"><img src=2003/remove.gif border=0></a></td>";
	echo "</tr></form>";
 
  	$counter ++;
 
		// Loop that will apply the TRADE DISCOUNT 
		// LEAVE THIS HERE INCASE I BREAK THINGS $price = $price + (calcVAT (htmlspecialchars($row['Price_ExVat']))) * htmlspecialchars($row['quantity']);
		$price = $price + (calcVAT (htmlspecialchars($row['Price_ExVat']))) * htmlspecialchars($row['quantity']);
		$i = $row["Prod_Make"];
 
		switch ($i) {
		
		case "Sony":
			
			($percentage =  15/100);
			$discount = ($percentage * $price);
			$totaldiscount =  $price - $discount;
			break;
			
		case "Etc":
			($percentage =  15/100);
			$discount = ($percentage * $price);
			break;
			
		default:
			// Nothing
		}
}

// END the TRADE DISCOUNT LOOP
 
$subtotal = $price - $discount;
$total = $subtotal + $postagerate ;
 
//pick overall postage type
 
if ($postagerate == 5)
	$postage = 3;
else if ($postagerate == 9)
	$postage = 2;
else if ($postagerate == 10)
	$postage = 1;
}

//update shopper table with new/changed info
$sqlshopper = "SELECT * FROM shopper WHERE User_ID = '" . $userID . "'";
 
//echo "query: " . $sqlshopper;
$result = mysql_query($sqlshopper);
$rowCount2 = mysql_num_rows($result);
 
//add shopper
if ($rowCount2 == 0)
	// Nothing 
{
	$sqladd = "INSERT INTO shopper" . $shopperFields . "VALUES ('" . $userID . "', '" . $total . "', '" . $postage . "')";
	$shopadd = mysql_query($sqladd);
 
if (!$shopadd)
	echo "<font class=error><p>Your basket has not been processed</p></font>";
	}
else
	// Nothing
	{
	
	//update details
	$sqlupdate = "UPDATE shopper SET Basket_total = '" . $total . "', Postage = '" . $postage . "' WHERE user_ID = '" . $userID . "'";
	$shopupdate = mysql_query($sqlupdate);
 
if (!$shopupdate)
	echo "<font class=error><p>Your basket has not been updated</p></font>";
}

// finish table
echo "</td></tr>"; 
 
?>
Link to comment
Share on other sites

This is almost completely working!   :)

 

The rows that I want discount on are perfect, they show the price, the discount amount (as a minus) and then the new row total,

 

The only problem is the non discounted items (i.e. not in the switch code)  displays the discount and total from the row above??

 

The end Subtoal and Grand totals are correct though....?

 

Here is my now working code:

 

 

<?php
 
}
 
else
 
//*************display contents of basket//////////////////////////////////////////////////////
 
 
//************display contents of basket//////////////////////////////////////////////////////
 
// echo each header from array
//foreach ($dbFields as $headIndex)
// echo an extra blank header for the delete item column
// fetch each row as an associative array
 
$counter = 1;
$price = 0;
 
//set default postage value outside loop
$postagerate = 15.00;
 
while ($row = mysql_fetch_assoc($result))
{
  //decide which postage value is the highest and use that to calculate overall price
  //get the postage values for each product
  $sqlpostquery = "SELECT * FROM postage WHERE Post_ID = '" . htmlspecialchars($row['Post_ID']) . "'";
 
  //get the postage values from the database
  $postresult = mysql_query($sqlpostquery);
  $rowpost = mysql_fetch_assoc($postresult);
 
  // check if postage value was available
  if ($postresult || !(mysql_num_rows($postresult) == 0)){
     $rawpostage = htmlspecialchars($rowpost['Post_Cost']) ? htmlspecialchars($rowpost['Post_Cost']) : 0.00;
 
   //get the lowest postage rate.
    if ($postagerate > $rawpostage)
      {
        $postagerate = $rawpostage;
    }
  
  else
  {
    $postagerage = 0.00;
  }
 
  //round postage rate of 2 decimal places
  $postagerate = $postagerate;
 
  //release the postage resultset array
  mysql_free_result($postresult);
 
// Pecentage discount code - Needs testing:
 
$price = $price + (calcVAT (htmlspecialchars($row['Price_ExVat']))) * htmlspecialchars($row['quantity']);
$i = $row["Prod_Make"];
 
switch ($i) {
 
case "sony":
 
($percentage =  15/100);
$discount = ($percentage * $price / 1.200);
$totaldiscount =  $price / 1.200 - $discount;
break;
 
case "apple":
($percentage =  15/100);
$discount = ($percentage * $price / 1.200);
$totaldiscount =  $price / 1.200 - $discount;
break;
 
default:
// Nothing
}
 
  echo "<form action=basket.php method=get name=form".$counter.">
 
<input name=update type=hidden value=yes>
<input name=productID type=hidden value=". $row['productID'] ."><tr class=stdtable>
<td align=center> " . htmlspecialchars($row['Prod_Make']) .  "</td>
<td align=center> " . htmlspecialchars($row['Prod_REF']) .  "</td>
<td align=left>" . htmlspecialchars($row['Product_Desc']) . "</td>
<td align=center><input name=updateQuantity onchange=submit(); type=text size=2 value=". htmlspecialchars($row['quantity'])."></td>
<td align=center>". $currency . number_format(htmlspecialchars($row['Price_ExVat']), 2) ."</td>
 
 
 
 
<td align=center> - "  . number_format($discount, 2)  . "</td>
<td align=center>"  . number_format($totaldiscount, 2)  . "</td>
 
";
 
//REMOVE ITEM 
echo "<td align=center><a href=\"basket.php?delete=yes&productID=" . $row['productID'] . "\"><img src=2003/remove.gif border=0></a></td>";
echo "</tr></form>";
 
   $counter ++;
 
  }
 
// END the TRADE DISCOUNT LOOP $subtotal = $price - $discount;
 
$subtotal = $price /1.200 - $discount;
$total = $subtotal * 1.200 ;
 
//pick overall postage type
 
if ($postagerate == 5)
$postage = 3;
else if ($postagerate == 9)
$postage = 2;
else if ($postagerate == 10)
$postage = 1;
}
 
//update shopper table with new/changed info
$sqlshopper = "SELECT * FROM shopper WHERE User_ID = '" . $userID . "'";
 
//echo "query: " . $sqlshopper;
$result = mysql_query($sqlshopper);
$rowCount2 = mysql_num_rows($result);
 
//add shopper
if ($rowCount2 == 0)
// Nothing 
{
$sqladd = "INSERT INTO shopper" . $shopperFields . "VALUES ('" . $userID . "', '" . $total . "', '" . $postage . "')";
$shopadd = mysql_query($sqladd);
 
if (!$shopadd)
echo "<font class=error><p>Your basket has not been processed</p></font>";
}
else
// Nothing
{
 
//update details
$sqlupdate = "UPDATE shopper SET Basket_total = '" . $total . "', Postage = '" . $postage . "' WHERE user_ID = '" . $userID . "'";
$shopupdate = mysql_query($sqlupdate);
 
if (!$shopupdate)
echo "<font class=error><p>Your basket has not been updated</p></font>";
}
 
// finish table
echo "</td></tr>"; 
Edited by roldahayes
Link to comment
Share on other sites

Making me work for this solved tick lol,...

 

Can you print screen, don't understand what you mean:

 "The only problem is the non discounted items (i.e. not in the switch code)  displays the discount and total from the row above?? "

 

 

Would this fix it?

 

Change:

default:
	// Nothing

TO:

default:
$percentage =  "";
$discount =  "";
$totaldiscount =  "";

Don't forget to press solve when we come to a solution... Need my brownie points ;-)

Link to comment
Share on other sites

Ok... So the "Sony" item is fine:   price minus 15%   (£11.24)  equals £63.71

 

But the "Apple" item with a starting price of £109.95  shouldn't attract any discount to the total in the end column should still be £109.95

 

The totals at the bottom are correct though!

 

 

cart2.jpg

Link to comment
Share on other sites

Place in the apple section of the switch:

$percentage =  "";
$discount =  "";
$totaldiscount =  "";

OR

 

You can remove apple so it goes to the default section.

 

Out of curiosity; is sony going to always have a discount? 

Edited by Ansego
Link to comment
Share on other sites

That brings up the error:

 

Warning: number_format() expects parameter 1 to be double, string given in

 

Also, if you look at this screenshot where both lines attract discount:

 

cart3.jpg

 

The 15% of £37.46 which is £5.62 + the £11.24 gathered in the line about equal £16.86  so it appears to be adding the lines discount total to the one above it!?

Edited by roldahayes
Link to comment
Share on other sites

Get this to work first, it is wanting a numeric, should of been a easy fix for you, put this also above the switch before it starts so it is fresh, might fix the other problem;

The other screen shot shows incorrect data too.

 

Paste your full code in again so I can take another look...  

$percentage =  '';
$discount =  '';
$totaldiscount =  '';
Link to comment
Share on other sites

Full Code:

 

 

<?php
error_reporting(E_ALL ^ E_NOTICE);
 
session_start ();
 
 
//echo "userID = ".$_COOKIE['userID'];
 
// ADDED THIS 27/5/10
 
 $_POST = filter_var_array($_POST,FILTER_SANITIZE_STRING);
 $_GET = filter_var_array($_GET,FILTER_SANITIZE_STRING);
 
 
 
 
 
 
//include header code
 
include_once("head.php");
 
// use the user_connection include file's connectDB function
 
include_once("usr_conn.php");
 
if(!connectDB())
 
{
 
echo "<p>Unable To Connect To Database</p>";
 
return;
 
}
 
 
 
  if (isset ($_GET['src']))
 
  {
 
    $_SESSION['returnTo'] = $_GET['src'];
 
  }
 
 
 
  // assign variables
 
//echo "ref1:" . $HTTP_REFERER . "<br>";
 
//$temp = (string)$HTTP_REFERER;
 
//$urlref2 = substr($temp,0,6);
 
//echo "ref:" . $urlref;
 
 
 
$urlref = $HTTP_REFERER;
 
$prodID = htmlentities ($_GET['productID']);
 
$delete = htmlentities ($_GET ['delete']);
 
$quantity = htmlentities ($_GET['quantity']);
 
$updateQuantity = htmlentities ($_GET['updateQuantity']);
 
$update = htmlentities ($_GET ['update']);
 
$currency = "£";
 
// maximum querys per user basket
 
$MAXBASKETQUERY = 25;
 
// start the html table
 
 
 
 
?>
 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>TRADE Basket:</title>
        <meta name="description" content="." />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
 
 
 
 
<!--  start header graphic html -->
 
 
 
    <link href="style.css" rel="stylesheet" type="text/css" />
</head>
 
<table width="770" border="0" align="center" cellpadding="0" cellspacing="0">
 
  <tr>
 
    <td valign="top" background=""><!--  end header graphic html -->
 
      <!--  start Nav html --><!-- end nav html -->
 
      <table width="800" border="0" align="center" cellpadding="0">
 
        <tr>
 
          <td height="170" valign="top">
 
            <div align="center">
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
 
                <tr>
 
                  <td height="71"> <div align="center">
 
                      <table width="100%" border="0" cellspacing="0" cellpadding="7">
 
                        <tr>
 
                          <td width="72%"> </td>
 
                        </tr>
 
                      </table>
 
                      <table width="98%" border="0" align="center" cellpadding="5" cellspacing="0">
 
                        <tr>
 
                          <td width="28%" valign="top"><a href="<?php echo $_SESSION['returnTo']; ?>"><img src="2003/continue2.gif" alt="CLICK HERE TO CONTINUE SHOPPING" width="212" height="39" border="0" /></a></td>
 
                          <td width="72%"> </td>
 
                        </tr>
 
                      </table>
 
                    </div></td>
 
                </tr>
 
                <tr>
 
                  <td align=center>
 
                    <!--begin basket output-->
 
                    <table width="98%" border="1" align="center" cellpadding="2" cellspacing="0" bordercolor="#FFFFFF">
 
                      <!--start tableheaders-->
 
                      <tr class="headertable">
 
                        <td width="111" height="25"> <div align="center">Manufacturer:</div></td>
 
                        <td width="75" height="25"> <div align="center">REF NO.</div></td>
 
                        <td width="292" height="25"> <div align="center">DESCRIPTION</div></td>
 
                        <td width="42" height="25"> <div align="center">QTY</div></td>
                        
                        <td width="71" height="25"> <div align="center">ex vat</div></td>
 
                        <td width="71"> <div align="center">Discount<br />
 
 
 
                          </div></td>
 
                        <td width="57" height="25"> <div align="center">TOTAL<br />
 
                            (ex vat)</div></td>
 
                        <td width="59" height="25" bgcolor="#FFFFFF">
 
                          <div align="center"><font face="Verdana, Arial, Helvetica, sans-serif"></font></div></td>
 
                      </tr>
 
                      <!--end table headers-->
 
                      <tr class="stdtable">
 
                        <td colspan="6" align="center">
 
                          <?php
 
// check if user has no cookie set
 
if ($userID == "")
 
{
 
?>
 
                          <table border="0" align="center" cellpadding="0" cellspacing="0">
 
                            <tr class="stdtable" align="center">
 
                              <td colspan="6">No Product Querys made yet </td>
 
                            </tr>
 
                          </table>
 
                          <p>
 
                            <?php
 
return;
 
}
 
//if the updated quantity is 0 or blank remove item from basket
 
if ($updateQuantity == '0'){
 
$delete = 'yes';
 
}
 
if (($update == 'yes')&& ($updateQuantity == '')){
 
$delete = 'yes';
 
}
 
// if delete parameter set to yes run the delete code
 
if ($delete == 'yes')
 
{
 
// if prodID is set, delete that specific product from this userID's basket
 
if ($prodID != "")
 
{
 
$sqlquery = "DELETE FROM basket WHERE userID = '" . $userID . "' AND  productID = '" . $prodID . "'";
 
$result = mysql_query($sqlquery);
 
if ($result)
 
{
 
?>
 
                          </p>
 
                          <p>  </p>
 
                          <table border="0" align="center" cellpadding="15" cellspacing="0">
 
                            <tr class="stdtable" align="center">
 
                              <td width="564" colspan="6"><h3><font size="5" face="Arial, Helvetica, sans-serif"><strong>Item
                                
                                Deleted. Click <a href="basket.php"><font color="#990000" class="important">Here</font></a>
                                
                                to refresh the basket </strong></font></h3></td>
 
                            </tr>
 
                          </table>
 
                          <?php
 
}
 
else
 
{
 
?>
 
                          <table border="0" align="center" cellpadding="0" cellspacing="0">
 
                            <tr class="stdtable" align="center">
 
                              <td width="368" colspan="6">Unable To Delete <br />
 
                                Item Click <a href="basket.php">Here</a> to refresh
 
                                the basket </td>
 
                            </tr>
 
                          </table>
 
                          <?php
 
}
 
// close the wykes database connection
 
mysql_close();
 
return;
 
}
 
}
 
//update the shopping basket quantity
 
if ($update == 'yes')
 
{
 
// if prodID is set, delete that specific product from this userID's basket
 
if ($prodID != "")
 
{
 
$sqlquery = "UPDATE basket SET quantity = '" . $updateQuantity . "' WHERE productID = '" . $prodID . "'AND userID = '" . $userID . "'";
 
$result = mysql_query($sqlquery);
 
if ($result)
 
{
 
?>
 
                          <table border="0" align="center" cellpadding="0" cellspacing="0">
 
                            <tr class="stdtable" align="center">
 
                              <td colspan="6">Your basket has been updated</td>
 
                            </tr>
 
                          </table>
 
                          <?php
 
}
 
else
 
{
 
?>
 
                          <table border="0" align="center" cellpadding="0" cellspacing="0">
 
                            <tr class="stdtable" align="center">
 
                              <td colspan="6">Your basket has NOT been updated</td>
 
                            </tr>
 
                          </table>
 
                          <?php
 
}
 
 
 
//mysql_close();
 
//return;
 
}
 
}
 
// find the number of rows in this userID's basket
 
$sqlquery = "SELECT * FROM basket WHERE userID = '" . $userID . "'";
 
$result = mysql_query($sqlquery);
 
if (!$result)
 
{
 
echo "<p><font class=error>Could not find any entrys for this Basket</small></p>";
 
mysql_close();
 
return;
 
}
 
else
 
$rowCount = mysql_num_rows($result);
 
// if prodID is set new product is passed in, add/update the userID's basket
 
if ($prodID != "")
 
{
 
// find if the prodID already exists in this userID's basket
 
$sqlquery = "SELECT * FROM basket WHERE userID = '" . $userID . "' AND productID = '" . $prodID . "'";
 
$result = mysql_query($sqlquery);
 
$rowCount = mysql_num_rows($result);
 
// if no matches  insert the product into the userID's basket
 
if ($rowCount == 0)
 
{
 
// find number of items in basket
 
$sqlquery = "SELECT * FROM basket WHERE userID = '" . $userID . "'";
 
$result = mysql_query($sqlquery);
 
$rowCount = mysql_num_rows($result);
 
// check if the maxquery's has been reached
 
if ($rowCount > ($MAXBASKETQUERY - 1))
 
{
 
echo ("<p><font class=error>Only $MAXBASKETQUERY overall orders are allowed, your product could not be added to basket</font></p>");
 
}
 
else
 
{
 
$expiretime = time() + 7200;
 
$sqlquery = "INSERT INTO basket" . $basketFields . "VALUES ('" . $prodID . "', '1', '" .$userID . "', '" . $expiretime . "')";
 
$result = mysql_query($sqlquery);
 
if (!$result)
 
echo "<font class=error><p>Could not add item to Basket</p></font>";
 
}
 
}
 
}
 
// select the userID's basket query and the Product Reference relating to each of the basket's productID's
 
$sqlquery = "SELECT products.Prod_REF, basket.productID, basket.quantity, products.Prod_Make, products.Prod_Model, products.Prod_Type, products.Car_Make, products.Car_Model, products.Price_ExVat, products.Post_ID, Product_Desc FROM basket INNER JOIN products ON basket.productID = products.Prod_ID WHERE ((basket.userID) = '" . $userID . "')";
 
$result = mysql_query($sqlquery);
 
$rowCount = mysql_num_rows($result);
 
// echo $sqlquery;  // *debug
 
// assign the table headers
 
//$dbFields = array( "Reference", "Product Query");
 
// check if no entries in basket
 
if (!$result || (mysql_num_rows($result) == 0)){
 
?>
 
                          <table border="0" align="center" cellpadding="0" cellspacing="0">
 
                            <tr class="stdtable" align="center">
 
                              <td colspan="6" class="error">No Product Queries
 
                                In Basket. </td>
 
                            </tr>
 
                          </table>
 
                          <?php
 
}
 
else
 
//*************display contents of basket//////////////////////////////////////////////////////
 
 
//************display contents of basket//////////////////////////////////////////////////////
 
// echo each header from array
//foreach ($dbFields as $headIndex)
// echo an extra blank header for the delete item column
// fetch each row as an associative array
 
$counter = 1;
$price = 0;
 
//set default postage value outside loop
$postagerate = 15.00;
 
while ($row = mysql_fetch_assoc($result))
{
  //decide which postage value is the highest and use that to calculate overall price
  //get the postage values for each product
  $sqlpostquery = "SELECT * FROM postage WHERE Post_ID = '" . htmlspecialchars($row['Post_ID']) . "'";
 
  //get the postage values from the database
  $postresult = mysql_query($sqlpostquery);
  $rowpost = mysql_fetch_assoc($postresult);
 
  // check if postage value was available
  if ($postresult || !(mysql_num_rows($postresult) == 0)){
     $rawpostage = htmlspecialchars($rowpost['Post_Cost']) ? htmlspecialchars($rowpost['Post_Cost']) : 0.00;
 
   //get the lowest postage rate.
    if ($postagerate > $rawpostage)
      {
        $postagerate = $rawpostage;
    }
  
  else
  {
    $postagerage = 0.00;
  }
 
  //round postage rate of 2 decimal places
  $postagerate = $postagerate;
 
  //release the postage resultset array
  mysql_free_result($postresult);
 
// Pecentage discount code - Needs testing:
 
$price = $price + (calcVAT (htmlspecialchars($row['Price_ExVat']))) * htmlspecialchars($row['quantity']);
$i = $row["Prod_Make"];
 
 
switch ($i) {
 
 
case "APPLE":
 
$percentage =  '0';
$discount =  '0';
$totaldiscount =  '0';
break;
 
 
case "SONY":
 
($percentage =  15/100);
$discount = ($percentage * $price / 1.200);
$totaldiscount =  $price / 1.200 - $discount;
break;
 
 
 
 
 
default:
// Nothing
}
 
  echo "<form action=basket.php method=get name=form".$counter.">
 
<input name=update type=hidden value=yes>
<input name=productID type=hidden value=". $row['productID'] ."><tr class=stdtable>
<td align=center> " . htmlspecialchars($row['Prod_Make']) .  "</td>
<td align=center> " . htmlspecialchars($row['Prod_REF']) .  "</td>
<td align=left>" . htmlspecialchars($row['Product_Desc']) . "</td>
<td align=center><input name=updateQuantity onchange=submit(); type=text size=2 value=". htmlspecialchars($row['quantity'])."></td>
<td align=center>". $currency . number_format(htmlspecialchars($row['Price_ExVat']), 2) ."</td>
 
 
 
 
<td align=center> - "  . number_format($discount, 2)  . "</td>
<td align=center>"  . number_format($totaldiscount, 2)  . "</td>
 
";
 
//REMOVE ITEM 
echo "<td align=center><a href=\"basket.php?delete=yes&productID=" . $row['productID'] . "\"><img src=2003/remove.gif border=0></a></td>";
echo "</tr></form>";
 
   $counter ++;
 
  }
 
// END the TRADE DISCOUNT LOOP $subtotal = $price - $discount;
 
$subtotal = $price /1.200 - $discount;
$total = $subtotal * 1.200 ;
 
//pick overall postage type
 
if ($postagerate == 5)
$postage = 3;
else if ($postagerate == 9)
$postage = 2;
else if ($postagerate == 10)
$postage = 1;
}
 
//update shopper table with new/changed info
$sqlshopper = "SELECT * FROM shopper WHERE User_ID = '" . $userID . "'";
 
//echo "query: " . $sqlshopper;
$result = mysql_query($sqlshopper);
$rowCount2 = mysql_num_rows($result);
 
//add shopper
if ($rowCount2 == 0)
// Nothing 
{
$sqladd = "INSERT INTO shopper" . $shopperFields . "VALUES ('" . $userID . "', '" . $total . "', '" . $postage . "')";
$shopadd = mysql_query($sqladd);
 
if (!$shopadd)
echo "<font class=error><p>Your basket has not been processed</p></font>";
}
else
// Nothing
{
 
//update details
$sqlupdate = "UPDATE shopper SET Basket_total = '" . $total . "', Postage = '" . $postage . "' WHERE user_ID = '" . $userID . "'";
$shopupdate = mysql_query($sqlupdate);
 
if (!$shopupdate)
echo "<font class=error><p>Your basket has not been updated</p></font>";
}
 
// finish table
echo "</td></tr>"; 
                     
 
?>
 
                    </table>
 
                    <table width="100%" border="0" cellspacing="0" cellpadding="1">
 
                      <tr>
 
                        <td width="57%"><div align="right">
 
                            <table width="100%" border="0" cellspacing="0" cellpadding="5">
 
                              <tr>
 
                                <td width="90%"><div align="right">
                                  <input type="image" name="update" src="2003/update2.gif" />
                                </div></td>
 
                                <td width="10%"><div align="right"></div></td>
 
                              </tr>
 
                            </table>
 
                          </div></td>
 
                        <td width="43%"><table width="347" border="1" cellpadding="1" cellspacing="0" bordercolor="#FFFFFF">
                            <tr bordercolor="#FFFFFF">
                              <td height="25" align="right" bgcolor="#EBEBEB" class=small><font color="#000000"><strong>SUB
 
                              TOTAL (ex vat)</strong></font></td>
                              <td height="25" align="center" bgcolor="#EBEBEB" class=header><?php
//Display the Subtotal ex vathere: 
 echo $currency;
 
 echo number_format($subtotal, 2);
 
 ?></td>
                            </tr>
                            <tr bordercolor="#FFFFFF">
 
                              <td width="70%" height="25" align="right" bgcolor="#EBEBEB" class=small> </td>
 
                              <td width="30%" height="25" align="center" bgcolor="#EBEBEB" class=header> </td>
 
                            </tr>
 
                            <tr bordercolor="#FFFFFF">
 
                              <td height="25" align="right" class="small"><font color="#000000">POSTAGE</font></td>
 
                              <td height="25" align="center" class="header">
 
                                <?php
 
 echo $currency;
 
 echo number_format($postagerate, 2);
 
 ?>
 
                              </td>
 
                            </tr>
 
                            <tr bordercolor="#FFFFFF" bgcolor="#EAEADB">
 
                              <td height="25" align="right" class="small"><font color="#000000"><strong>TOTAL ( Inc VAT) </strong></font></td>
 
                              <td height="25" align="center" class="header">
 
                                <?php
 
 echo $currency;
 
 echo number_format($total , 2);
 
 ?>
 
                              </td>
 
                            </tr>
 
                          </table></td>
 
                      </tr>
 
                    </table>
 
                    <!--end basket output-->
 
                  </td>
 
                </tr>
 
              </table>
 
              <hr width="600" size="1" />
 
              <div align="left"></div>
 
              <table border="0" cellspacing="0" cellpadding="15">
 
                <tr>
 
                  <td width="213" align="center" valign="top"><div align="left"><a href="<?php echo $_SESSION['returnTo']; ?>"><img src="2003/continue2.gif" alt="CLICK HERE TO CONTINUE SHOPPING" width="212" height="39" border="0" /></a></div></td>
 
                  <td width="366" align="right" class="asd"><h1 align="right"><span class="important-text2"><a href="checkout.php"><img src="2003/proceed.gif" alt="CLICK HERE TO SUPPLY PAYMENT AND SHIPPING INFORMATION" width="212" height="41" border="0" /></a><br />
                      </span></h1></td>
                  <td width="212" align="left" valign="top"> </td>
 
                </tr>
              </table>
<br />
</div></td>
 
        </tr>
 
      </table></td>
 
  </tr>
 
</table>
 
<!--end basket htm-->
 
<?php
 
//include footer code
 
    //release the postage resultset array
 
mysql_free_result($result);
 
// close the wykes database connection
 
mysql_close();
 
?>
Link to comment
Share on other sites

When you added those three lines do we get the same output?

 

You have the three lines before the default and remove the "APPLE" so it defaults

 

default: 

$percentage =  '0';
$discount =  '0';
$totaldiscount = '0';
Edited by Ansego
Link to comment
Share on other sites


switch ($i) {

case "SONY":

($percentage =  15/100);
$discount = ($percentage * $price / 1.200);
$totaldiscount =  $price / 1.200 - $discount;

break;
default:

$percentage =  '0';
$discount =  '0';
$totaldiscount =  '0';

}

 

Edited by Ansego
Link to comment
Share on other sites

Just a heads up the scope of your code has a downfall with having the code like this is that it is NOT fully dynamic; for example:

 

If you added items to your database ie 'Microsoft', 'Panasonic', 'Nec' etc you would then require updating the discounts manually every time. Or even IF you wanted to change the discount on 'Sony' you would have to do it manually.

Link to comment
Share on other sites

Ok, I have changed the default to:

 

default:
 
($percentage =  0/0);
$discount = ($percentage * $price / 1.200);
$totaldiscount =  $price / 1.200 - $discount;
// Nothing
}
 
Which fixes the non discount line to display "£0" discount and total the line correctly but the next line appears to be deducting 15% from the line above and adding the rows together!
Edited by roldahayes
Link to comment
Share on other sites

Can you show me what's going on in 'number_format()' - Also the chuck of code we are working on from the $price down to the  $counter ++; paste in here please.

 

Seems pointless but test this just the same, cleared the variables before the switch:

$price = $price + (calcVAT (htmlspecialchars($row['Price_ExVat']))) * htmlspecialchars($row['quantity']);
$i = $row["Prod_Make"];


$percentage =  '0';
$discount =  '0';
$totaldiscount =  '0'; 


switch ($i) {


case "SONY":


($percentage =  15/100);
$discount = ($percentage * $price / 1.200);
$totaldiscount =  $price / 1.200 - $discount;
break;
default:


$percentage =  '0';
$discount =  '0';
$totaldiscount =  '0';


}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.