【问题标题】:How to pagination [closed]如何分页[关闭]
【发布时间】:2016-01-12 21:29:56
【问题描述】:

如何在我的代码中添加分页。在表格中回显时,我不知道要为我的数据放置页面

<?php
 $sql_query="SELECT * FROM utility_readings WHERE readings_type='1'";
 $result_set=mysql_query($sql_query);
 while($row=mysql_fetch_row($result_set))
 {
?>
        <tr>
            <td><?php echo $row[1]; ?></td>
            <td><?php echo $row[2]; ?></td>
            <td><?php echo $row[3]; ?></td>
            <td width="20%"><?php echo $row[4]; ?></td>
            <td><a href="javascript:edit_id('<?php echo $row[0]; ?>')"><img src="../edit.png" align="center" /></a></td>
        </tr>
<?php
 }
 ?>

谢谢!

【问题讨论】:

  • 你必须在教程中学习它。有很多。

标签: php pagination


【解决方案1】:

我将解释分页背后的基本逻辑。

  1. 查找项目总数
  2. 您希望在一页上显示多少项
  3. 然后将总项目数除以每页项目数得到总页数
  4. 在 next 和 prev 按钮上调用函数以获取相关项目。如果您使用 MySQL 查询来获取数据。您可以在查询中使用 LIMIT 选项来获取所需的记录。

这些是基本步骤,但是,我建议您为此目的使用一些经过测试的脚本。否则,您需要处理所有可能对您来说非常复杂的情况。

【讨论】:

    【解决方案2】:

    像这样创建一个类:

    _conn = $conn; $this->_query = $query; $rs= $this->_conn->query( $this->_query ); $this->_total = $rs->num_rows; } } 并像这样检索它: _limit = $limit; $this->_page = $page; if ( $this->_limit == 'all' ) { $query = $this->_query; } 别的 { $query = $this->_query 。 “ 限制 ” 。 ( ( ( $this->_page - 1 ) * $this->_limit ) 。 ", $this->_limit"; } $rs = $this->_conn->query( $query ); 而 ( $row = $rs->fetch_assoc() ) { $结果[] = $行; } $result = 新的 stdClass(); $result->page = $this->_page; $result->limit = $this->_limit; $result->total = $this->_total; $结果->数据= $结果; 返回$结果; } 使用这个很棒的教程来充分理解如何使用 PHP 进行分页: http://code.tutsplus.com/tutorials/how-to-paginate-data-with-php--net-2928 或者以非 OOP 方式进行: ”。 “EMP 名称:{$row['emp_name']}
    ”。 EMP 工资:{$row['emp_salary']}
    “。 "--------------------------------
    "; } 如果($页面> 0) { $last = $page - 2; echo "最近 10 条记录 |"; echo "接下来的 10 条记录"; } 否则 if( $page == 0 ) { echo "接下来的 10 条记录"; } else if( $left_rec 最后 10 条记录"; } mysql_close($conn); ?>

    【讨论】:

    • 谢谢,但我需要普通代码(没有“-->”)。我不喜欢它。
    猜你喜欢
    • 2011-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-21
    • 2012-08-28
    • 2015-02-16
    相关资源
    最近更新 更多