【问题标题】:php pagination in functionphp分页功能
【发布时间】:2018-10-03 00:50:00
【问题描述】:

我试图在表格中放置一个分页。 我在一个名为 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>

问题是如何将其转换为在表格中进行分页?

【问题讨论】:

    标签: php mysql pagination


    【解决方案1】:

    以下示例将基于 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-18
      • 1970-01-01
      相关资源
      最近更新 更多