2015-05-29 37 views
4

我目前有一些php腳本輸出查詢結果。我想在最後添加兩個按鈕來顯示/隱藏最後一個元素,但我不確定如何執行此操作。協調顯示/隱藏JQuery腳本到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

如果你得到的結果行數爲$ 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來,它的類股利= 「顯示隱藏」