php - 包含数据的可点击 HTML 表格能够在点击时显示更多数据

标签 php html css sql

我有我的 MySql DB(其中包含客户信息),我从中获取数据,并在表格中显示它们。我只在表格上显示客户姓名。我希望能够在每个表格行内单击一个按钮,然后显示该特定行的附加信息

代码在这里:

function customers(){
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "gymdb";
$p = '';
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM company_customers WHERE company_id=" . $_SESSION['id'] . ";";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    $p .= '
        <hmtl>
        <head>

        </head>
        <body>
            <table class="container">
                <thead>
                    <tr>
                        <th><h1>Name</h1></th>
                        <th><h1>Surname</h1></th>
                        <th><h1>More Info</h1></th>
                    </tr>
                </thead>
    ';
    while($row = $result->fetch_assoc()) {
        $p .= '
                <tbody>
                        <tr>
                            <td style="width:50%">' . $row['customer_name'] . '</td>
                            <td>' . $row['customer_surname'] . '</td>
                            <td  style="width:100px" class=thisone onclick="show()">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; +</td>
                        </tr>             
                </tbody>
            <script>

            </script>
            </body>
            </html>
            ';
    }
} else {

}
$conn->close();    

最佳答案

<td style="width:100px"
    class=thisone onclick="show('.$row['customer_id'].');">

    <div class="hidden_content" style="display:none;" id="content_'.$row['customer_id'].'">
    ' . $row['customer_name'] . '
    </div>
</td>

然后需要实现show功能:

function show(id){
    document.getElementById('content_'+id).style.display='block';
}

更好的决定是不要将大量数据发送到单个页面并通过额外的 ajax 查询获取额外的信息。

关于php - 包含数据的可点击 HTML 表格能够在点击时显示更多数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46055143/

相关文章:

java - 如何选择安全消息传递前端和符合 HIPAA 的数据库解决方案?

html - 我的水平子导航还有几个问题......但差不多了

javascript - 使用 jQuery 获取另一个元素

javascript - 如何在同一个 html 文档中制作多个幻灯片?

php - Composer 错误 : "PHP extension fileinfo is missing from your system"

javascript - 如何在 JavaScript 函数中声明 PHP 变量?

html - ie8下拉菜单乱码

javascript - 浏览器缓存图像防止淡入

javascript - 折叠不适用于 Bootstrap 的移动导航栏

php - 从一个选择中获取值并从数据库中调用另一选择的值