Search code examples
phppagination

Php paging - How to continue numbering in following pages


In my pagination code, I list 5 buildings per page and number them from 1 to 5,

<?php
$page = intval($_GET['page']);
if(!$page) $page = 1;
$totalno=mysql_num_rows(mysql_query("SELECT M1_ID FROM M1Buildings WHERE M1_Cat2 BETWEEN 3 AND 4 && M1_Status=5"));
$limit = 5;
$show = $page * $limit - $limit;
$buildinglist = mysql_query("SELECT * FROM M1Buildings  WHERE M1_Cat2 BETWEEN 3 AND 4 && M1_Status=5 ORDER BY M1_Height DESC LIMIT $show,$limit",$con);
$firstrowno=1;//This will be 1 for page 1, 6 for page 2, 11 for page 3 etc..
?>

Some html code here, then:

<?php
$startno=1;//This is the number of each row, starting from 1
while ($rowblist = mysql_fetch_array ($buildinglist))
{
echo '<tr>
        <td>'.$startno.'</td>
<td>Some information here</td>
<td>Some information here</td>
    </tr>';
    $startno++;
    $firstrowno+5;
}
?>

There is no problem in first page, but when I pass to page 2, it again starts from 1, but I want it starts from 6, I couldn't get how can I achieve it. I've performed a search, but no result. Actually with my limited english, I can't know how to search it with proper words, I guess. Thanks for your understanding!


Solution

  • You need to update your query to use offset. (** Sorry, didn't notice you were using the shorthand **)

    Took a moment for me to get the gist of the question. Use: $startno = $show + 1;

    instead of: $firstrowno= 1;

    After the HTML:

    <?php
    while ($rowblist = mysql_fetch_array ($buildinglist))
    {
    echo '<tr>
            <td>'.$startno.'</td>
    <td>Some information here</td>
    <td>Some information here</td>
        </tr>';
        $startno++;
    }
    ?>
    

    The variables do not retain their state between trips to the server. (Unless you store them and retrieve them from sessions.