如何解决使 HTML 行可点击,然后显示来自数据库的行用户配置文件,对于使用 while 循环创建的表
我使用 while 循环创建了一个用户的 HTML 表,以从数据库(动态表)中获取表数据, 但我想让行或至少该行的第一个单元格可点击,当点击它时,它会打开另一个页面,显示来自点击行的用户信息,有点像用户个人资料。 显示信息的页面也是动态的,并根据单击的行从数据库中检索数据。这是我的表代码
<table class="container">
<thead>
<tr>
<th><h1>Name</h1></th>
<th><h1>Code</h1></th>
<th><h1>Votes Today</h1></th>
<th><h1>Total Votes</h1></th>
</tr>
</thead>
<?PHP
$conn=MysqLi_connect("localhost","root","","voting");
if(!$conn)
die("connection error");
$sql = "SELECT `name`,`code`,`Votetoday`,`total` from contestent";
$result = $conn-> query($sql);
if ($result-> num_rows > 0) {
while ($row = $result-> fetch_assoc())
{
echo "<tr><td>" . $row["name"] . "</td><td>" . $row["code"] . "</td><td>" . $row["Votetoday"] . "</td><td>".
$row["total"] . "</td><tr>";
}
echo "</table>";
}
?>
</table>
<?PHP
$sql1 = MysqLi_query($result,"SELECT name,code,Age from contestent,images where
contestent.code = images.cont_id and images.cont_id=3");
// the number three in cont_id=3 shoud be a variable with a value depending on which row was clicked from the table.
while($rows1 = MysqLi_fetch_array($sql1))
{
$contName = $rows1 ['name'];
$contAge = $rows1 ['Age'];
$contCode = $rows1 ['code'];
echo "<h3> Name: ". $contName."<br>";
echo "Age: ".$contAge."<br>";
echo "Code: ".$contCode;
}
?>
解决方法
您可以使用 GET
方法解决此问题。
GET 方法发送附加到页面请求的编码用户信息。页面和编码信息由 ? 分隔。字符。
http://www.test.com/profile.php?name=Adam&code=25
在 profile.php
页面中,您可以获取值,
<?php
if( $_GET["name"] || $_GET["code"] ) {
echo "Welcome ". $_GET['name']. "<br />";
echo "Your Code ". $_GET['code'];
exit();
}
?>
因此 HTML 代码段将如下所示,
<table class="container">
<thead>
<tr>
<th><h1>Name</h1></th>
<th><h1>Code</h1></th>
<th><h1>Votes Today</h1></th>
<th><h1>Total Votes</h1></th>
</tr>
</thead>
<?php
$conn=mysqli_connect("localhost","root","","voting");
if(!$conn)
die("connection error");
$sql = "SELECT `name`,`code`,`votetoday`,`total` from contestent";
$result = $conn-> query($sql);
if ($result-> num_rows > 0) {
while ($row = $result-> fetch_assoc())
{
echo "<tr><td>" . $row["name"] . "</td><td>" . $row["code"] . "</td><td>" . $row["votetoday"] . "</td><td>".$row["total"] . "</td>
<td><a href='profile.php?name=".$row["name"]."&code=".$row["code"]."'>View Profile</a></td><tr>";
}
echo "</table>";
}
?>
</table>
您可以根据需要添加更多 GET
变量。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。