预期:
从我的SQL中选择数据。Foreach以回显数据和按钮。当数据为空时;加入";具有onclick功能的按钮。当数据是"0"时;加入";,隐藏";加入";具有onclick功能的按钮;结果";具有onclick功能的按钮。
结果:
无法隐藏";加入";具有onclick功能的按钮;结果";具有onclick功能的按钮。
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "battle";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT * FROM battleRecord Group by id");
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach ($stmt->fetchAll() as $row) {
if ($row['participation'] == 'joined') {
$displayJoin = 'none';
$displayResult = 'block';
} else {
$displayJoin = 'block';
$displayResult = 'none';
}
?>
<tr>
<td style='width:150px;border:1px solid black;'>
<p><?php echo $row['id']?></p>
</td>
<td style='width:150px;border:1px solid black;'>
<button style="display:<?php echo $displayJoin ?>" id="joinBattle" onclick="joinBattle(<?php echo htmlspecialchars($row['id']) ?>)">joinbattle</button>
<button style="display:<?php echo $displayResult ?>" id="checkResult" onclick="checkResult(<?php echo htmlspecialchars($row['result']) ?>)">result</button>
</td>
</tr>
<?PHP
}
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn = null;
?>
</body>
</html>
结果和Mysql
MySQL第1部分
MySQL第2部分
结果
查看源
您从未将$displayJoin
设置为block
。使用此:
if ($row['participation'] == 'joined') {
$displayJoin = 'none';
$displayResult = 'block';
} else {
$displayJoin = 'block';
$displayResult = 'none';
}