-1

I have a code that project videos on youtube based on the youtube apiv3 my problem is how do I auto-increment the id of each li by 1 unit as it gets appended to the results I've tried several approaches using PHP to loop through the result but I keep getting an unwanted result.

<script>
    $.get(
      "https://www.googleapis.com/youtube/v3/playlistItems", data,
      function(data){
        var output;
        $('#results').html("");
        $.each(data.items, function(i, item){
          console.log(item);
          videoTitle = item.snippet.title;
          rvideoID = item.snippet.resourceId.videoId;
          vidThumburl = item.snippet.thumbnails.high.url;
          <?php $counter = 1; while($counter <= 10 ){ ?>
          output = '<li id="menu<?=$counter;?>" style="list-style:none;"><a class="videos2 video" href="https://www.youtube.com/watch?v=' + rvideoID + '">' + videoTitle + '<a/></li>';

          //Append to results list
          $('#results').append(output);
          <?php $counter++; } ?>
        });


        try{
          if(data.prevPageToken){
            $("#results").append('<li id="menu<?=$counter;?>" style="list-style:none;"><a class="" href="javascript:void(0);" onclick="javascript:getVids(pid, \'' + data.prevPageToken + '\');">&raquo; Prev Page<a/></li>');
          }
        }catch(err){
        }

        try{
          if(data.nextPageToken){
            $("#results").append('<li id="menu<?=$counter;?> style="list-style:none;"><a class="" href="javascript:void(0);" onclick="javascript:getVids(pid, \'' + data.nextPageToken + '\');">Next Page &laquo;<a/></li>');
          }
        }catch(err){
        }
    });
  }
</script>
<body>
<div id="container">
  <ul id="results"></ul>
</div>
</body>
Vikrant
  • 4,920
  • 17
  • 48
  • 72
drmacsika
  • 33
  • 9

1 Answers1

0

Just place your iterator into javascript-code

var counter = 1;
$.each(data.items, function(i, item){
    console.log(item);
    videoTitle = item.snippet.title;
    rvideoID = item.snippet.resourceId.videoId;
    vidThumburl = item.snippet.thumbnails.high.url;
    output = '<li id="menu' + counter + '" style="list-style:none;">' +
        '<a class="videos2 video" href="https://www.youtube.com/watch?v=' + rvideoID + '">' + videoTitle + '<a/>'+
        '</li>';

    //Append to results list
    $('#results').append(output);
    counter++;
});
br3t
  • 1,646
  • 2
  • 20
  • 27