php功能分页

时间:2016-08-03 21:37:00

标签: php mysql pagination

我试图在表格中加入一个分页。 我在一个名为Login的类中有这个函数,它回显了表的一部分:

public function getCodes()
{
    // if database connection opened
    if ($this->databaseConnection()) {
        $query_codes = $this->db_connection->prepare('SELECT i.id, i.code, i.active, p.enterprise, (SELECT count(*) FROM internal) AS total FROM internal AS i LEFT JOIN pusers AS p ON i.user_id = p.user_id');
        $query_codes->execute();
        // get result row (as an object)
        //$result_row = $query_products->fetchObject();
        while ($result_row = $query_codes->fetchObject()){
            $this->totalPages = $result_row->total;
            echo'<tr>'; // printing table row
            echo '<td>'.$result_row->code.'</td><td>'.$result_row->active.'</td><td>'.$result_row->enterprise.'</td>
            <td>
                <form method="post" action="codes">
                    <input type="hidden" name="id" value='.$result_row->id.'>
                    <button type="submit" id="register-submit-btn" class="button" name="delete_code" onclick="return confirm(\'Quieres borrar el Código?\')">Borrar</button>
                    <button type="submit" id="register-submit-btn" class="button" name="activate_code" onclick="return confirm(\'Quieres activar el Código?\')">Activar</button>
                </form>
            </td>'; // we are looping all data to be printed till last row in the table
            echo'</tr>'; // closing table row
        }
    } else {
        return false;
    }
}

在另一个文件中我称之为:

<div class="table-style">
            <table class="table-list">
                <tbody><tr>
                    <th>Código</th>
                    <th>Activo</th>
                    <th>Empresa</th>
                    <th>Acciones</th>
                </tr>
                <?php $login->getCodes();?>
            </tbody></table>
 </div>

问题是我怎样才能将其转化为表格中的分页?

1 个答案:

答案 0 :(得分:0)

以下示例将基于URL参数:

  1. 我们需要设置应生成多少行。
  2. 在URL中,我们将使用“ page”参数来确定将生成哪个表页面。

    function getCodes(){
    
        $maxRows = 10;
        $offset = isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page'] > 0 ? $_GET['page'] : 0;
    
        if ($this->databaseConnection()) {
            $query_codes = $this->db_connection->prepare('SELECT i.id, i.code, i.active, p.enterprise, (SELECT count(*) FROM internal) AS total FROM internal AS i LEFT JOIN pusers AS p ON i.user_id = p.user_id' . ' LIMIT '.$maxRows.' OFFSET '. ($offset * $maxRows));
            $query_codes->execute();
            // get result row (as an object)
            //$result_row = $query_products->fetchObject();
            while ($result_row = $query_codes->fetchObject()){
                $this->totalPages = $result_row->total;
                echo'<tr>'; // printing table row
                echo '<td>'.$result_row->code.'</td><td>'.$result_row->active.'</td><td>'.$result_row->enterprise.'</td>
                <td>
                    <form method="post" action="codes">
                        <input type="hidden" name="id" value='.$result_row->id.'>
                        <button type="submit" id="register-submit-btn" class="button" name="delete_code" onclick="return confirm(\'Quieres borrar el Código?\')">Borrar</button>
                        <button type="submit" id="register-submit-btn" class="button" name="activate_code" onclick="return confirm(\'Quieres activar el Código?\')">Activar</button>
                    </form>
                </td>'; // we are looping all data to be printed till last row in the table
                echo'</tr>'; // closing table row
            }
        } else {
            return false;
        }
    }
    

现在在您的网址中,只需添加page参数,如下所示:

http://localhost/you-page-with-table/ ?page = 1