函数中的php分页

函数中的php分页,php,mysql,pagination,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.en

我想在一张桌子上放一个页码。 我在一个名为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>

问题是如何将其转换为表中的分页?

以下示例将基于URL参数:

我们需要设置应该生成多少行。 在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;
    }
}
现在,只需在url中添加页面参数,如下所示:


?page=1

以下示例将基于URL参数:

我们需要设置应该生成多少行。 在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;
    }
}
现在,只需在url中添加页面参数,如下所示:

?第1页