Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Live example: <a href="http://jsfiddle.net/tSsvb/1/" rel="nofollow">http://jsfiddle.net/tSsvb/1/</a></p> <p>I started with 2 variables which represent</p> <ul> <li>an array of the various seasons</li> <li>an object which represents the pricing matrix </li> </ul> <hr> <pre><code>var seasonLookup = [ {startDay: 1, startMonth:1, endDay: 10, endMonth: 6, season:1}, {startDay: 21, startMonth:9, endDay: 31, endMonth: 12, season:1}, {startDay: 11, startMonth:6, endDay: 30, endMonth: 6, season:2}, {startDay: 1, startMonth:9, endDay: 20, endMonth: 9, season:2}, {startDay: 1, startMonth:7, endDay: 31, endMonth: 8, season:3}, ]; var priceMatrix = { bike3: { 1: { t1: 2, t2: 2, t3: 1}, 2: { t1: 5, t2: 3, t3: 2}, 3: { t1: 10, t2: 3, t3: 5} } }; </code></pre> <hr> <p>The first is pretty straightforward. The second I have only moddled up bike3 as it's the one youve used in your example. <code>1</code>,<code>2</code> &amp; <code>3</code> represent the season, <code>t1</code> - <code>t3</code> represent the tier's of payment with <code>t1</code> hardcoded as 1-2 days, <code>t2</code> as 3-7 and <code>t3</code> as 8+.</p> <p>Then I have created 2 functions. One gets the season for a specified date:</p> <pre><code>function getSeason(date){ var day = date.getDate(); var month = date.getMonth()+1; var year = date.getFullYear(); for(var i=0;i&lt;seasonLookup.length;i++){ var s = seasonLookup[i]; var startDate = new Date(year, s.startMonth-1,s.startDay); var endDate = new Date(year, s.endMonth-1,s.endDay); if(date &gt;= startDate &amp;&amp; date &lt;= endDate) return s.season; } return null; } </code></pre> <p>The other gets the total price for a specified bike in a specified season for a specified number of days:</p> <pre><code>function getPrice(bike, season, days){ var tier = ""; if(days &lt;=2) tier = "t1"; else if(days &lt;=7) tier = "t2"; else tier = "t3" return priceMatrix[bike][season][tier] * days; } </code></pre> <hr> <p>Next step is a method to do the actual calculation based on a start date, end date and bike:</p> <pre><code>function calculatePrice(startDate, endDate, bike) { var currentSeason = getSeason(startDate); var totalPrice = 0; var daysInSeason = 0; var currentDate = startDate; while(currentDate&lt;=endDate){ var season = getSeason(currentDate); if(season != currentSeason){ totalPrice += getPrice(bike,currentSeason,daysInSeason); currentSeason = season; daysInSeason = 0; } daysInSeason++; currentDate.setDate(currentDate.getDate()+1); } totalPrice += getPrice(bike,currentSeason,daysInSeason); return totalPrice; } </code></pre> <p>And the final part is to hook it up so that it recalculates on any change of the dropdowns. jQuery is your friend here. I hadded a class <code>recalc</code> to all elements that should cause the recalculation, id's to everything to make them easier to reference, and hooked into the <code>change</code> event to build the parameters and call the method:</p> <pre><code>$('.recalc').change(function(){ var startDate = new Date(parseInt($('#yd').val(),10),parseInt($('#md').val(),10)-1,parseInt($('#dd').val(),10) ); var endDate = new Date(parseInt($('#yr').val(),10),parseInt($('#mr').val(),10)-1,parseInt($('#dr').val(),10)); var bike = $('#bike').val(); var price = calculatePrice(startDate,endDate,bike); $('#price').val(price); }); </code></pre> <p>Hope that helps, you may need to generate your pricing matricies from PHP, but thats an excercise for you :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. COHello, Okay, thanks I will check it and try to add something from my side too. So basically, what I checked is that you have passed the prices in array, but I have them passed via PHP, so any suggestions about that (if I will have more than 200 bikes), I would like them automatically get added dinamically, not to write each tiem in file. About first function, as far as I understood, it takes current date, but what I ment, is that it must take the date from form field "From". Okay that is nothing serious, it can be changed easily. Will try to figure out something too. Again thanks!
      singulars
    2. COOkay so I understood your idea about the first function. But now the question is if I pass the prices via PHP, how can I easily them add to javascript array PriceMatrix? Do you have any suggestions?
      singulars
    3. COHmm, no you understood it a little bit wrong :)! If the total days are less than 7 and greater than 2, then it will take the price, but if total days will be more than 8, then it will take one price (8+ price) for all days. So basically, if we will have to tal days 1 or 2, it will take price as 1-2 day price, if it will be > 2 and <= 7 then it will take price as 3 - 7 days. And if it total days will be more than 8 (in this case 87), it will take price for all calculation from 8+ days. In additional, when you have time, please check my reply to your answer ;)! Thank you for helping me :)!
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload