Jump to content

jarvis

Members
  • Content count

    440
  • Joined

  • Last visited

Community Reputation

1 Neutral

About jarvis

  • Rank
    Advanced Member

Profile Information

  • Gender
    Not Telling
  1. jarvis

    Prevent Blanks in Array

    Thanks again!
  2. jarvis

    Prevent Blanks in Array

    Hi, How do you prevent the following happening: Array ( [] => Array So, effectively I want to remove that one from the main array. i.e. Array ( [] => Array ( [0] => Japanese GP [1] => Japanese GP [2] => Williams ) [2005] => Array ( [0] => Japanese GP [1] => Williams ) [2006] => Array ( [0] => Japanese GP ) ) Becomes: Array ( [2005] => Array ( [0] => Japanese GP [1] => Williams ) [2006] => Array ( [0] => Japanese GP ) ) I tried using: if (!empty($year['value'])): Within my foreach but sadly that didn't work Thanks
  3. jarvis

    Group and Total

    @Barand Once again, many thanks for your time, assistance and patience. It has been very much appreciated!!
  4. jarvis

    Group and Total

    Hi @Barand, I realised I had sort($group); which was screwing the results, removing this now shows exactly what I needed: Year: 2011 Total: 20 Year: 2012 Total: 35 Year: 2013 Total: 35 Year: 2014 Total: 45 Year: 2015 Total: 19 Year: 2016 Total: 19 Year: 2017 Total: 10 Year: 2018 Total: 12 Thank you once again for all your help
  5. jarvis

    Group and Total

    Ok thanks @Barand Just to say, using your code: foreach ($group as $year => $total) { echo "Year: $year Total: $total<br>"; } Outputs the same as my code: Year: 0 Total: 10 Year: 1 Total: 12 Year: 2 Total: 19 Year: 3 Total: 19 Year: 4 Total: 20 Year: 5 Total: 35 Year: 6 Total: 35 Year: 7 Total: 45 So not sure what you mean? All I'm trying to do is about the above but replacing the Year: 0 with Year 2011 (for example)
  6. jarvis

    Group and Total

    Hi @Barand, I had: foreach ($group as $k => $v) { echo "\$group[$k] => $v.\n"; } Which looks similar, but I can't get the year to show. I simply get: Year: 0 Total: 10 Year: 1 Total: 12 Year: 2 Total: 19 Year: 3 Total: 19 Year: 4 Total: 20 Year: 5 Total: 35 Year: 6 Total: 35 Year: 7 Total: 45 The key doesn't seem to contain the year, or once again, am I missing the obvious? I truly appreciate your help thought!
  7. jarvis

    Group and Total

    Thanks again @Barand I meant how do I loop through like so: if ($group): foreach ( $group as $g) : echo '<p>Year: '.$g['year'].' Value: '.$g['value'].'</p>'; endforeach; endif; Basically, I'm trying to build up the values so I can loop it out as: Year: 2018 Value: 2 Year: 2011 Value: 20 Year: 2012 Value: 35 Year: 2013 Value: 35 Year: 2014 Value: 10 Year: 2015 Value: 15 Year: 2016 Value: 15 Year: 2017 Value: 10 Thanks again
  8. jarvis

    Group and Total

    Hi @Barand Oh! But it produces the result I need: Array ( [2018] => 12 [2011] => 20 [2012] => 35 [2013] => 35 [2014] => 45 [2015] => 19 [2016] => 19 [2017] => 10 ) All I need is to work out how to access each one? Or have I totally missed the point? In which case, please accept my apologies!
  9. jarvis

    Group and Total

    Hi @Barand Thank you so much! Just needed to move the print_r outside the loop and it's exactly what I needed. Thanks again!
  10. jarvis

    Group and Total

    Hi All, Hoping someone can point out the blinkin obvious! I'm trying to group a bunch of data and total it. For example, the data looks like this: Year: 2018 Value: 10 Year: 2011 Value: 20 Year: 2012 Value: 35 Year: 2013 Value: 35 Year: 2014 Value: 35 Year: 2018 Value: 2 Year: 2015 Value: 4 Year: 2016 Value: 4 Year: 2014 Value: 10 Year: 2015 Value: 15 Year: 2016 Value: 15 Year: 2017 Value: 10 I then use an array to group the data by year, like so: if ($test): $group = array(); foreach ( $test as $t) : echo '<p>Year: '.$t['year'].' Value: '.$t['value'].'</p>'; $group[$t['year']][] = $t['value']; endforeach; endif; This gets me one step further by producing: Array ( [2018] => Array ( [0] => 10 [1] => 2 ) [2011] => Array ( [0] => 20 ) [2012] => Array ( [0] => 35 ) [2013] => Array ( [0] => 35 ) [2014] => Array ( [0] => 35 [1] => 10 ) [2015] => Array ( [0] => 4 [1] => 15 ) [2016] => Array ( [0] => 4 [1] => 15 ) [2017] => Array ( [0] => 10 ) ) What I really need, is to show something like: Array ( [2018] => Array ( [0] => 12 ) [2011] => Array ( [0] => 20 ) [2012] => Array ( [0] => 35 ) [2013] => Array ( [0] => 35 ) [2014] => Array ( [0] => 45 ) [2015] => Array ( [0] => 19 ) [2016] => Array ( [0] => 19 ) [2017] => Array ( [0] => 10 ) ) What would be the best way to achieve this please?
  11. jarvis

    randomise ajax for loop

    Once again, thanks cyberRobot, that worked a charm! Here's the final code in case someone else needs a hand: $.ajax({ url: '//api.instagram.com/v1/users/self/media/recent', dataType: 'jsonp', type: 'GET', data: {access_token: token, count: num_photos}, success: function(data){ data.data.sort(function() { return 0.5 - Math.random() }); for( x in data.data ){ $('#insta').append('<img src="'+data.data[x].images.low_resolution.url+'"><br/>'); } }, error: function(data){ console.log(data); } });
  12. jarvis

    randomise ajax for loop

    Hi cyberRobot, Thats correct. Glad you confirmed there are no built in functions. I'll look at that link, see if I can get that working. Many thanks
  13. jarvis

    randomise ajax for loop

    Hi All, I have the below script: <div id="insta"></div> <script src='//code.jquery.com/jquery-2.2.4.min.js'></script> <script> var token = 'xxx', num_photos = 10; $.ajax({ url: '//api.instagram.com/v1/users/self/media/recent', dataType: 'jsonp', type: 'GET', data: {access_token: token, count: num_photos}, success: function(data){ for( x in data.data ){ $('#insta').append('<img src="'+data.data[x].images.low_resolution.url+'"><br/>'); } }, error: function(data){ console.log(data); } }); </script> It retrieves a set number of images from instagram and works a treat! What I'd like to do, is randomise the results but I can't see how to achieve this. I know it would mean using math.round but can't see how to wrap that in? Any help is very much appreciated
  14. jarvis

    JavaScript not working in IE11

    <form name="loandata"> <table class="loancalc"> <tr> <td class="smaller-width">Amount of the loan:</td> <td> <input type="range" min="2500" max="25000" step="500" data-rangeslider onChange="calculate();"> <output class="loan-amount" name="principal" id="principal" onChange="calculate();"></output> </td> </tr> <!-- <tr> <td>Annual percentage rate of interest:</td> <td><input type="text" name="interest" onChange="calculate();" value="6"></td> </tr> --> <tr> <td class="smaller-width">Repayment period in years:</td> <td> <input type="range" min="1" max="5" data-rangeslider onChange="calculate();"> <output class="loan-years" name="years" id="years" onChange="calculate();"></output> </td> </tr> <tr> <td>Your monthly payment:</td> <td class="calc-result">£<span class="result" id="payment"></span></td> </tr> <tr> <td>Your total payment:</td> <td class="calc-result">£<span class="result" id="total"></span></td> </tr> <tr> <td>Your total interest payments:</td> <td class="calc-result">£<span class="result" id="totalinterest"></span></td> </tr> </table> </form> <script language="JavaScript"> function calculate() { // Get the user's input from the form. Assume it is all valid. // Convert interest from a percentage to a decimal, and convert from // an annual rate to a monthly rate. Convert payment period in years // to the number of monthly payments. //var principal = document.loandata.principal.value; var principal = document.getElementById("principal").value; var interest = 6 / 100 / 12; //var payments = document.loandata.years.value * 12; var payments = document.getElementById("years").value * 12; // Now compute the monthly payment figure, using esoteric math. var x = Math.pow(1 + interest, payments); var monthly = (principal*x*interest)/(x-1); // Get named <span> elements from the form. var payment = document.getElementById("payment"); var total = document.getElementById("total"); var totalinterest = document.getElementById("totalinterest"); // Check that the result is a finite number. If so, display the // results by setting the HTML content of each <span> element. if (isFinite(monthly)) { payment.innerHTML = monthly.toFixed(2); total.innerHTML = (monthly * payments).toFixed(2); totalinterest.innerHTML = ((monthly*payments)-principal).toFixed(2); } // Otherwise, the user's input was probably invalid, so display nothing. else { payment.innerHTML = ""; total.innerHTML = "" totalinterest.innerHTML = ""; } } </script> <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script> <script> $(function() { var $document = $(document); var selector = '[data-rangeslider]'; var $element = $(selector); // For ie8 support var textContent = ('textContent' in document) ? 'textContent' : 'innerText'; // Example functionality to demonstrate a value feedback function valueOutput(element) { var value = element.value; var output = element.parentNode.getElementsByTagName('output')[0] || element.parentNode.parentNode.getElementsByTagName('output')[0]; output[textContent] = value; } $document.on('input', 'input[type="range"], ' + selector, function(e) { valueOutput(e.target); }); // Basic rangeslider initialization $element.rangeslider({ // Deactivate the feature detection polyfill: false, // Callback function onInit: function() { valueOutput(this.$element[0]); }, // Callback function onSlide: function(position, value) { console.log('onSlide'); console.log('position: ' + position, 'value: ' + value); }, // Callback function onSlideEnd: function(position, value) { console.log('onSlideEnd'); console.log('position: ' + position, 'value: ' + value); } }); }); $(document).ready(function() { calculate(); }); </script>
  15. Hi All, I've the following script which serves as a loan calculator. I've changed some of the inputs to use range sliders. It works fine in Chrome, FF and Edge, however, it simply won't play ball in IE11. The few errors it was throwing up I've since addressed yet it still won't work I'm perplexed (and JavaScript is not my language of choice) Any help would be much appreciated Thanks in advanced
×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.