我在网站上工作,正在从数据库表 (category_names) 中提取类别名称。然后我在网站上使用 php 将它们显示在 html 无序列表上。我想限制 category_names 的数量,然后使用 jquery(或其他任何东西,但我更喜欢 jQuery)检索更多 category_names 并添加一个 less 按钮返回。
我希望我让这个问题容易理解,感谢您的帮助!
最佳答案
基本上使用 AJAX 来获取更多内容。首先使用 LIMIT
加载一些。
<ul id="categories">
<?php
$res = mysql_query('SELECT * FROM `category_names` LIMIT 10'); // change limit to suit
while ($row = mysql_fetch_array($res)) {
echo '<li>'.$row['name'].'</li>'; // or whatever your field is called
}
?>
</ul>
<span id="loadmore" num_loaded="10">Load More</span>
然后使用下面的jQuery加载更多:
$('#loadmore').click(function() {
var loaded = $(this).attr('num_loaded');
$.ajax({
url:'load_categories.php',
type:'get',
data:{'from':loaded,'to':loaded+10},
success: function (res) {
var categories = $.parseJSON(res);
categories.each(function() {
$('#categories').append('<ul>'+this+'</ul>');
});
$('#loadmore').attr('num_loaded',loaded+10);
}
});
});
最后,您需要创建 AJAX 调用的 PHP 页面 - load_categories.php
<?php
if (!isset($_GET['from'])) exit;
if (!isset($_GET['to'])) exit;
$from = $_GET['from'];
$to = $_GET['to'];
$diff = $from-$to;
// connect / select db
$res = mysql_query('SELECT * FROM `category_names` LIMIT '.$from-1.','.$to.';');
$arr = array();
while ($row = mysql_fetch_array($res)) {
array_push($arr,$row['name']);
}
echo json_encode($arr);
?>
关于php - 单击 PHP、JQUERY、MYSQL 从数据库中显示更多信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8608297/