[英]Get the row number in the table and save it into php variable
I'm using while loop to display the data from the database into a table. 我正在使用while循环将数据库中的数据显示到表中。 It should allow user to redirect to other page to edit the information when user clicks on any of the row.
当用户单击任何行时,它应允许用户重定向到其他页面以编辑信息。 I am trying to get the row number which the user clicked on, and need to convert to a php variable.
我试图获取用户单击的行号,并且需要将其转换为php变量。 How can i do this?
我怎样才能做到这一点? Is there other way to get the row number?
还有其他获取行号的方法吗? Here is my code:
这是我的代码:
<div id="table-wrapper">
<div id="table-scroll">
<table class="table" border="1" border-spacing="2" padding="2" >
<tr class="row">
<td>
<font face="Arial, Helvetica, sans-serif">Staff ID</font>
</td>
<td>
<font face="Arial, Helvetica, sans-serif">Name</font>
</td>
<td>
<font face="Arial, Helvetica, sans-serif">Position</font>
</td>
</tr>
<?php
$i = 0;
while ($i < $num) {
$f1 = mysql_result($result, $i, "ID");
$f2 = mysql_result($result, $i, "Name");
$f3 = mysql_result($result, $i, "Position");
?>
<script>
function myFunction(x) {
alert("Row index is: " + x.rowIndex);
//i can get the row number using this
}
</script>
<tr onclick="myFunction(this)">
<td>
<font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font>
</td>
<td>
<font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font>
</td>
<td>
<font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font>
</td>
</tr>
<?php
$i++;
}
?>
</table>
</div>
</div>
It would be best to use an actual link, assuming that you're going to make a standard GET request (if not, please expand a bit on what it is that you're trying to do): 最好使用一个实际的链接,假设您要发出标准的GET请求(如果没有,请对您要执行的操作进行扩展):
<td>
<a href="path/to/some_script.php?n=<?php echo $i ?>">
<font face="Arial, Helvetica, sans-serif"><?php echo $f1 ?></font>
</a>
</td>
If you must use JavaScript, you could also store the number as data, which might be a bit more reliable: 如果必须使用JavaScript,还可以将数字存储为数据,这可能会更可靠:
<script>
function myFunction(el) {
alert(el.dataset.row);
}
</script>
<tr data-row="<?php echo $i ?>" onclick="myFunction(this)">
<font face="Arial, Helvetica, sans-serif"><?php echo $f1 ?></font>
</td>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.