Php 同一页面中的多个帖子部分
从这样的数据库Php 同一页面中的多个帖子部分,php,html,css,Php,Html,Css,从这样的数据库 [ POSTS ] | id | title | part1 | part2 | part3 | |------|--------------|-------------|-------------|-------------| | 1 | ST1 | storyp1 | storyp1 | stor
[ POSTS ]
| id | title | part1 | part2 | part3 |
|------|--------------|-------------|-------------|-------------|
| 1 | ST1 | storyp1 | storyp1 | storyp1 |
| 2 | ST2 | storyp2 | storyp2 | storyp2 |
| 3 | ST3 | storyp3 | storyp3 | storyp3 |
| 4 | ST4 | storyp4 | storyp4 | storyp4 |
__________________________________________
单击按钮时,如何将页面分为3部分显示
<?php
$query = "SELECT * FROM posts WHERE id = :id";
$stmt = $conn->prepare($sqlFetch);
$stmt->execute([':id' => $id]);
$row = $stmt->fetch()
$title = $row['title'];
$part1 = $row['part1'];
$part1 = $row['part2'];
$part1 = $row['part3'];
?>
<body>
<h1 class="title">
<?php echo $title; ?>
</h1>
<button>part1</button>
<button>part2</button>
<button>part3</button>
<div class="part1">
<?php echo $part1; ?>
</div>
<div class="part2">
<?php echo $part2; ?>
</div>
<div class="part3">
<?php echo $part3; ?>
</div>
</body>
第一部分
第二部分
第三部分
我考虑过使用CSS
display:none
但是不知道如何触发按钮,有没有一种可能的方法可以使用PHP来实现这一点?我也考虑过单选按钮,但我想这不是最好的方法。我想这会帮助你:
先创建GetData.php
<?php
$q = ($_GET['q']);
include_once 'DbConnection.php'; //This is your connection
$Query = "SELECT * FROM posts WHERE id='$q'";
$Result = mysqli_query($Link, $Query);
while ($Rows = mysqli_fetch_assoc($Result))
{
$title = $Rows['title'];
$part1 = $Rows['part1'];
$part2 = $Rows['part2'];
$part3 = $Rows['part3'];
echo "<div class='part1'>$part1</div>";
echo "<div class='part2'>$part2</div>";
echo "<div class='part3'>$part3</div>";
}
?>
<html>
<head><title>Show the result</title>
<script>
function ShowResult(str)
{
if (str == "") {
return;
}
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
} else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("DisplayData").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","GetData.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<div>
<input type="text" id="txtSearch" onkeyup="ShowResult(this.value); onblur="ShowResult(this.value);"/>
<div>
<div id="DisplayData"></div>
</body>
</html>
使用JS和onclick查找压力机,并采取相应的行动(查找内容并隐藏/显示)@LunarWatcher这是不可能使用PHP的吗?不。PHP是服务器端语言,而JavaScript是客户端语言。按钮按下是客户端的。看见