我试图在有人单击时在动态表中获取信息,因此我可以逐列阅读并提取数据,然后将其显示在文本框中。由于这是我的新语言,所以不知道如何做,希望您能提供帮助。目前,我正在做的是从数据库中提取数据并使用它创建动态表。
我已经尝试了一些方法,但是它们都没有起作用,因此我不会在这里粘贴所有失败的尝试。
这是我的代码(因为很多人在使用mysql时遇到问题,所以将其更新为Mysqli_:
if (($result)||(mysqli_errno == 0))
{
echo "<table cellspacing='0' cellpadding='0' border='0' width='126%'>
<tr>
<td>
</table>
<div style='width:450; height:350px; overflow:auto;'>
<table cellspacing='0' cellpadding='1' border='1' width='380px'>
<tr style='color:white;background-colorgrey'>";
if (mysqli_num_rows($result)>0)
{
//loop thru the field names to print the correct headers
$i = 0;
while ($i < mysqli_num_fields($result))
{
$fetch = mysqli_fetch_field_direct($result, $i);
echo "<th>". $fetch->name . "</th>";
$i++;
}
//display the data
while ($rows = mysqli_fetch_assoc($result))
{
echo "<tr>";
foreach ($rows as $data)
{
echo "<td align='center' width='400px'>". $data . "</td>";
}
echo"</tr>";
}
}else{
echo "<tr><td colspan='" . ($i+1) . "'>No Results found!</td></tr>";
}
echo "</table></div>";
最佳答案
对于任何对答案感兴趣的人,我都知道了(变量“ id”存储列“ 0”中的元素,但您可以将0更改为不同的列):
<table cellspacing='0' cellpadding='0' border='0' width='126%'>
<tr>
<td>
</table>
<div style='width:450; height:350px; overflow:auto;'>
<table style="display: table-row" class='prueba' cellspacing='0' cellpadding='1' border='1' width='380px'>
<tbody>
<tr style='color:white;background-colorgrey'>
<?php
///******///
///CONECTION AND QUERY INFO WOULD GO HERE///
///******///
if (($result)||(mysqli_errno == 0))
{
if (mysqli_num_rows($result)>0)
{
//loop thru the field names to print the correct headers
$i = 0;
echo "<th><a>Check</a></th>";
while ($i < mysqli_num_fields($result))
{
$fetch = mysqli_fetch_field_direct($result, $i);
echo "<th><a>". $fetch->name . "</a></th>";
$i++;
}
//display the data
while ($rows = mysqli_fetch_assoc($result))
{
echo "<tr onClick='Content(this)'>";
foreach ($rows as $data)
{
echo "<td align='center' width='400px'><a>". $data . "</a></td>";
}
echo"</tr>";
}
}/*else{
echo "<tr><td colspan='" . ($i+1) . "'>No Results found!</td></tr>";
}*/
}else{
echo "'Error in running query :'. mysqli_error()";
}
//echo "<style>";
/*echo "</tr></table></td></tr><tr><td>
<table min-width='400px' cellspacing='0' cellpadding='1' border='1'>";*/
echo "</table></div>";
echo "<script>
function Content(elem){
elem.style.backgroundColor = 'red';
var id = $(elem).find('td:eq(0)').text();
alert(id);}
</script>";
?>
</p>