javascript 单击 PHP、JQUERY、MYSQL 时从数据库中显示更多信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8608297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display more from database on click PHP, JQUERY, MYSQL
提问by the_
I'm working on a website and am pulling category names from a db table (category_names). I then display them on the website using php on to an html unordered list. I want to limit the number of category_names and then using jquery(or anything else but I prefer jQuery) retrieve more category_names and add a less button to go back.
我在一个网站上工作,正在从数据库表 (category_names) 中提取类别名称。然后我使用 php 在网站上将它们显示在一个 html 无序列表上。我想限制 category_names 的数量,然后使用 jquery(或其他任何东西,但我更喜欢 jQuery)检索更多 category_names 并添加一个 less 按钮返回。
I hope I made this question easy to understand, and thank you for any help!
我希望我使这个问题易于理解,并感谢您的帮助!
回答by Alex Coplan
Basically use AJAX to pull in more. Start off by loading in a few by using LIMIT
.
基本上使用 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>
Then use the following jQuery to load more:
然后使用以下 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);
}
});
});
Finally you'll need to create the PHP page that the AJAX calls - load_categories.php
最后,您需要创建 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);
?>
回答by Cory Dolphin
There are a number of different approaches that work better or worse depending upon your needs.
根据您的需要,有许多不同的方法可以更好或更糟地工作。
Approach 1 (simpler, less efficient, scales poorly): execute the full query, and store all of the results on the DOM, just hiding them using jQuery (jQuery expander is a simple plugin you may want to try out, though I have found it limiting in customization).
方法 1(更简单、效率更低、扩展性差):执行完整查询,并将所有结果存储在 DOM 上,仅使用 jQuery 隐藏它们(jQuery 扩展器是一个您可能想要尝试的简单插件,但我发现它限制了定制)。
Approach 2 (more complicated, but more efficient/scalable, also faster): Use MySQL limit, you can actually send a second mysql request on click, however, you would want to make sure this is asynchronous so as to not delay the user's interactions. http://php.about.com/od/mysqlcommands/g/Limit_sql.htm
方法 2(更复杂,但更高效/可扩展,也更快):使用 MySQL 限制,您实际上可以在单击时发送第二个 mysql 请求,但是,您需要确保这是异步的,以免延迟用户的交互. http://php.about.com/od/mysqlcommands/g/Limit_sql.htm
This is similar to: PHP/MySQL Show first X results, hide the rest
回答by Newbie Coder
Or you could do something simpler:
或者你可以做一些更简单的事情:
$get = mysqli_queury ($link, "SELECT * FROM db_name WHERE blank='blank' LIMIT 5"
if (isset($_POST['button']; {
$get = mysqli_query ($link, "SELECT * FROM db_name WHERE blank='blank' LIMIT 10"
}
?>
<form action="showmore.php" method="POSt">
<input type="button" id="button" name="button">
</form>
Hope that helps!
希望有帮助!
回答by Emmanuel N
Use a datagrid with pagenation. I think datatable JQuery plugginwill work well for you
使用带分页的数据网格。我认为数据表JQuery 插件很适合你