title: How can pass my fetched data from database to my script array
body: <p>first, the snippet I provided is the function I want to recreate. This snippet combines 2 players in 1 array if they have greater than or less than equal 15 in weight.</p>
  <p>For example:
  <a href="https://i.stack.imgur.com/rLDF5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rLDF5.png" alt="This is my snippet result" /></a></p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>  const source = [
      
      { entryID: 1, entryName: "player1", weight: 1900 },
      { entryID: 2, entryName: "player2", weight: 1905 },
      { entryID: 3, entryName: "player3", weight: 1910 },
      { entryID: 4, entryName: "player4", weight: 1915 },
      { entryID: 5, entryName: "player5", weight: 1920 },
      { entryID: 6, entryName: "player6", weight: 1925 },
      { entryID: 7, entryName: "player1", weight: 1930 },
      { entryID: 8, entryName: "player2", weight: 1915 },
      { entryID: 9, entryName: "player3", weight: 1905 },
      { entryID: 10, entryName: "player4", weight: 1940 },
      { entryID: 11, entryName: "player5", weight: 1960 },
      { entryID: 12, entryName: "player6", weight: 1925 },
      { entryID: 13, entryName: "player1", weight: 1965 },
      { entryID: 14, entryName: "player2", weight: 1935 },
      { entryID: 15, entryName: "player3", weight: 1925 },
      { entryID: 16, entryName: "player4", weight: 1935 },
      { entryID: 17, entryName: "player5", weight: 1950 },
      { entryID: 18, entryName: "player6", weight: 1930 },
      { entryID: 19, entryName: "player1", weight: 2000 },
      { entryID: 20, entryName: "player2", weight: 2010 },
      { entryID: 21, entryName: "player3", weight: 2020 },
      { entryID: 22, entryName: "player4", weight: 1700 },
      { entryID: 23, entryName: "player5", weight: 1730 },
      { entryID: 23, entryName: "player6", weight: 1765 },
      { entryID: 24, entryName: "player1", weight: 1780 },
      { entryID: 25, entryName: "player2", weight: 1765 },
      { entryID: 26, entryName: "player3", weight: 1785 },
      { entryID: 27, entryName: "player4", weight: 1800 },
      { entryID: 28, entryName: "player5", weight: 1865 },
      { entryID: 29, entryName: "player6", weight: 1825 },
      { entryID: 30, entryName: "player1", weight: 1755 },
      { entryID: 31, entryName: "player2", weight: 1745 },
      { entryID: 32, entryName: "player3", weight: 1750 },
      { entryID: 33, entryName: "player4", weight: 1965 },
      { entryID: 34, entryName: "player5", weight: 1960 },
      { entryID: 35, entryName: "player6", weight: 1995 },
  
    ];
  
    function newCombine(data, difference) {
          let nonMatched = [...data]
      const groups = {};
  
      for (let i = 0; i &lt; nonMatched.length - 1; i++) {
        const first = nonMatched[i];
  
        inner: for (let j = nonMatched.length - 1; j &gt; i; j--) {
          const second = nonMatched[j];
          const delta = Math.abs(first.weight - second.weight);
  
          if (delta &lt;= difference &amp;&amp; first.entryName !== second.entryName) {
            const groupKey = `${first.weight}_${second.weight}`;
            groups[groupKey] = [first, second];
            nonMatched = nonMatched.filter(
              (obj) =&gt;
                obj.entryID != first.entryID &amp;&amp; obj.entryID != second.entryID
            );
            i = -1;
            break inner;
          }
        }
      }
      return { ...groups, ...nonMatched };
    }
  
    const a = newCombine(source, 15);
    console.log(a);</code></pre>
  </div>
  </div>
  </p>
  <p>Now my target is, how can I do the same from the snippet I provided to my ajax? Knowing that the data will be from my fetched data from database and not the static one.</p>
  <p><strong>html:</strong></p>
  <pre><code>                                               &lt;input type=&quot;text&quot; name=&quot;status&quot; id=&quot;status&quot;&gt;                   
                  &lt;h3&gt;Are you sure that you want to match?&lt;/h3&gt;
                    
                       
                                      &lt;table id=&quot;entry_list1&quot; class=&quot;table table-bordered table-hover&quot; cellspacing=&quot;0&quot; hidden&gt;
                                      
                                      &lt;thead&gt;
                                         &lt;tr&gt;
                                            &lt;th&gt;Entry ID&lt;/th&gt;
                                            &lt;th&gt;Event ID&lt;/th&gt;
                                            &lt;th&gt;Player &lt;/th&gt;
                                            &lt;th&gt;Weight&lt;/th&gt;
                                            &lt;th&gt;Status&lt;/th&gt; 
                                            &lt;th&gt;Lightband&lt;/th&gt;
  
                                         &lt;/tr&gt;
                                      &lt;/thead&gt;
                                      &lt;tbody&gt;
                                      &lt;/tbody&gt;
                                   &lt;/table&gt;
                                   
                           
                                   &lt;div id=&quot;result&quot;&gt; &lt;/div&gt;
                       
              
                
                    &lt;div class=&quot;modal-footer&quot;&gt;
        
                    &lt;button type=&quot;button&quot; class=&quot;btn btn-secondary pull-left&quot; data-dismiss=&quot;modal&quot;&gt;Close&lt;/button&gt;
                    &lt;button type=&quot;button&quot; id=&quot;submitMatching&quot; class=&quot;btn btn-primary&quot;&gt;Match&lt;/button&gt;    
                    &lt;/div&gt;
                    
                   
  </code></pre>
  <p><strong>Ajax:</strong></p>
  <pre><code>$(document).ready(function() {
  
  var eventID = $('#eventsssiud').val();
  
  
      //datatables
      table = $(&quot;#entry_list1&quot;).DataTable({
  
  
          processing: false,
          serverSide: true,
          order: [],
          searching: false,
          paging: false,
          info: false,
  
          ajax: {
              url: &quot;&lt;?php echo site_url('controller/function')?&gt;&quot;,
              type: &quot;POST&quot;,
              async: true,
              dataType: &quot;json&quot;,
              data: {eventID:eventID},
               success: function(data) {
  //On this success, I need to store my data to the const source so that it'll be dynamic and not the static one.
  
              },
          },
  
          &quot;columnDefs&quot;: [{
                  &quot;targets&quot;: [0], //first column
                  &quot;orderable&quot;: false, //set not orderable
              },
              {
                  &quot;targets&quot;: [-1], //last column
                  &quot;orderable&quot;: false, //set not orderable
              },
  
          ],
      });
  });
  </code></pre>
  <p><strong>Controller:</strong></p>
  <pre><code>public function ajax_list()
  {
     
      
      
      $list = $this-&gt;repo-&gt;get_datatables();
      $data = array();
  
      $no = $_POST['start'];
      foreach ($list as $person) {
          $no++;
       
              
              
  
          $data[] = array(
              &quot;entryID&quot;   =&gt; $person-&gt;entryID,
              &quot;entryName&quot; =&gt; $person-&gt;entryName,
              &quot;weight&quot;    =&gt; $person-&gt;weight,
              &quot;lightBand&quot; =&gt;$person-&gt;lightBand,
  
          );
  
      
      $output = array(
          &quot;draw&quot; =&gt; $_POST['draw'],
          &quot;recordsTotal&quot; =&gt; $this-&gt;repo-&gt;count_all(),
          &quot;recordsFiltered&quot; =&gt; $this-&gt;repo-&gt;count_filtered(),
          &quot;data&quot; =&gt; $data,
          
      );
      //output to json format
      
      echo json_encode( $output );
      
      
      
  }
  }
  </code></pre>
  <p>Model:</p>
  <pre><code>var $table = 'entry';
  var $column_order = array(null,'entryName','weight');
  var $order = array('entryID' =&gt; 'asc');
  var $column_search = array('entryID','entryName','weight');
  //set column field database for datatable orderable     
  
  private function _get_datatables_query()
  
  {
      if($this-&gt;input-&gt;post('eventsssiud')){
          $this-&gt;db-&gt;where('eventID',$this-&gt;input-&gt;post('eventsssiud'));
      }
    
      $this-&gt;db-&gt;from($this-&gt;table);
    
      
    
     
      
      
      $i = 0;
      
      foreach ($this-&gt;column_search as $item) // loop column
      {
          if($_POST['search']['value']) // if datatable send POST for search
          {
              
              if($i===0) // first loop
              {
                  $this-&gt;db-&gt;group_start(); // open bracket. query Where with OR clause better with bracket. because maybe can combine with other WHERE with AND.
                  $this-&gt;db-&gt;like($item, $_POST['search']['value']);
              }
              else
              {
                  $this-&gt;db-&gt;or_like($item, $_POST['search']['value']);
              }
              
              if(count($this-&gt;column_search) - 1 == $i) //last loop
                  $this-&gt;db-&gt;group_end(); //close bracket
          }
          $i++;
      }
      
      if(isset($_POST['order'])) // here order processing
      {
          $this-&gt;db-&gt;order_by($this-&gt;column_order[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
      }
      else if(isset($this-&gt;order))
      {
          $order = $this-&gt;order;
          $this-&gt;db-&gt;order_by(key($order), $order[key($order)]);
      }
  }
  
  function get_datatables()
  {
      $this-&gt;_get_datatables_query();
      
      if($_POST['length'] != -1)
          $this-&gt;db-&gt;limit($_POST['length'], $_POST['start']);
          $query = $this-&gt;db-&gt;get();
          return $query-&gt;result();
  }
  
  function count_filtered()
  {
      $this-&gt;_get_datatables_query();
      $query = $this-&gt;db-&gt;get();
      return $query-&gt;num_rows();
  }
  
  public function count_all()
  {
      $this-&gt;db-&gt;from($this-&gt;table);
      
     
      
      
      
      $i = 0;
      
      foreach ($this-&gt;column_search as $item) // loop column
      {
          if($_POST['search']['value']) // if datatable send POST for search
          {
              
              if($i===0) // first loop
              {
                  $this-&gt;db-&gt;group_start(); // open bracket. query Where with OR clause better with bracket. because maybe can combine with other WHERE with AND.
                  $this-&gt;db-&gt;like($item, $_POST['search']['value']);
              }
              else
              {
                  $this-&gt;db-&gt;or_like($item, $_POST['search']['value']);
              }
              
              if(count($this-&gt;column_search) - 1 == $i) //last loop
                  $this-&gt;db-&gt;group_end(); //close bracket
          }
          $i++;
      }
      
      if(isset($_POST['order'])) // here order processing
      {
          $this-&gt;db-&gt;order_by($this-&gt;column_order[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
      }
      else if(isset($this-&gt;order))
      {
          $order = $this-&gt;order;
          $this-&gt;db-&gt;order_by(key($order), $order[key($order)]);
      }
      return $this-&gt;db-&gt;count_all_results();
  }
  </code></pre>