Skip to content Skip to sidebar Skip to footer

Unique Id For Dynamic Table

I will be generating a HTML table with data pulled from MySQL.The number of rows in my MySQL table are not fixed. &l

Solution 1:

Why can't you do something like this ?

<?php
  $i = 1;
  while($row=mysql_fetch_assoc($result))
  { ?>
    <tr id="row<?php echo $i;?>">
      <td id="cell-left-<?php echo $i;?>"><?php echo $row['col1'];?></td>
      <td id="cell-right-<?php echo $i;?>"><?php echo $row['col2'];?></td>
    </tr>
  <?php
     $i++;
   } ?>

Please note, I have added ids row, cell-left- and cell-right- by myself. You may change them as per your requirements.


Solution 2:

You can use a counter when iterating through the rows, maybe something like this:

<?php
    $rowCount = 0;

    while($row=mysql_fetch_assoc($result))
    {
        $rowCount++;
        ?>
        <tr id="<?php echo 'row' . $rowCount;?>">
            <td><?php echo $row['col1'];?></td>
            <td><?php echo $row['col2'];?></td>
        </tr>
        <?php
    }

?>

You can now select an element with

var rowID = 1;
document.getElementById("row" + rowID);

Hope this helps.


Post a Comment for "Unique Id For Dynamic Table"