samoht Posted September 24, 2007 Share Posted September 24, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/70527-recalculate-function/ Share on other sites More sharing options...
EvanAgee Posted September 25, 2007 Share Posted September 25, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/70527-recalculate-function/#findComment-354570 Share on other sites More sharing options...
samoht Posted September 25, 2007 Author Share Posted September 25, 2007 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 ??? Please help thanks Quote Link to comment https://forums.phpfreaks.com/topic/70527-recalculate-function/#findComment-354571 Share on other sites More sharing options...
EvanAgee Posted September 25, 2007 Share Posted September 25, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/70527-recalculate-function/#findComment-354575 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.