Vineet Basantani Vineet Basantani - 6 months ago 7
HTML Question

How can I add rows in my HTML table equal to the number of entries I have in Mysql DB?

Also, how can I access the

<td>
for each value?
This is my table in HTML:

<table class="inventory" style="float:left; position:relative;">
<tbody>
<tr>
<th>Name</th>
<th>Rate</th>
<th>OT</th>
<th>Leaves</th>
<th>Total</th>
</tr>
<tr>
<td>
<input type="text" placeholder="Name"/>
</td>
<td>
<input type="text" placeholder="Rate"/>
</td>
<td>
<input type="text" placeholder="OT"/>
</td>
<td>
<input type="text" placeholder="Leaves"/>
</td>
<td>
<input type="text" placeholder="Total"/>
</td>
</tr>
<tr>
<td>
<input type="text" placeholder="Name"/>
</td>
<td>
<input type="text" placeholder="Rate"/>
</td>
<td>
<input type="text" placeholder="OT"/>
</td>
<td>
<input type="text" placeholder="Leaves"/>
</td>
<td>
<input type="text" placeholder="Total"/>
</td>
</tr>
</tbody>
</table>


How can I add rows in my HTML table equal to the number of entries I have in Mysql DB?

Answer

I am assuming your database connection as $con. Place your database table name and column name in appropriate place.

<table class="inventory" style="float:left; position:relative;">
  <tbody>
    <tr>
      <th>Name</th>
      <th>Rate</th>
      <th>OT</th>
      <th>Leaves</th>
      <th>Total</th>
    </tr>
    <?php
    $query = mysqli_query($con,"SELECT * FROM tableName");
    while($row = mysqli_fetch_array($query,MYSQLI_ASSOC))
    {?>
    <tr>
      <td>
        <input type="text" placeholder="Name" value="<?php echo $row['Name_Column_name']?>"/>
      </td>
      <td>
        <input type="text" placeholder="Rate" value="<?php echo $row['Rate_Column_name']?>"/>
      </td>
      <td>
        <input type="text" placeholder="OT" value="<?php echo $row['OT_Column_name']?>"/>
      </td>
      <td>
        <input type="text" placeholder="Leaves" value="<?php echo $row['Leaves_Column_name']?>"/>
      </td>
      <td>
        <input type="text" placeholder="Total" value="<?php echo $row['Total_Column_name']?>"/>
      </td>
    </tr>
    <?php }?>
  </tbody>
</table>
Comments