Question

I had made a page to fetch data from mysql database. I want to show only 10 result per page and other result using next button and previous result using previous button.

I used the following code to fetch data:

$item=$_POST('item');
$query=mysql_query("select name,address,contact from info_tb where keyword='$item'");

while($row=mysql_fetch_array('$query'))
{
echo "Name : row[0]":
echo "Address : row[1]":
echo "Contact : row[2]":
}
Was it helpful?

Solution

Make use of the LIMIT keyword on your mySQL Query.

SELECT * FROM `your_table` LIMIT 0, 10 

Seems like you want to navigate records, so ..

 SELECT * FROM `your_table` LIMIT 5, 10 

These records will be displayed 6, 7, 8, 9, 10, 11, 12, 13, 14, 15

If you want to navigate you need to control this paramter as illustrated.

SELECT * FROM `your_table` LIMIT 5, 10
                                 ^  

Seems like you posted code lately, Try something like this on your code.

$item=$_POST['item'];
$page=$_POST['pno'];
$query=mysql_query("select name,address,contact from info_tb where keyword='$item' limit '$page',10");

OTHER TIPS

For your first page you will have to retrieve from your database your first 10 results, the 1, 2, 3 ... 9, 10 rows:

SELECT * FROM `table` LIMIT 0, 10

and print out your results. If, let's say the user is on page 4 you will have to retrieve from 31, 32, 33.. 39, 40 rows. Calculate your offset and row_count in a server script like PHP:

$page_no = 4;
$row_count = 10; 
$offset = ($page_no - 1) * $row_count; // results 30

Now your SQL will be:

SELECT * FROM `table` LIMIT 30, 10;

so you will have the 31, 32, 33... rows that you can print out. The page number can be retrieved via GET or POST parameters.

You could of course print out the whole result and use jQuery to paginate through it:

<!-- Download jquery and link locally.  Directlinking to jQuery should only be used for testing purposes. -->
<script type='text/javascript' src='http://code.jquery.com/jquery-git2.js'></script>
<script type="text/javascript">
    var pageno = 0;
    var maxpageno = 0;
    function show(){
        if (pageno < 0) { pageno = 0}
        if (pageno > maxpageno) { pageno = maxpageno}
        $('#results tbody tr').hide();
        $('#results tbody tr.page'+pageno).show();
    }
</script>
<body onload="show();">
<?php
    $sql = "SELECT * FROM books";   
    //result is boolean for query other than SELECT, SHOW, DESCRIBE and EXPLAIN
    $result = $short_connect->query($sql);
    if (($result) && ($result->num_rows > 0))
    {
        $results = array();
        print "<table id=\"results\">\n<thead>\n<tr><th>id</th><th>title</th><th>isbn</th><th>ean</th><th>year</th></tr>\n
        <tr><td colspan=5>
        <a href=\"#null\" onclick=\"pageno = 0; show();\">&lt;&lt;</a>
        <a href=\"#null\" onclick=\"pageno--; show();\">&lt;</a>
        <a href=\"#null\" onclick=\"pageno++; show();\">&gt;</a>
        <a href=\"#null\" onclick=\"pageno = maxpageno; show();\">&gt;&gt;</a>
        </th></tr>\n
        </thead>\n<tbody>\n";  //table starter and header
        $i = 0;                       //start count
        $ipp = 10;                        //items per page
        while ($row = $result->fetch_array())
        {
            $rop = $i % $ipp;           //result on page
            $pno = floor($i / $ipp);        //page number
            $i++;                         //increase count
            print "<tr class=\"page$pno\"><td>$row[0]</td><td>$row[1]</td><td>$row[2]</td><td>$row[3]</td><td>$row[4]</td></tr>\n";
        }
        print "</tbody>\n<tfoot><tr><td colspan=5>
        <a href=\"#null\" onclick=\"pageno = 0; show();\">&lt;&lt;</a>
        <a href=\"#null\" onclick=\"pageno--; show();\">&lt;</a>
        <a href=\"#null\" onclick=\"pageno++; show();\">&gt;</a>
        <a href=\"#null\" onclick=\"pageno = maxpageno; show();\">&gt;&gt;</a>
        </td></tr>\n</tfoot>\n</table>\n"; // footer
        $result->free();
    }
    $short_connect->close();
    print "<script type=\"text/javascript\"> maxpageno = $pno;</script>";
?>
</body>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top