Enter function to Database

Hi all, I have created a function to calculate the VAT, I don’t want to echo the the VAT Amount but rather enter it to the database. Unsure of how I get the function to enter into the database with the calculated amount. I’m sure it’s something very simple.

function vatProcess($vat){
//Divide the entered amount by current VAT Rate
$getNet = $vat/1.20;
	//Subtract VAT Amount from amount entered
	$total = $vat-$getNet;
		//Give the amount of VAT...
		$grand = number_format($total,2);
}

So I basically would like to know how I can connect to $grand and enter that variable into the database.

Hey guys, ok a bit of progress, things are now working…sort of. I have a foreach loop for entering various products into the database. I’m trying to unset my variable - which it does but on the next loop it doesn’t re-register the variable $condi in the if statement. Any help really appreciated! :slight_smile:

foreach($_POST['products'] as $id=>$product){
$blip = preg_replace('/[^0-9\\.]/', '', $product['price']); 
	$total = $blip*$product['quantity'];
		//If product is not used, go ahead and calculate VAT!
		if($product['cond']=="New"){
	        $condi = vatProcess($product['price']);
		}
					
		//Push Data into Database					
		$sql = "INSERT INTO order_products (receipt_id, customer_id, product_name, product_price, product_quant, product_serial, product_code, product_cond, vat, sub_total) VALUES ('$_POST[receiptno]','$_GET[id]','$product[name]','$blip','$product[quantity]','$product[serial]','$product[stock]', '$product[cond]', '$condi', '$total')";
	mysql_query($sql);
			
			//Unset $condi variable ready for next loop!
			 unset($condi);
			 
			 }

I can’t se anything wrong ><. Are you sure that that the if statement evaluates to true after the next loop?

Sorry, I’ve not slept for 31 hours, It looks messy, but it should prove useful in helping you find a better solution. :slight_smile:


define('VAT_RATE', 1.20);

function get_nett_from_gross($gross, $vat){
  return $gross / $vat;
}

$products = array(
  array(
    'name'  => 'foo',
    'cond'  => 'new',
    'price' => 10.99
  ),
  array(
    'name'  => 'foo',
    'cond'  => 'old',
    'price' => 5.50
  ),
  array(
    'name'  => 'ying',
    'cond'  => 'new',
    'price' => 1.99
  ),
  array(
    'name'  => 'yang',
    'cond'  => 'old',
    'price' => .50
  )
);

$sql = 'INSERT INTO table (id, condition, price, vat) VALUES ';

foreach($products as $id => $product){

  $isNewProduct = 'new' === $product['cond'];

  $product['vat'] = 0;

  if($isNewProduct){
    $product['price'] = get_nett_from_gross($product['price'], VAT_RATE);
    $product['vat']   = VAT_RATE;
  }

  $sql .= sprintf(
    "\
(%d, '%s', %f, %f),",
    $id,
    $product['cond'],
    $product['price'],
    $product['vat']
  );
}

echo rtrim($sql, ',') . ';' ;

/*
  INSERT INTO table (id, condition, price, vat) VALUES
  (0, 'new', 9.158333, 1.200000),
  (1, 'old', 5.500000, 0.000000),
  (2, 'new', 1.658333, 1.200000),
  (3, 'old', 0.500000, 0.000000);
*/

Hey chaps, cheers for your help, managed to sort it…not sure what I did now (forgotten). Dude you need some sleep, all I can say is that you must be drinking a lot of coffee…! :D:cool::lol: