Hello everyone,

I have a table that i want to limit to 10 results per page and have a simple pagination script to generate the next 10 results. I have tried many different options, but alas it does not work. here is what I have so far.

<tbody>

                        <?
                        //specify how many results to display per page
                        $limit = 10;

                        //get pagination
                        $s = mysql_real_escape_string($_REQUEST['s']);

                        $results = mysql_query("SELECT * FROM products ORDER BY id LIMIT $s,$limit");
                        $row_num_links_main =mysql_num_rows ($results);
                            $n=0;
                            while($row = mysql_fetch_array($results)){

                            ?>
                        <tr>
                            <td><? echo $n=$n+1 ?></td>
                            <td><? echo $row['itemnumber']?></td>
                            <td><a href="?editproduct=<? echo $row['id'] ?>"><? echo $row['productname'] ?></a></td>
                            <td>$<? echo $row['price'] ?></td>
                            <td><? $taxable = $row['taxable'];
                            if($taxable == 1){
                                $taxable = 'Yes';
                            }else{
                                $taxable = '';
                            }  echo $taxable ?></td>
                            <td><a id="commentDelete" onClick="return delTicket(this.id);" href="?del=<? echo $row['id'] ?>">Delete</a></td>
                       </tr>
                        <? } ?>

                     </tbody>
                     <?
                         if($row_num_links_main > $limit){
    // next we need to do the links to other search result pages
        if ($s >=1) { // do not display previous link if 's' is '0'
            $prevs=($s-$limit);
            echo '<div class="search_previous"><a href="'.$PHP_SELF.'?s='.$prevs.'&q='.$var.'">Previous</a>
            </div>';
        }
    // check to see if last page
        $slimit =$s+$limit;
        if (!($slimit >= $row_num_links_main) && $row_num_links_main!=1) {
            // not last page so display next link
            $n=$s+$limit;
            echo '<div  class="search_next"><a href="'.$PHP_SELF.'?s='.$n.'&q='.$var.'">Next</a>
            </div>';
        }
    }
                     ?>

Any ideas on how to get this to work?

                        <?
                        $start = 0;
$per_page = 10;
if(!isset($_GET['page']))
{

$page = 1;
}else
{
$page = $_GET['page'];
}
if($page<=1)
$start = 0;
else
$start = $page * $per_page - $per_page;

                        $results = mysql_query("SELECT * FROM products ORDER BY id LIMIT $start, $per_page");
                            $n=0;
                            $num_rows = mysql_num_rows($results);
                            $num_pages = $num_rows / $per_page;


                            while($row = mysql_fetch_array($results)){
                            ?>
                        <tr>
                            <td><? echo $n=$n+1 ?></td>
                            <td><? echo $row['itemnumber']?></td>
                            <td><a href="?editproduct=<? echo $row['id'] ?>"><? echo $row['productname'] ?></a></td>
                            <td>$<? echo $row['price'] ?></td>
                            <td><? $taxable = $row['taxable'];
                            if($taxable == 1){
                                $taxable = 'Yes';
                            }else{
                                $taxable = '';
                            }  echo $taxable ?></td>
                            <td><a id="commentDelete" onClick="return delTicket(this.id);" href="?del=<? echo $row['id'] ?>">Delete</a></td>
                       </tr>
                        <? } ?>

                     </tbody>
                     </table>
                     <div>
                     <?php

####### Math OF +1 and -1 for the page ########

$prev = $page - 1;
$next = $page + 1;

echo"<hr>";
if($prev == 0){
    echo'';
}else{
echo "<a href='?page=$prev'>Prevous Page</a> | ";
}
     echo "<a href='?page=$next'>Next Page</a>";
?>
                     </div>

Fixed and solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.