崩溃中的PHP表
我很难把桌子上的下一行折叠起来。我的代码仅适用于表的第一行,在该行中我单击了折叠按钮 代码:崩溃中的PHP表,php,html-table,Php,Html Table,我很难把桌子上的下一行折叠起来。我的代码仅适用于表的第一行,在该行中我单击了折叠按钮 代码: <table id="yourEvent" class="table table-hover"> <thead> <tr> <td style="text-align: center" w
<table id="yourEvent" class="table table-hover">
<thead>
<tr>
<td style="text-align: center" width="20px"><strong>No.</strong></td>
<td><strong>Attendee Name</strong></td>
</tr>
</thead>
<?php while($row = mysqli_fetch_array($resultAttendeeList))
{ ?>
<tbody>
<tr>
<td class="counterCell" style="text-align: center" width="20px"></td>
<td><button type="button" class="btn btn-xs bg-maroon" style="width: 20px" data-toggle="collapse" data-target="#divCol"><i class="fa fa-plus"></i></button> <?php echo $row['fullname'];?>
<div class="collapse" id="divCol">
<table>
<tr>
<td>Address: <?php echo $row['address']; ?></td>
</tr>
<tr>
<td>Contact: 0<?php echo $row['contact']; ?></td>
</tr>
</table>
</div>
</td>
</tr>
<?php } ?>
</tbody>
</table>
否。
与会者姓名
&emsp;
地址:
联系人:0
您在循环中对多个div使用相同的ID。您可以创建增量int变量并在div id中连接
尝试以下代码:通过循环添加变量增量。此外,您应该在
标记之后启动循环
<tbody>
<?php $ctr=1;
while($row = mysqli_fetch_array($resultAttendeeList))
{ ?>
<tr>
<td class="counterCell" style="text-align: center" width="20px"></td>
<td><button type="button" class="btn btn-xs bg-maroon" style="width: 20px" data-toggle="collapse" data-target="#divCol_<?=$ctr?>"><i class="fa fa-plus"></i></button> <?php echo $row['fullname'];?>
<div class="collapse" id="divCol_<?=$ctr?>">
<table>
<tr>
<td>Address: <?php echo $row['address']; ?></td>
</tr>
<tr>
<td>Contact: 0<?php echo $row['contact']; ?></td>
</tr>
</table>
</div>
</td>
</tr>
<?php $ctr=$ctr+1; } ?>
联系人:0
请尝试这是完整的代码。请提供动态id。确保在处理任何jquery/javascript操作时,divid始终是唯一的。否则,事件将不会为所有元素绑定
<?php
$servername = "host";
$username = "user";
$password = "pass";
// Create connection
$conn = mysqli_connect($servername, $username, $password, 'testdb');
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM table ORDER BY ID limit 5";
$result = mysqli_query($conn, $sql);
// Numeric array
//$row = mysqli_fetch_array($result);
?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<table id="yourEvent" class="table table-hover">
<thead>
<tr>
<td style="text-align: center" width="20px"><strong>No.</strong></td>
<td><strong>Attendee Name</strong></td>
</tr>
</thead>
<tbody>
<?php
$j = 1;
while ($row = mysqli_fetch_array($result)) {
?>
<tr>
<td class="counterCell" style="text-align: center" width="20px"><?php echo $j; ?></td>
<td>
<button type="button" class="btn btn-xs bg-maroon" style="width: 20px" data-toggle="collapse" data-target="#divCol_<?php echo $j; ?>">
<i class="fa fa-plus"></i>
</button> <?php echo $row['EMP_FULLNAME']; ?>
<div class="collapse" id="divCol_<?php echo $j; ?>">
<table>
<tr>
<td>Address: <?php echo $row['EMP_ADDRESS']; ?></td>
</tr>
<tr>
<td>Contact: 0<?php echo $row['EMP_MOBILE']; ?></td>
</tr>
</table>
</div>
</td>
</tr>
<?php
$j++;
}
?>
</tbody>
</table>
</body>
</html>
否。
与会者姓名
联系人:0
id
s应该是唯一的,但是您在循环中使用了id=“divCol”
。所以你有了count($resultatendeelist)
相同的id
号。因此,它只识别第一行,而不识别任何后续行。谢谢!这管用!我在里面使用echo这个人id,它的工作原理和charmtried代码一样,但它的工作原理是一样的。还是只开第一家row@ktato你可以速记$ctr=$ctr+1
只需$ctr++
@Eefy。感谢您尝试我的代码,尽管它没有解决问题。呵呵:)@ktaro您在循环中将$ctr
的值设置为1,因此它将始终为每个div分配divCol\u 1
。将该变量移到循环之外,它应该可以正常工作。嗨@Thomascheffer。哦,是的!我没看到。谢谢你指点我。@Eefy也许这就是我的代码不起作用的原因。我刚刚更新了我的答案。:)