动态表的唯一ID [英] unique id for dynamic table
问题描述
我将使用从MySQL提取的数据来生成HTML表.MySQL表中的行数不是固定的.
I will be generating a HTML table with data pulled from MySQL.The number of rows in my MySQL table are not fixed.
<?php
while($row=mysql_fetch_assoc($result))
{ ?>
<tr>
<td><?php echo $row['col1'];?></td>
<td><?php echo $row['col2'];?></td>
</tr>
<?php } ?>
现在如何为表行
和 table data
元素分配唯一的 id
??
Now how do I have the table rows
and table data
elements assigned unique id
??
由于行数不固定,我无法为新循环设置退出条件,因此无法生成另一个循环.
Another loop to generate them won't work as I can't set an exit condition for the new loop as number of rows are not fixed.
请指导我如何进行此操作.我只能使用 Javascript
,不能使用 JQUERY
.
Please guide me as to how to go forward about it. I can only use Javascript
and not JQUERY
.
推荐答案
为什么不能做这样的事情?
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++;
} ?>
请注意,我自己添加了编号 row
, cell-left-
和 cell-right-
.您可以根据自己的要求进行更改.
Please note, I have added ids row
, cell-left-
and cell-right-
by myself. You may change them as per your requirements.
这篇关于动态表的唯一ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!