我想显示一定数量的结果(例如 5)并制作:
<a href="" onclick="<?php ShowNextResult(); ?>">
并使用 onlick 显示接下来的 5 个结果。
最佳答案
编辑:: HTML
<div id="results">
<div class="result"></div>
<div class="result"></div>
<div class="result"></div>
</div>
<a href="#" id="showMore" />Show more</a>
JavaScript 使用Jquery如下
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$('#showMore').click(function(event) {
event.preventDefault();
$number = $('.result').size();
$.ajax({
type: "POST",
url: "getNext.php",
data: "count=$number",
success: function(results){
$('#results').append(results);
}
});
});
});
</script>
PHP
您应该创建一个新的 php 页面(getNext.php)来获取查询结果
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM Persons LIMIT {$_POST['count']},5");
while($row = mysql_fetch_array($result))
{
echo "<div class='result'>".$row['FirstName'] . " " . $row['LastName']."</div>";
}
mysql_close($con);
?>
帮助
你可以使用类似的 SQL
SELECT x,xx,xxx FROM XxXxXs Limit $_POST['count'],5
关于php - 如何在 JavaScript 中显示 MySQL 中 "onclick"的下一个结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3681407/