PDA

View Full Version : Automatic Daily Price Reduction Script?



StephenGeorge
04-02-2010, 10:32 AM
I would like to display a price on my website that decreases by a set amount each day. I would assign values to ‘StartDate’, ‘StartPrice’ and ‘ReductionAmount’. For example, if StartDate=(2010,3,1), StartPrice=2000 and ReductionAmount=50.

Then, hopefully, (StartPrice – ((Date – StartDate) x ReductionAmount)) should give the following:

On April 1 2010, website showed “Price today is $2000”

On April 2 2010 website shows “Price today is $1950”

On April 3 2010 website will show “Price today is $1900”

etc ...

As I am an embarrassing novice, could you please help me with the script for this! Thank you very much.

vwphillips
04-02-2010, 01:24 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
<script type="text/javascript">
/*<![CDATA[*/

function Reduction(o){
var srt=o.StartDate.split(/\D/);
var days=Math.floor((new Date()-new Date(srt[0]*1,srt[1]-1,srt[2]*1,0,0,0,0))/1000/60/60/24);
var cal=o.StartPrice-days*o.ReductionAmount;
if (cal>0){
var obj=document.getElementById(o.ID);
obj[obj.nodeName=='INPUT'?'value':'innerHTML']='$'+cal;
}
}

/*]]>*/
</script>

</head>

<body>
<input id="t1" />
<br />
<span id="t2" ></span>

<script type="text/javascript">
/*<![CDATA[*/

Reduction({
ID:'t1',
StartDate:'2010/4/1',
StartPrice:2000,
ReductionAmount:50
});

Reduction({
ID:'t2',
StartDate:'2010-3-1',
StartPrice:1000,
ReductionAmount:10
});

/*]]>*/
</script>
</body>

</html>

djr33
04-02-2010, 08:30 PM
Be aware that Javascript is not secure.

It's best to use PHP or another serverside language (processed on the server, not on the user's computer like Javascript) when you are dealing with any "secure" information.

There are a few reasons for this:
1. In this case it will display it to the user. It will NOT save it for your records or display the same info on the next page (like in the shopping cart) unless you have that same script run on every page-- but it still won't have that value once they end up paying for it, unless you somehow transfer it to the server such as through a form.
2. Javascript can be altered by the client. They can, if they know Javascript, change the price. If there is any way that means they would be able to pay less, then you would not want that. (For example, if as I said in (1), they could submit it through a form to the server, and be paying whatever they enter rather than the default.)
3. If for nothing else, users can then view this script and figure out your pricing plans: they will wait a week and you'll look 7*$50, or something like that.

StephenGeorge
04-04-2010, 05:58 PM
Thank you vwphillips and djr33 for your replies.

Unfortunately, I don’t think I have managed to describe what I am looking for particularly well, for which I apologise!


I am trying to create an automatic reverse auction!
There will only be one instance of ‘bidding’.
The price will simply be displayed on the website as an ‘offer to buy’.
The price will start high and go down a set amount each day.
The start price, start date and reduction amount will all be decided by me and fixed into the Javascript coding.
The reduction will occur at the same time each day, when the Date officially changes, and will stay at the new lower price for the rest of the day.
Another reduction will occur the next day etc.
When someone decides they want to buy the item on offer at the price displayed on a particular day (rather than wait for the price to reduce further at the risk of losing the item to someone else with a deeper pocket!), they will email their intention to me.
They will not be able to buy it directly from the website, nor will they be able to change the price on the website themselves as there would be no form or facility available to do this.
Javascript would be just the mechanism to display the reducing price each day.
The price each day will be calculated by the fixed ‘variables’ set in the Javascript code (ie the start date, start price and reduction amount).
The only truly variable ‘variable’ will be the Date, and even that will change automatically each day!


I hope this explains my thoughts slightly better than my first attempt!

Thank you for your help.

djr33
04-04-2010, 08:53 PM
Then I understand, but there is still one applicable issue from above:
3. If for nothing else, users can then view this script and figure out your pricing plans: they will wait a week and you'll look 7*$50, or something like that.

And also the fact that visitors without Javascript turned on will see nothing.

I would recommend using a serverside language like PHP if your server allows it and that is very easy to setup.
It's easy in Javascript too, but then actually getting the text to show up somewhere on the page is slightly more complex, but possible.

jscheuer1
04-04-2010, 10:09 PM
Other considerations here are that without a conversion to UT, the time/price in javascript will vary at times depending upon the user's timezone. However, if their system clock is inaccurate, even with a conversion to UT, the price they see will be off, perhaps wildly so. You would be advertising a price to some viewers that you have no intention of meeting. Depending upon circumstances, this could lead to lawsuits.

If for no other reason, this last alone dictates that this should be done on the server side.

djr33
04-04-2010, 10:30 PM
It's possible to seed the time valid from a serverside value (or perhaps is there a way to get this using Javascript from a timestamp on the file from the server or something?), but that seems to defeat the point anyway.

If it is possible to do this in PHP, you can also create a database so that you don't have to setup everything manually, if you'd like.

StephenGeorge
04-05-2010, 08:07 AM
Thank you jscheuer1 and djr33 again! Your points about whether the user has Javascript enabled, what their system clock time is and also their time zone are critical.

It is now obvious to me that I need to have complete control over what price is displayed on the user's screen and this can only be done server-side. Javascript is not the right way to go about this. PHP definitely seems to be the right course of action, especially factoring in the use of a database, as suggested.

I think I will repost in the PHP forum!

Thank you all for your help.