微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使 HTML 行可点击,然后显示来自数据库的行用户配置文件,对于使用 while 循环创建的表

如何解决使 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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?