javascript - Php Counter Hides Comma after loading and not showing permanently

685

I am trying to figure out a php counter. I have added Number format in a php function to print counter digits separated with a comma after first 3 digits. But Comma doesn't show up permanently after page load, When I reload the page, Comma shows for a moment but immediately hides. Below is the code i have used to get this result and please review the counter page.

<?php
$bg = get_field('counter_bg');

$init_value = get_field('init_value');
$init_date = get_field('init_date');
$seconds = strtotime("now") - strtotime($init_date);
$countup_value = get_field('countup_value');
$number = round((($seconds * $countup_value) + $init_value) * 100) / 100;

if($number) :
 $title = get_field('counter_title');
 $text = get_field('counter_text');
 ?>
 <section id="home-counter" <?php if($bg['url']) echo "style='background-image: url({$bg['sizes']['slide-thumb']})'"; ?>>
  <div >
   <?php
   if($title) echo "<h3 class='counter-title'>{$title}</h3>";
   echo "<div id='counter-number'>";
   echo Number_format ($number);
   echo "</div>";
   if($text) echo "<div class='counter-text'>{$text}</div>";
   ?>

  </div><!--containr-->
 </section><!--home-section-->

 <script>
  (function($) {

   $(document).ready(function(){

    var counter = $('#counter-number');
    var coutUp = Number(<?= $countup_value ?>);

    setInterval(function() {
     counter.text(calculate_value);
    }, 1000)

    function calculate_value() {
     var initDate = moment('<?= $init_date ?>').format('x');
     var nowDate = moment().format('x');
     var dif = Number((nowDate - initDate) / 1000);
     var value = Number(dif * coutUp);
     // console.log(initDate, nowDate, dif, value, '<?= $init_date ?>');
     return value.toFixed(2)

    }

   });

  })(jQuery);
 </script>

<?php endif; ?>

Please check the current Comman display issue at: http://airlite.designase.com/it/

251

Answer

Solution:

According to the phpnumber_format, you want to group the number by thousands. You can to that by changing the return of thecalculate_value function like this:

return value.toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");

The regex will add a comma every 3 digits, like this:

   (function($) {

      $(document).ready(function(){

        var counter = $('#counter-number');
        var coutUp = Number(1);

        setInterval(function() {
          counter.text(calculate_value);
        }, 1000)

        function calculate_value() {
          var initDate = moment(20111031).format('x');
          var nowDate = moment().format('x');
          var dif = Number((nowDate - initDate) / 1000);
          var value = Number(dif * coutUp);
          // console.log(initDate, nowDate, dif, value, '<?= $init_date ?>');
          return value.toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
        }

      });

    })(jQuery);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id="counter-number"></span>



507
votes

Answer

Solution:

Now it should keep the comma. You can set the number of decimals by changing the (2) parameter in toFixed() function in the end of the JS function.

<?php
$bg = get_field('counter_bg');

$init_value = get_field('init_value');
$init_date = get_field('init_date');
$seconds = strtotime("now") - strtotime($init_date);
$countup_value = get_field('countup_value');
$number = round((($seconds * $countup_value) + $init_value) * 100) / 100;

if($number) :
 $title = get_field('counter_title');
 $text = get_field('counter_text');
 ?>
 <section id="home-counter" <?php if($bg['url']) echo "style='background-image: url({$bg['sizes']['slide-thumb']})'"; ?>>
  <div >
   <?php
   if($title) echo "<h3 class='counter-title'>{$title}</h3>";
   echo "<div id='counter-number'>";
   echo Number_format ($number);
   echo "</div>";
   if($text) echo "<div class='counter-text'>{$text}</div>";
   ?>

  </div><!--containr-->
 </section><!--home-section-->

 <script>
  (function($) {

   $(document).ready(function(){

    var counter = $('#counter-number');
    var coutUp = Number(<?= $countup_value ?>);

    setInterval(function() {
     counter.text(calculate_value);
    }, 1000)

    function calculate_value() {
     var initDate = moment('<?= $init_date ?>').format('x');
     var nowDate = moment().format('x');
     var dif = Number((nowDate - initDate) / 1000);
     var value = Number(dif * coutUp);
     // console.log(initDate, nowDate, dif, value, '<?= $init_date ?>');
     return value.toFixed(2).replace(".", ",")
    }

   });

  })(jQuery);
 </script>

<?php endif; ?>



People are also looking for solutions of the problem: xmlhttprequest error flutter
Source

Share


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.


Similar questions

Find the answer in similar questions on our website.

344 javascript - Php Counter Hides Comma after loading and not showing permanently
748 php - Cannot get element id in javascript inside for loop
451 php - CakePHP hasMany through (the join model) not saving
616 php - Views global counter IF condition is not working?
775 mysql - Records Update, but when grabbed from PHP file, do not change
818 php - PayPal auto return does not send back any POST data
48 mysql - PHP header not working as expected
622 json - PHP returning [false] when I have not asked it too
810 php - jqPlot label rendering not displaying correctly
393 php - htaccess is not working on iis
507

Answer

Solution:

Now it should keep the comma. You can set the number of decimals by changing the (2) parameter in toFixed() function in the end of the JS function.

<?php
$bg = get_field('counter_bg');

$init_value = get_field('init_value');
$init_date = get_field('init_date');
$seconds = strtotime("now") - strtotime($init_date);
$countup_value = get_field('countup_value');
$number = round((($seconds * $countup_value) + $init_value) * 100) / 100;

if($number) :
 $title = get_field('counter_title');
 $text = get_field('counter_text');
 ?>
 <section id="home-counter" <?php if($bg['url']) echo "style='background-image: url({$bg['sizes']['slide-thumb']})'"; ?>>
  <div >
   <?php
   if($title) echo "<h3 class='counter-title'>{$title}</h3>";
   echo "<div id='counter-number'>";
   echo Number_format ($number);
   echo "</div>";
   if($text) echo "<div class='counter-text'>{$text}</div>";
   ?>

  </div><!--containr-->
 </section><!--home-section-->

 <script>
  (function($) {

   $(document).ready(function(){

    var counter = $('#counter-number');
    var coutUp = Number(<?= $countup_value ?>);

    setInterval(function() {
     counter.text(calculate_value);
    }, 1000)

    function calculate_value() {
     var initDate = moment('<?= $init_date ?>').format('x');
     var nowDate = moment().format('x');
     var dif = Number((nowDate - initDate) / 1000);
     var value = Number(dif * coutUp);
     // console.log(initDate, nowDate, dif, value, '<?= $init_date ?>');
     return value.toFixed(2).replace(".", ",")
    }

   });

  })(jQuery);
 </script>

<?php endif; ?>

People are also looking for solutions to the problem: php - Yii Controller Can't Find View

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.