我有我的 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()"> +</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/