title: Adding or Creating "forEach" to function in javascript / jquery
body: <p>First and foremost I'd like to thank everyone who participates contributes and adds to the forums / community, you guys provide an extremely appreciated and valued resource to those that just need information and assistance when needed.  Simple thing to do for some, but to many it means volumes.  So thanks again.</p>
  
  <p>I'm no coding or script expert but I definitely learn quick and know enough to get around and translate (especially if the language is explained) attempting to get a player on my site to populate and propagate a playlist based on an uploaded file string.</p>
  
  <p>So far I have the player completely up and running, it properly parses the upload links to display the separate mp3 files to play as well as the separate description information per file.</p>
  
  <p>The problem is when I click the play button all of the files mp3 play at once.   I'm almost positive I need to add a forEach statement or something along those lines either here:</p>
  
  <pre><code>$(function(){
      $('#player1').plp({
          'volume': 80,
          'playlist':[
              {"title":"{$pic.video_descr }", 
              "author":"{.Me}", 
              "cover":"files/covers/fullfilename", 
              "mfile":"{.siteurl}{.fullfilename}", 
              "duration":"330"},
  
          ] 
       });
  });
  </code></pre>
  
  <p>for each title, author, cover, duration and mfile, </p>
  
  <p>or somewhere around here</p>
  
  <pre><code>function init_track(i){
      cpl = i;
      $this.find('.playlist span').removeClass('active');
      $this.find('.playlist span:eq('+i+')').addClass('active');
      $audio.src = options.playlist[i].mfile;
      $this.find('.title').html(options.playlist[i].title);
      $this.find('.author').html(options.playlist[i].author);
      $this.find('.cover').attr('src', options.playlist[i].cover);
  }
  
  
  for(i=0; i &lt; options.playlist.length; i++){
      $this.find('.playlist').append('&lt;span&gt;'+options.playlist[i].author+' - '+options.playlist[i].title+'&lt;/span&gt;');
  }
  init_track(cpl);
  
  $this.find('.playlist span').click(function(){
      init_track($(this).index());
  });
  </code></pre>
  
  <p>here is the full .js</p>
  
  <pre><code>(function($) {
      jQuery.fn.plp = function(options) {
          var options = $.extend({
              volume: 50,
              playlist: [],
              autostart: false
          }, options);
  
          var make = function() {
              var $this = $(this);
              var cpl = 0;
              var $audio = new Audio();
              var isrand = false;
  
              $this.find('.volume').slider({
                  animate: true,
                  range: 'min',
                  value: options.volume,
                  min: 0,
                  max: 1,
                  step: 0.01,
                  slide: function(event, ui) {
                      $audio.volume = ui.value;
                  }
              });
  
              $this.find('.long').slider({
                  animate: true,
                  range: 'min',
                  value: 0,
                  min: 0,
                  max: 60,
                  step: 1,
                  slide: function(event, ui) {
                      $audio.currentTime = ui.value;
                  }
              });
  
              $audio.addEventListener('canplay', function(_event) {
                  if ($audio.duration) {
                      $this.find('.all').html('&amp;nbsp;/&amp;nbsp;' + toMinit($audio.duration));
                      $this.find('.long').slider({
                          'max': $audio.duration
                      });
                  } else {
                      this.find('.all').html('&amp;nbsp;/&amp;nbsp;' + toMinit(options.playlist[cpl].duration));
                      $this.find('.long').slider({
                          'max': options.playlist[cpl].duration
                      });
                  }
  
                  if (options.autostart) {
                      $audio.play();
                      $this.find('.tlb_stop').addClass('isplay');
                  } else {
                      options.autostart = true;
                  }
              });
  
              $audio.addEventListener('ended', function() {
                  if (isrand) {
                      var rand = cpl;
                      while (rand == cpl) {
                          rand = Math.floor(Math.random((options.playlist.length)));
                      }
  
                      init_track(rand);
                  } else {
                      if (cpl == options.playlist.length - 1) {
                          cpl = -1;
                      }
                      init_track(cpl + 1);
                  }
              });
  
              $audio.addEventListener('timeupdate', function() {
                  $this.find('.long').slider({
                      'value': $audio.currentTime
                  });
                  $this.find('.current').html(toMinit($audio.currentTime));
              });
  
  
              function toMinit(val) {
                  val = Number(val);
                  var ost = Math.floor(val % 60);
                  var tm = Math.floor(val / 60);
                  if (ost &lt; 10) {
                      ost = '0' + ost;
                  }
                  if (tm &lt; 10) {
                      tm = '0' + tm;
                  }
                  return tm + ':' + ost;
              }
  
              function init_track(i) {
                  cpl = i;
                  $this.find('.playlist span').removeClass('active');
                  $this.find('.playlist span:eq(' + i + ')').addClass('active');
                  $audio.src = options.playlist[i].mfile;
                  $this.find('.title').html(options.playlist[i].title);
                  $this.find('.author').html(options.playlist[i].author);
                  $this.find('.cover').attr('src', options.playlist[i].cover);
              }
  
              for (i = 0; i &lt; options.playlist.length; i++) {
                  $this.find('.playlist').append('&lt;span&gt;' + options.playlist[i].author + '  - ' + options.playlist[i].title + ' &lt; /span&gt;');
              }
              init_track(cpl);
  
              $this.find('.playlist span').click(function() {
                  init_track($(this).index());
              });
  
              $this.find('.tlb_prev').click(function() {
                  if (isrand) {
                      var rand = cpl;
                      while (rand == cpl) {
                          rand = Math.floor(Math.random() *
  
                              (options.playlist.length));
                      }
  
                      init_track(rand);
                  } else {
                      if (cpl === 0) {
                          cpl = options.playlist.length;
                      }
                      init_track(cpl - 1);
                  }
                  return false;
              });
  
              $this.find('.tlb_stop').click(function() {
                  if ($audio.paused) {
                      $audio.play();
                      $(this).addClass('isplay');
                  } else {
                      $audio.pause();
                      $(this).removeClass('isplay');
                  }
                  return false;
              });
  
              $this.find('.tlb_next').click(function() {
                  if (isrand) {
                      var rand = cpl;
                      while (rand == cpl) {
                          rand = Math.floor(Math.random() *
                              (options.playlist.length));
                      }
  
                      init_track(rand);
                  } else {
                      if (cpl == options.playlist.length - 1) {
                          cpl = -1;
                      }
                      init_track(cpl + 1);
                  }
                  return false;
              });
  
              $this.find('.vol_icon').click(function() {
                  $(this).toggleClass('active');
                  $this.find('.volume').fadeToggle(100);
                  return false;
              });
  
              $this.find('.pl_icon').click(function() {
                  $(this).toggleClass('active');
                  $this.find('.playlist').fadeToggle(100);
                  return false;
              });
  
              $this.find('.while_icon').click(function() {
                  if ($audio.loop) {
                      $(this).removeClass('active');
                      $audio.loop = false;
                  } else {
                      $(this).addClass('active');
                      $audio.loop = true;
                  }
                  return false;
              });
  
              $this.find('.rand').click(function() {
                  if (isrand) {
                      $(this).removeClass('active');
                      isrand = false;
                  } else {
                      $(this).addClass('active');
                      isrand = true;
                  }
                  return false;
              });
          };
          return this.each(make);
      };
  })(jQuery);
  </code></pre>
  
  <p>Here is the PHP/HTML (smarty template):</p>
  
  <pre><code>&lt;script type="text/javascript" src="/jquery.js"&gt;   
  &lt;/script&gt;
  &lt;script type="text/javascript" src="/player.js"&gt;  
  &lt;/script&gt;
  
  {foreach item=pic from=$pics} {/foreach}
  &lt;table&gt;
      &lt;tr&gt;
          &lt;td valign="middle"&gt;
              {if $pic.ext == 'yt'}
              &lt;div style=
              "background-image:url('http://img.youtube.com/vi/{$pic.ytref}/0.jpg');"&gt;
              &lt;div style="height:120px; max-width:120px;"&gt;
                      &lt;a alt="video no {$pic.videono}" href=
                      "http://www.youtube.com/v/{$pic.ytref}&amp;amp;fs=1&amp;amp;rel=0::"
                      title="Click to Play"&gt;&lt;img height="120" onmouseout=
                      "this.src='playbtred.png'" onmouseover=
                      "this.src='playbty.png'" src="playbtred.png" width=
                      "120"&gt;&lt;/a&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;div style=
              "width:60%;word-wrap: break-word;padding-bottom:20px;"&gt;
                  {$pic.video_descr }
              &lt;/div&gt;{elseif $pic.ext == 'mp3'} 
              &lt;script type="text/javascript"&gt;
  
  
              $(function(){
              $('#player1').plp({
              'volume': 80,
              'playlist':[
                  {"title":"{$pic.video_descr }", 
                  "author":"Test2", 
                  "cover":"files/covers/1.jpg", 
                  "mfile":"{$config.siteurl}{$pic.fullfilename}", 
                  "duration":"330"},
  
              ] 
              });
  
  
              });   
  
              &lt;/script&gt;&lt;br&gt;
              &lt;div style=
              "width:60%;word-wrap: break-word;padding-bottom:20px;"&gt;
              &lt;/div&gt;{else} {include file='otheruploads.tpl'}&lt;br&gt;
              {$pic.video_descr } {/if}
          &lt;/td&gt;
      &lt;/tr&gt;
  &lt;/table&gt;
  &lt;div class="player" id="player1"&gt;
      &lt;img alt="" class="cover" src=""&gt;&lt;!--album cover--&gt;
       &lt;span class="title"&gt;&lt;/span&gt;&lt;!--song title--&gt;
       &lt;span class="author"&gt;&lt;/span&gt;&lt;!--artist--&gt;
      &lt;div class="long"&gt;&lt;/div&gt;
      &lt;div class="current"&gt;
          00:00
      &lt;/div&gt;
      &lt;div class="all"&gt;&lt;/div&gt;
      &lt;div class="volume"&gt;&lt;/div&gt;
      &lt;div class="vol_icon"&gt;&lt;/div&gt;
      &lt;div class="rand"&gt;&lt;/div&gt;
      &lt;div class="while_icon"&gt;&lt;/div&gt;
      &lt;div class="toolbar"&gt;
          &lt;div class="tlb_prev"&gt;&lt;/div&gt;
          &lt;div class="tlb_stop"&gt;&lt;/div&gt;
          &lt;div class="tlb_next"&gt;&lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="pl_icon active"&gt;&lt;/div&gt;
      &lt;div class="playlist flexcroll"&gt;&lt;/div&gt;
  &lt;/div&gt;{/if}
  </code></pre>
  
  <p>
  </p>
  
  <p>I'm just not sure where or how.  I've literally spent the last two days searching and researching to figure it out.  I'm so sure its simple, but I'm lost at the moment, and would GREATLY appreciate the help.</p>