【问题标题】:PHP SQLSRV Pagination function returns one rowPHP SQLSRV 分页函数返回一行
【发布时间】:2020-10-10 00:47:56
【问题描述】:

我的分页无法正常工作。我得到了页码,但每页只有一行,而应该有 10 行。我是 php 新手,正在修改我在这个网站上找到的代码。当我加载页面时,我可以通过 id 号告诉我显示的结果是我期望查询中的第 10 行......第 1-9 行丢失了。

<?php
//Function to return rows for each page
function getPage($stmt, $pageNum, $rowsPerPage)
  {
  $offset = ($pageNum - 1) * $rowsPerPage;
  $rows = array();
  $i = 0;
  while(($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_NUMERIC,SQLSRV_SCROLL_ABSOLUTE,$offset + $i)) && $i < $rowsPerPage)
  {
      array_push($rows, $row);
      $i++;
  }
  return $rows;
  }

?>

          <?php
          // Set the number of rows to be returned on a page.
          $rowsPerPage = 10;
          $usr = $_SESSION['user'];
          if ($_SESSION['admin']="YES") {
            $query = "SELECT iID,FirstName,LastName, convert(varchar, SubmitDate, 101) as SubDate,LEFT(ImproveIdea,30) as MyIdea,Status FROM Idea where Status='Pending' ORDER BY iID desc";
          }else {
            $query = "SELECT iID,FirstName,LastName,convert(varchar, SubmitDate, 101) as SubDate,LEFT(ImproveIdea,30) as MyIdea,Status FROM Idea where SubmitBy='".$usr."' ORDER BY iID desc";
          }
            $stmt = sqlsrv_query($connect,$query, array(), array( "Scrollable" => 'static' ));

          if ( !$stmt )
          die( print_r( sqlsrv_errors(), true));
          ?>

 <table class="table table-striped table-bordered">
    <thead class="thead-light">
      <tr>
        <th width="54%" scope="col">Submitted By</th>
        <th width="14%" scope="col">Date Submitted</th>
        <th width="13%" scope="col">Status</th>
        <th width="19%" scope="col">Actions</th>
      </tr>
    </thead>
    <tbody>
      <?php
        $pageNum = isset($_GET['pageNum']) ? $_GET['pageNum'] : 1;
        $page = getPage($stmt, $pageNum, $rowsPerPage);
           foreach($page as $row)
               $ideanum = $row[0];
               $fname = $row[1];
               $lname = $row[2];
               $submitdate = $row[3];
               $idea = $row[4]; 
               $status = $row[5];
               echo '<tr>';
               echo '<td>'.$ideanum.' '.$fname.' '.$lname.' - '.$idea.'...</td>';
               echo '<td>'.$submitdate.'</td>';
               echo '<td>'.$status.'</td>';
               echo '<td><button type="button" class="btn btn-success"><i class="la la-eye"></i></button><button type="button" class="btn btn-info"><i class="la la-edit"></i></button></td>';
               echo "</tr>";
?>
<?php
// Get the total number of rows returned by the query.
// Display links to "pages" of rows.
$rowsReturned = sqlsrv_num_rows($stmt);
if($rowsReturned === false)
die( print_r( sqlsrv_errors(), true));
elseif($rowsReturned == 0)
{
echo "No rows returned.";
exit();
}
else
{               
// Display page links.
$numOfPages = ceil($rowsReturned/$rowsPerPage);
for($i = 1; $i<=$numOfPages; $i++)
{
    $pageLink = "?pageNum=$i";
    print("<a href=$pageLink>$i</a>&nbsp;&nbsp;");
}
echo "<br/><br/>";
} 
            ?>
        </tbody>
      </table>

【问题讨论】:

  • 我无法重现此行为,代码似乎正确。但是,请考虑以下几点:1) 始终在语句中使用参数;2) 使用 ORDER BY OFFSET ... FETCH ... 实现分页。
  • 我是 php 新手,所以我很欣赏 Zhorov 的提示。原来我的 foreach 语句中缺少该问题。

标签: php pagination sqlsrv


【解决方案1】:

已解决:问题是我的 foreach 语句中缺少括号,正如 redditor 向我指出的那样。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多