admin管理员组文章数量:1323224
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.
I hope I made this question easy to understand, and thank you for any help!
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.
I hope I made this question easy to understand, and thank you for any help!
Share Improve this question asked Dec 22, 2011 at 18:32 the_the_ 1,1733 gold badges30 silver badges62 bronze badges4 Answers
Reset to default 12Basically use AJAX to pull in more. Start off by loading in a few by using 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:
$('#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
<?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);
?>
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).
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
This is similar to: PHP/MySQL Show first X results, hide the rest
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!
Use a datagrid with pagenation. I think datatable JQuery pluggin will work well for you
本文标签: javascriptDisplay more from database on click PHPjQueryMYSQLStack Overflow
版权声明:本文标题:javascript - Display more from database on click PHP, JQUERY, MYSQL - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739281056a2156242.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论