gpt4 book ai didi

函数中的 php 分页

转载 作者:行者123 更新时间:2023-11-30 22:14:53 25 4
gpt4 key购买 nike

我正在尝试将分页放入表格中。我在一个名为 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 参数:

  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;
    }
    }

现在在你的 url 中添加 page 参数如下:

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

关于函数中的 php 分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38754046/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com