我目前有一些 php 脚本可以输出查询结果。我想在末尾添加两个按钮来显示/隐藏最后一个元素,但我不确定该怎么做。
以下是我的php脚本:
while($result = mysqli_fetch_array($iname))
{
echo "<b>Event Name:</b> " .$result['EventName'];
echo "<br> ";
echo "<b>Location:</b> ".$result['Location'];
echo "<br>";
//this is where I would like to add my two buttons, that would show the "hidden" content when clicked
这是我用 HTML 脚本编写的内容,我想将其调和到 PHP 输出中:
<!DOCTYPE html>
<html>
<head>
<scriptsrc="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js></script>
<script>
$(document).ready(function(){
$("#hidden").hide();
$("#hide").click(function(){
$("#hidden").hide(500);
});
$("#show").click(function(){
$("#hidden").show(500);
});
});
</script>
</head>
<body>
<button id="show">Show</button>
<button id="hide">Hide</button>
<p id="hidden">
Some Random Text that will be shown when the buttons are clicked
</p>
</body>
</html>
关于如何完成此操作有什么建议吗?
最佳答案
如果使用 $num_rows = mysql_num_rows($result); 获取结果行数怎么样? 然后,在循环中放置一个计数器。
$counter = 1;
$theClass="";
while($result = mysqli_fetch_array($iname))
{
if ($counter == mysql_num_rows($result);){
$theClass="showHide";
}
echo "<div class='$theClass'>";
echo "<b>Event Name:</b> " .$result['EventName'];
echo "<br> ";
echo "<b>Location:</b> ".$result['Location'];
echo "<br>";
echo "</div>
$counter++;
}
然后,将您的 javascript 应用于 class="showHide"的 div
关于php - 协调显示/隐藏 JQuery 脚本到 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30532120/