sidevisning med PHP og SQL
Jeg har fundet følgende kode som kun viser 25 personer pr. sideI toppen af siden vises alle tilgængelige sidetal alt efter hvor mange resultater.
Eksempel: 1 2 3 4 5 6 7 8 9 10 osv.
Men jeg kunne godt tænkte mig det således:
Eksempel: 1 2 3 4 5 Næste
Hvis der klikkes på side 5:
Eksempel: Forrige 3 4 5 6 7 Næste
Har dette noget med funkttionen ceil at gøre?
$conn = sqlsrv_connect("localhost", array("Database"=>"Test", "UID"=>"test", "PWD"=>"test"));
/* Set the number of results to display on each page. */
$rowsPerPage = 25;
/* Define a query to get the number of rows on the server.
This query doesn't actually retrieve rows, it just
retrieves the number of rows that are available. */
$tsql = "SELECT COUNT(Id) FROM Persons";
/* Execute the query. */
$stmt = sqlsrv_query($conn, $tsql);
if($stmt === false)
{
echo "Error in query execution.";
die( print_r( sqlsrv_errors(), true));
}
/* Get the number of rows returned. */
$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
{
/* Display page links. */
$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> ");
}
else
{
print("<a href=$pageNum>$i</a> ");
}
}
echo "<br/><br/>";
}
/* Order target data by ID and select only items
(by row number) to display on a given page. */
$tsql = "SELECT * FROM
(SELECT ROW_NUMBER() OVER(ORDER BY Id)
AS RowNumber,
Firstname,
Surname
FROM Persons)
AS Temp
WHERE RowNumber BETWEEN ? AND ?";
/* Determine which row numbers to display. */
if(isset($_GET['pageNum']))
{
$highRowNum = $_GET['pageNum'] * $rowsPerPage;
$lowRowNum = $highRowNum - $rowsPerPage + 1;
}
else
{
$lowRowNum = 1;
$highRowNum = $rowsPerPage;
}
/* Set query parameter values. */
$params = array(&$lowRowNum, &$highRowNum);
/* Execute the query. */
$stmt2 = sqlsrv_query($conn, $tsql, $params);
if($stmt2 === false)
{
echo "Error in query execution.";
die( print_r( sqlsrv_errors(), true));
}
/* Print table header. */
print("<table border='1px'>
<tr>
<td>Row Number</td>
<td>Product Name</td>
<td>Product ID</td>
</tr>");
/* Display results. */
while($row = sqlsrv_fetch_array($stmt2) )
{
print("<tr>
<td>$row[0]</td>
<td>$row[1]</td>
<td>$row[2]</td>
</tr>");
}
/* Close table. */
print("</table>");
?>