Jump to content

recalculate function


samoht

Recommended Posts

Hello,

 

I am having some difficulty understanding how to write a function that will recalculate the prices for an product based on the user input of a $cost variable -

and taking that value and doing some math on it based on a price code table that has factors for the retail and the sale price.

 

currently I have this as a function to populate my page with the prices already in my db,

 

<?php
function get_pprice($pid)
{
  $ProductId = $pid;
  $sql = dbQuery("	
SELECT PriceRetail, PriceSell, PriceHold, Cost, ClientPriceCode, ProductPriceId
FROM productprice
WHERE ProductId ='$ProductId'
");

	while($row = dbFetchAssoc($sql))
  	{
	  $rows[] = $row;
  	}

  	return $rows;

} 

 

and this is a snippet of how I am poplulating my table;

 

<?php
$pprice = get_pprice($ProductId,$pc, $Cost);
for($n = 0; $n < $nClients; $n++)
{
		$ppc = $pprice[$n]['ClientPriceCode'];
		$q = dbQuery("SELECT ShortName FROM clientpricecode WHERE ClientPriceCode = '$ppc'");
		$row = dbFetchAssoc($q);
		extract($row);
									  
	echo "\n\t<tr class=\"d".($n & 1)."\">\n\t";
	echo "<td><input name=\"ClientPriceCode[$n]\" type=\"hidden\" id=\"ClientPriceCode\" value=\"".$pprice[$n]['ClientPriceCode']."\">$ShortName</td>";
	echo "<td><input size=\"7\" name=\"PriceSell[$n]\" type=\"text\" id=\"PriceSell\" style=\"text-align:right\" value=\"".$pprice[$n]['PriceSell']. "\"></td>";
	echo "<td><input size=\"7\" name=\"PriceHold[$n]\" type=\"text\" id=\"PriceHold\" style=\"text-align:right\" value=\"".$pprice[$n]['PriceHold']. "\"></td>";
	echo "</tr>";
}

 

Any ideas on how to write get the recalculate function started?

And once that works - how will I update the fields on the HTML table?

Link to comment
Share on other sites

If you're wanting to do "live" recalculating (meaning it happens in real time) you have a couple of options.

 

One option would be to use Javascript and create a function that recalculates everything in the table, replacing the contents of each cell with the new values. I would recommend something like prototype.js to make the process a little simpler, or you can simply table cell ID attributes and update the innerHTML property of each of those cell IDs using the results of your recalculation.

 

The other option would be to use Ajax to reprocess all of the totals and update the table contents. This would be much more involved.

 

Keep in mind that, aside from ajax, there's no way to update the data in real time without reloading the page using PHP. You could change the total, hit submit and then display an updated table, that's easy.

Link to comment
Share on other sites

Thanks for the input.

 

I think I may go the rout of AJAX - but was not sure how to update the innerHTML once I create a function that queries and recalculates my prices.

 

I'll try to get an AJAX setup started and post my stopping points.

 

for now though

You could change the total, hit submit and then display an updated table, that's easy.

 

I wouldn't mind doing this - but its not seeming so easy for me  ??? ;D

Please help

 

thanks

Link to comment
Share on other sites

The Ajax is going to be a little more difficult to do since it requires the interaction of both JS and the PHP. I would suggest loading all of the data to the page via the PHP and then use simple javascript to manipulate that data on the page. You could then, after the data had all been recalculated, re-submit that form to a function that updates the data in the DB if need be.

 

I'd be happy to assist with the physical setup, but I cannot volunteer the time, sadly it's not quite that simple. :)

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.