title: How to include the values of a for loop into an array in Php?
body: <p>I have checked many similar answers for my problem. But couldn't find the correct one. My approach to the solution for the main question might be wrong. If so, please correct me.</p>
  <p>I have a task to make a table that has a button to add more rows. Then by clicking the save button, the whole table must be saved in the database. I thought of saving each row might be easy rather than saving the whole table at once. This is the code I used to create the rows.</p>
  <pre><code>function add_new_row_but(){
      var count=$('#divCount').val();
      count=parseInt(count);
      ++count;
      $('#divCount').val(count);
      var table = document.getElementById(&quot;assest_table&quot;);
      var row = table.insertRow(count);
      var cell1 = row.insertCell(0);
      var cell2 = row.insertCell(1);
      var cell3 = row.insertCell(2);
      cell1.innerHTML = count;
      cell2.innerHTML = '&lt;input type=&quot;text&quot; class=&quot;form-control&quot; placeholder=&quot;eg- mobile phone&quot; name=&quot;issuedItems'+count+'&quot; id=&quot;issuedItems'+count+'&quot; value=&quot;&lt;?php if (isset($rowdata)){echo $rowdata[0]-&gt;issuedItems;} ?&gt;&quot;&gt;';
      cell3.innerHTML = '&lt;input type=&quot;number&quot; class=&quot;form-control&quot; placeholder=&quot;eg- 5000/=&quot; name=&quot;cost'+count+'&quot; id=&quot;cost'+count+'&quot; value=&quot;&lt;?php if (isset($rowdata)){echo $rowdata[0]-&gt;cost;}?&gt;&quot;&gt;';
  
  }
  </code></pre>
  <p>Then I used a for loop to save the data row by row in the database.</p>
  <pre><code>for ($i = 1; $i &lt;= $count; $i++){
          $data = array(
      
              &quot;employeeid&quot; =&gt; $this-&gt;input-&gt;post(&quot;employeeid&quot;),
              &quot;employeename&quot; =&gt; $this-&gt;input-&gt;post(&quot;employeename&quot;),
              &quot;address&quot; =&gt; $this-&gt;input-&gt;post(&quot;address&quot;),
              &quot;issuedItems&quot; =&gt; $this-&gt;input-&gt;post(&quot;issuedItems'+$i+'&quot;),
              &quot;cost&quot; =&gt; $this-&gt;input-&gt;post(&quot;cost'+$i+'&quot;),
              &quot;issueddate&quot; =&gt; $this-&gt;input-&gt;post(&quot;issueddate&quot;)
  
          );
  
          print_r($data);
  
          try {
              
              return $this-&gt;db-&gt;insert(&quot;property&quot;, $data);
  
  
          } catch (Exception $exc) {
          
              return false;
              
          }
      }
  
  }
  </code></pre>
  <p>But in <code>print_r</code>, it's not showing the cost and issued items. I think the way I inject the for loop <code>$i</code> into the array is wrong as other values show as an array.</p>
  <p>Please solve this. Thank you.</p>