简体   繁体   中英

Display the search results 5 at a time

I have a page as below to search for words. I want to show 5 results at a time. Then when I hit the show more button, get another 5, and so on so forth.

1-I don't quite know how to include the $_POST['search'] in the javascript.

2-The results I get are sometimes duplicate, which means my "offset" and "limit" definitions are not accurate.

Could you please help me?

<script src="jquery-1.8.3.js" language="javascript" type="application/javascript"></script>

<script>
    $('#showMore').live('click',function(){
        var offset = $('#displayData tr').length;
        var limit  = offset; 
        offset = offset+limit; 
        var search = "?????"; // I don't know how to implemet the search word 
                                   //  in here

     $.ajax({
        url:'getResults.php',
        type:'POST',
        data: {offset: offset, search: search},
        dataType:"text",
        success: function(returnData){
                                $('#displayData').append(returnData);

        }
        })
        })
 </script>


    $search=$_POST["search"];   

    $query2= mysql_query("SELECT *
                FROM table 
                limit 0,5")
    or die(mysql_error());

    ...

<input type="button" id="showMore" value="Show More" />

and the Getresults.php (partially)

$offset = $_POST['offset'];
$search = $_POST['search'];

$query2= mysql_query("SELECT *
            FROM table
            limit $offset,5")
or die(mysql_error());

I would recommend using an "ORDER BY" clause in your MySQL. That should help keep your results from duplicating when using a limit. As an example of how to switch your code to MySQLi, it's pretty straightforward.

$link = mysqli_connect('DATABASE HOST', 'USERNAME', 'PASSWORD', 'DATABASE');

$query = "SELECT * FROM table ORDER BY id limit $offset, 5";
$results = mysqli_query($link, $query) or die('MySQL Error');

while ($results = mysqli_fetch_array($results)) {
    print $results['Answer'];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM