0

我发现下面的代码每页只显示 25 个人。它根据结果显示页面顶部的所有页码。

Eksempel:1 2 3 4 5 6 7 8 9 10 osv。

但我喜欢这样: 示例:1 2 3 4 5 下一个

如果单击“5” 示例:Forrige 3 4 5 6 7 Næste

它与“ceil”功能有关吗?

$conn = sqlsrv_connect("localhost", array("Database"=>"Test", "UID"=>"test", "PWD"=>"test"));

$rowsPerPage = 25;

$tsql = "SELECT COUNT(Id) FROM Persons";

$stmt = sqlsrv_query($conn, $tsql);

$rowsReturned = sqlsrv_fetch_array($stmt);
if($rowsReturned === false)
{
echo "Error in retrieving number of rows.";
die( print_r( sqlsrv_errors(), true));
} 
elseif($rowsReturned[0] == 0)
{
echo "No rows returned.";
}
else
{    

$numOfPages = ceil($rowsReturned[0]/$rowsPerPage);
for($i = 1; $i<=$numOfPages; $i++)
{
    $pageNum = "?pageNum=$i";

    if ($_GET["pageNum"] == $i) 
    {
    print("<a href=$pageNum><b>$i</b></a>&nbsp;&nbsp;");
    }
    else
    {
    print("<a href=$pageNum>$i</a>&nbsp;&nbsp;");
    }
}
echo "<br/><br/>";
} 

$tsql = "SELECT * FROM 
        (SELECT ROW_NUMBER() OVER(ORDER BY Id) 
                    AS RowNumber, 
                        Firstname,    
                        Surname
    FROM Persons)
    AS Temp
    WHERE RowNumber BETWEEN ? AND ?";

if(isset($_GET['pageNum']))
{
$highRowNum = $_GET['pageNum'] * $rowsPerPage;
$lowRowNum = $highRowNum - $rowsPerPage + 1;
}
else
{
$lowRowNum = 1;
$highRowNum = $rowsPerPage;
}

$params = array(&$lowRowNum, &$highRowNum);

$stmt2 = sqlsrv_query($conn, $tsql, $params);
if($stmt2 === false) 
{
echo "Error in query execution.";
die( print_r( sqlsrv_errors(), true));
}

print("<table border='1px'>
    <tr>
        <td>Row Number</td>
        <td>Product Name</td>
        <td>Product ID</td>
    </tr>");

while($row = sqlsrv_fetch_array($stmt2) )
{
print("<tr>
        <td>$row[0]</td>
        <td>$row[1]</td>
        <td>$row[2]</td>
      </tr>");
 }

print("</table>");
4

0 回答 0