动态表的唯一ID [英] unique id for dynamic table

查看:73
本文介绍了动态表的唯一ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将使用从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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆