php - 协调显示/隐藏 JQuery 脚本到 PHP

标签 php jquery html function button

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

相关文章:

php - PHP 文档不一致?

php - 如何在 PHP 中更改默认页面布局的内容

javascript - 如何从异步调用返回响应?

javascript - 厚盒插件 - 识别

php - 从 mysql 查询到 php 的空白列表下拉列表

javascript - 我的 Javascript 怎么搞砸了?

javascript - 需要对显示内容的 ajax 数据结果加载效果

php - 如何在 echo <td> 中应用样式?

html - 仅更改表格的一部分的背景图像

javascript - 通过 iFrame 将新的 Google 网站嵌入其他网页