title: How to call javascript function inside php foreach loop
body: <p>Good day, i am having a trouble regarding calling my ajax function inside php for each loop, the objective of my code is to create a dynamic count of comments and appreciations, my problem is the the console says that my function is not defined
  <br><br>
  Here is where i called my script, it is inside a foreach loop, take note.</p>
  
  <pre><code>    &lt;?php foreach($allDiscussion as $data): ?&gt;
          &lt;div class="col s12 com_app_count_section"&gt;
              &lt;script&gt;count_app_and_com('&lt;?=$data-&gt;discussion_id?&gt;');&lt;/script&gt;
          &lt;/div&gt;
  &lt;?php endforeach; ?&gt;
  </code></pre>
  
  <p><br><br>
  and here is my script code
  <br></p>
  
  <pre><code>&lt;script&gt;
      $(function(){
      //Add New
      $('.btnCommentSubmit').click(function(e){
          e.preventDefault();
          var discussion_id_val = $(this).data("value");
          var account_user_id_val = $(this).data("id");
          $('#formAddComment').attr('action', '&lt;?php echo base_url() ?&gt;Discussion/addComment/');
          var url = $('#formAddComment').attr('action');
          var addCommentTxt = $(this).closest('#formAddComment').find('.addCommentTxt').val();
          $.ajax({
              type: 'post',
              url: url,
              data: {
                  addCommentTxt:addCommentTxt,
                  discussion_id:discussion_id_val,
                  account_user_id:account_user_id_val,
              },
              success: function(e){
                  $('.addCommentTxt').val("");
                  showAllComments(discussion_id_val);
              },
              error: function(e){
                  console.log(e);
                  alert('Could not add data');
              }
          });
  
      });
  
  });
  
      function count_app_and_com(discussion_id_val){
          console.log(discussion_id_val);
      }
  
  
      function showAllComments(discussion_id_val){
          $.ajax({
              type: 'post',
              url: '&lt;?php echo base_url() ?&gt;Discussion/showAllComments/'+discussion_id_val,
              async: false,
              dataType: 'json',
              success: function(data){
                  var html = '';
                  var i;
                  console.log(data);
                  for(i=0; i&lt;data.length; i++){
                      html += '&lt;div class="row"&gt;'+
                      '&lt;div class="chip "&gt;'+
                      '&lt;img src="&lt;?=base_url()?&gt;assets/img/sample.svg" alt="Contact Person"&gt;'+
                      data[i].name+
                      '&lt;/div&gt;'+
                      '&lt;span class="color-grey hide-on-med-and-down" style="font-size: 0.8vw;"&gt;'+
                      data[i].comment_at_formatted+'&lt;/span&gt;'+
                      '&lt;span class="color-grey hide-on-large-only" &gt;&lt;?=date('M d, Y',$data-&gt;discussion_posted_at)?&gt;'+data[i].comment_at_formatted+'&lt;/span&gt;'+
                      '&lt;p class=" Comment'+data[i].comment_id+'"&gt;'+
                      data[i].comment_content+
                      '&lt;/p&gt;'+
                      '&lt;/div&gt;';
                  }
                  $('.comment_section').html(html);
              },
              error: function(){
  
              }
          });
      }   
  &lt;/script&gt;
  </code></pre>