Jump to content
  • Checkout
  • Login
  • Get in touch


The e-commerce.

display value based on a variable retrieved from the database


Recommended Posts

I would like to display a value based on the following formula: newprice = oldprice - (discount * products purchased). The old price would be manually set, as well as the discount, but products purchased would be the variable that needs to be retrieved from the database for the given item.


I'd like this to automatically update and display the newprice in place of the oldprice after each time the item is purchased, which would include, of course, multiple buyers purchasing one or more amounts of the given item.


I have just a little bit of knowledge regarding both PHP and Javascript, and it's certainly not been enough.

Link to comment
Share on other sites

Ok--got some views, but no help here.


Does that mean a solution doesn't exist?


That might explain why I can't find anything anywhere, here or on the net.


Surely, there must be a way of getting a value from the database via php to show up in an equation, be it javascript or something else...


I've been close a couple of times since I posted, but not in the way I would like.


So, any ideas? Even, a sorry, can't do that, would be helpful.

Link to comment
Share on other sites

To add that feature, you either need to alter some database tables as well as do a bit of programming in PHP.


The total number of the item sold can be found in the table products, column products_ordered. This column is updated each time a product is sold.


To avoid a lot of programming work, you should do the calculation of the new price in the checkout_success.php and store it to the database (table products).


If I got it right, you should add two columns to the table products: discount, old_price and discount_valid


Discount is the amount of discount you want to give, old_price the former calculated price with the discount calculation, discount_valid is a flag which determines if the discount has to be calculated for this product. That's in case you have items where the price isn't calculated new each time a product is sold.


Within the checkout_success.php you retrieve a first the data from the table products to determine if it has to be calculated.


If so, you can make the calculations and then store the data back to the table products.


If you need further informations, let me know.

Link to comment
Share on other sites

Thanks for responding, and offering a solution. I haven't had a chance to do anything with it, but I appreciate all the help I can get. I'm probably going to need more help, from the looks of things, since it involves creating tables and interfacing with the dabase (things I've not done on my own). I will try to do as suggested and if when I am ready for more help I will ask. This is certainly a chore.


One thing I should say now, though is--this auto discount will always apply, until we run out of the item we're selling, so I don't really need to worry whether or not the discount applies--and where I want it to show up most is on the homepage, where everyone who comes to take a look can see to what the price has fallen, too.


Thanks again.

Link to comment
Share on other sites


This topic is now archived and is closed to further replies.

  • Create New...