最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Display more from database on click PHP, JQUERY, MYSQL - Stack Overflow

matteradmin13PV0评论

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 badges
Add a comment  | 

4 Answers 4

Reset to default 12

Basically 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

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far