Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jul 2008
    Posts
    43
    Thanks
    7
    Thanked 0 Times in 0 Posts

    jquery.percentageloader in PHP loop help

    I found this great percentage loader (http://widgets.better2web.com/loader/) that I would like to incorporate within a PHP while loop. I'm not a javascript programmer and could use some help with the following:

    1. Auto start the function when the page loads
    2. Include my PHP Loop within the function

    Here is the javascript code:

    Code:
     
    <script>
            $(function() {
              var $topLoader = $("#topLoader").percentageLoader({width: 256, height: 256, controllable : true, progress : 0.5, onProgressUpdate : function(val) {
                  $topLoader.setValue(Math.round(val * 100.0));
                }});
    
              var topLoaderRunning = false;
              $("#animateButton").click(function() {
                if (topLoaderRunning) {
                  return;
                }
                topLoaderRunning = true;
                $topLoader.setProgress(0);
                $topLoader.setValue('0kb');
                var kb = 0;
                var totalKb = <?PHP echo $total; ?>;
                
                var animateFunc = function() {
                  kb += 17;
                  $topLoader.setProgress(kb / totalKb);
                  $topLoader.setValue(kb.toString() + 'kb');
                  
                  if (kb < totalKb) {
                    setTimeout(animateFunc, 25);
                  } else {
                    topLoaderRunning = false;
                  }
                }
                
                setTimeout(animateFunc, 25);
                
              });
            });      
          </script>
    Here is my simple PHP Loop:

    PHP Code:
    <?PHP 
          $i 
    0
          
          while (
    $i != 100){ 
          
    $i++;
          } 
          
    ?>
    How can I place my PHP loop within the function so that the function runs at the same time? For example: (see my comments):

    Code:
                  if (kb < totalKb) {
                    setTimeout(animateFunc, 25);
    				
    				//PLACE LOOP HERE? 
                  } else {
    				 //PLACE LOOP HERE? 
                    topLoaderRunning = false;
                  }
    Also, how can I set the function to turn on automatically without clicking a button? Thanks in advanced for anyone's help on this.

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,391
    Thanks
    11
    Thanked 568 Times in 561 Posts
    I think you have a basic misunderstanding here. PHP is used only(!) to create the page in the first place. The page contains your HTML markup and also your inline Javascript code. Once PHP has finished(!), Javascript on the page will start running.

    This is the very simple truth. Basically that means: You cannot invoke any PHP from Javascript unless you request a new PHP page by either
    a) refreshing the page (Javascript on the current page will stop running and will be restarted from scratch after refresh)
    b) requesting a new page with page refresh (see a)
    c) requesting the output of a new PHP page using XMLHttpRequest (or respective JS framework methods). This will keep your current Javascript running and you will have to make sure to process the response (output) from the PHP script inside your Javascript (it will not be shown automatically)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •