1

我有一个页面显示每页上的记录数。我在每页上显示 5 条记录,然后在下一页显示下 5 条记录,依此类推。分页工作正常,但问题出在第一页我在每条记录旁边显示数字序列,即从 1 到 5。然后在下一页它应该显示从 6 到 10 的数字,在下一页 11 到 15 等等. 但是每个页码从 1 到 5 开始。我的代码如下。我尝试了不同的策略,但没有任何效果。请检查代码并告诉我在哪里进行更改以使其正常工作。提前致谢。

<div class="grid_12">
    <div class="box first round fullpage mh500 grid">
        <h2><?php echo $resource->_pageHead; ?></h2>
        <?php  $resource->displayMessage(); ?>
        <?php   
            if($resource->_recordCount > 0)
            {
        ?>
            <div class="block">
                <table class="listing" >
                    <thead>
                        <tr>
                            <th width="50" class="bdr-left">Sr. No.</th>
                            <th width="60">Name</th>
                            <th width="60">Email</th>
                            <th width="60">Address</th>
                            <th width="60">City</th>
                            <th width="60">State</th>
                            <th width="60">Phone Number</th>
                            <th width="60">Country</th>
                            <th width="60">Comment</th>
                            <th width="60">Inquiry Date</th>
                            <th width="60" class="bdr-right">Action</th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php
                        $i = 1;
                        $_SESSION['number'] = $i;
                        $perpage = 5;
                        $q = mysql_query("SELECT * FROM $resource->_table");
                        $total_record = mysql_num_rows($q);
                        $pages = ceil($total_record/$perpage);
                        $page = (isset($_GET['page']))?$_GET['page']:1;
                        $start = ($page-1) * $perpage;
                        $result = mysql_query("SELECT * FROM $resource->_table LIMIT $start, $perpage");
                        while($res = mysql_fetch_array($result))
                        {
                    ?>      
                            <tr class="odd gradeX">
                                <td><?php echo $i; ?></td>
                                <td><?php echo $res['name']; ?></td>
                                <td><?php echo $res['email'];?></td>
                                <td><?php echo $res['address'];?></td>
                                <td><?php echo $res['city'];?></td>
                                <td><?php echo $res['state'];?></td>
                                <td><?php echo $res['p_code']."-".$res['p_num'];?></td>
                                <td><?php echo $res['country'];?></td>
                                <td><?php echo substr($res['comments'], 0, 100);echo "...";?></td>
                                <td><?php echo $res['inquiry_date'];?></td>
                                <td align="center">
                                    <a href="<?php echo $_SERVER['PHP_SELF'].'?action=delete&id='.$res['id'];?>" onclick="return confirm('Do you want to delete this record?');">
                                        <img src="img/cross.png" alt="Delete" title="Delete"/>
                                    </a>
                                </td>
                            </tr>
                    <?php
                            $i++;
                        }
            }
                    ?>
                    </tbody>
                </table>
            </div>
            <div id="paging" style="padding-left:500px;">
                <?php
                    $prev=$page-1;
                    $next=$page+1;
                    if($prev > 0)
                    {
                        echo "<a href='?page=$prev'>Prev</a>";
                    }
                    echo "&nbsp;&nbsp;";
                    if($pages >= 1 AND $page <= $pages)
                    {
                        for($x=1;$x<=$pages;$x++)
                        {
                            echo "&nbsp;&nbsp;";
                            echo ($x==$page) ?"<a href=?page=$x style=\"font-weight:normal;\">$x</a>":'<a href="?page='.$x.'" >'.$x.'</a>';
                        }
                        echo "&nbsp&nbsp";
                        if($page<$pages)
                        {
                            echo "<a href='?page=$next'>Next</a>";
                        }
                    }
                ?>
            </div>
    </div>
    <div class="clear"></div>
</div>
4

3 回答 3

3

希望下面的逻辑对你有帮助,初始化一个变量$i = 0;
通过下面的逻辑计算一条记录的起始编号,

 $page_num   =   (int) (!isset($_GET['page']) ? 1 : $_GET['page']);
 $start_num =((($page_num*$num_records_per_page)-$num_records_per_page)+1);

初始化一个变量$i = 0;

然后在循环内部计算序列号,例如,

$slNo = $i+$start_num;

然后回显$slNo;

于 2014-05-26T06:20:10.293 回答
1

而不是echo $i; 尝试这个

$j= (($page-1) * $perpage) + i; echo $j;
于 2014-07-17T12:24:09.353 回答
0
You can go to this website and there is a simple example of pagination
http://snipplr.com/view/55519/ 
hope this works for you.
于 2013-05-24T10:17:41.183 回答