将Show/Hide JQuery脚本与PHP整合

4

我目前有一些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>

有关如何完成此任务,有何建议?
1个回答

1
如果您使用 $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。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接