Page 1 of 1

Help Needed :( loops

Posted: Thu Sep 12, 2002 5:22 pm
by JPlush76
I have a shopping cart class and when the user puts a product in their cart I have to do a check on that product to see if it has a price break on it
IE: product price each = $5.99 buy 5 and you get them for $3.99 each

I'm getting it to work half ass, basically I have a table that stores the buy# and the new price along with a product id to xref the products table but I'm always getting the last row of data's price right now

Code: Select all

<?php
function PriceBreakCheck($product,$qty)
	{
                                //GET MY RESULT SET TO SEE IF THERE IS A PRODUCT BREAK
		$result = query_db("SELECT products_price, products_pbreak FROM products WHERE products_id = $product");
		$row = @mysql_fetch_array($result);
	
                                // IF THERE IS NO PRODUCT BREAK JUST USE THE NORMAL PRODUCT PRICE
		if(($rowї'products_pbreak']) == 0){
	
			$this->unit_price = ($rowї'products_price']);
			
			return $this->unit_price;
		
                               //START THE SECTION TO LOOP THROUGH THE BREAKS
		} else { // 1st else
	
	$result2 = query_db("SELECT p.products_id, products_price, products_pbreak,break_buy,break_fixed "
	."FROM products as p,products_pbreak as pb WHERE p.products_id = $product ORDER BY break_buy asc");
	
	$numrows = mysql_num_rows($result);
	
GOTTA DO SOMETHING HERE TO LOOP THROUGH THE TABLE AND GET THE RIGHT DATA

}
}

?>
my table result looks like this:


Code: Select all

products_id  products_price  products_pbreak  break_buy   break_fixed  
4                  16.99       1              6                  14.99 
4                  16.99       1              100               12.99 
4                  16.99       1              200               10.99
basically if someone buys 150 of these things the price should be 12.99 each

if they buy 204 they should be 10.99 each

if they buy 3 then its under the minimum of 6 for the price break so the price should be the standard product price

I've been stuck on the logic for a few hours now :(

Posted: Thu Sep 12, 2002 6:09 pm
by JPlush76
ugh I got it working

Code: Select all

<?php
function PriceBreakCheck($product,$qty)
	{
		$result = query_db("SELECT products_price, products_pbreak FROM products WHERE products_id = $product");
		$row = @mysql_fetch_array($result);
	
		if(($rowї'products_pbreak']) == 0){
	
			$this->unit_price = ($rowї'products_price']);
			
			return $this->unit_price;
			
		} else { // 1st else
	

	$result2 = query_db("SELECT p.products_id, products_price, products_pbreak,break_buy,break_fixed "
	."FROM products as p,products_pbreak as pb WHERE p.products_id = $product ORDER BY break_buy asc");
	
	$row = mysql_fetch_array($result2);
	
	if($qty < ($rowї'break_buy']))
	{
		$this->unit_price = ($rowї'products_price']);
	} else {
	
		do 
		{
			$this->unit_price = ($rowї'break_fixed']);
		} while($row = mysql_fetch_array($result2) AND $qty >= ($rowї'break_buy']));
	}// end else
		
		return $this->unit_price;
			
	} // end 1st else
		
}//end function
?>