Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $(function () {
- xmlhttp = new XMLHttpRequest()
- xmlhttp.open("GET", "mysql.php?p=getfullstats", true);
- xmlhttp.send();
- xmlhttp.onreadystatechange = function () {
- if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
- var jsontext = xmlhttp.responseText;
- var json = JSON.parse(jsontext);
- if (jsontext == '[]') {
- return false
- } else {
- var newTr = '';
- for (var i = 0; i < json.length; i++) {
- newTr += (' / ' + json[i].date + ' ' + json[i].price);
- // parse a date in dd-mm-yyyy
- function parseDate(input) {
- var parts = input.match(/(\d+)/g);
- // new Date(year, month [, date [, hours[, minutes[, seconds[, ms]]]]])
- return new Date(parts[2], parts[1] - 1, parts[0]); // months are 0-based
- }
- // get sum of prices for given month
- function getSumForMonth(data, month) {
- var sum = 0;
- month -= 1; // months are 0-based
- $.each(data, function (idx, item) {
- if (parseDate(item.date).getMonth() == month) {
- sum += parseFloat(item.price);
- }
- });
- return sum;
- }
- var sum = getSumForMonth(json, 9); // sum for september
- console.log(sum);
- }
- }
- }
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement