Put pagination recordset inside a function
Hi, I need some help if its possible.
I'm using Dreamweaver's PHP code for selecting data from database and outputing them in pages ( Pagination ). I thought to create a very general record set and then put it in a function so I can re-use it, and change only things like the query and the $maxRows_. So it looks like this:
<?php
$maxRows_sql = 10; // per page
$pageNum_sql = 0;
if (isset($_GET['pageNum_sql'])) {
$pageNum_sql = $_GET['pageNum_sql'];
}
$startRow_sql = $pageNum_sql * $maxRows_sql;
$query_sql = "SELECT * FROM `database_table` ";
$query_limit_sql = sprintf("%s LIMIT %d, %d", $query_sql, $startRow_sql, $maxRows_sql);
$sql = mysqli_query($connection, $query_limit_sql) or die(mysqli_error());
$row_sql = mysqli_fetch_assoc($sql);
if (isset($_GET['totalRows_sql'])) {
$totalRows_sql = $_GET['totalRows_sql'];
} else {
$all_sql = mysqli_query($connection, $query_sql);
$totalRows_sql = mysqli_num_rows($all_sql);
}
$totalPages_sql = ceil($totalRows_sql/$maxRows_sql)-1;
$queryString_sql = "";
if (!empty($_SERVER['QUERY_STRING'])) {
$params = explode("&", $_SERVER['QUERY_STRING']);
$newParams = array();
foreach ($params as $param) {
if (stristr($param, "pageNum_sql") == false &&
stristr($param, "totalRows_sql") == false) {
array_push($newParams, $param);
}
}
if (count($newParams) != 0) {
$queryString_sql = "&" . htmlentities(implode("&", $newParams));
}
}
$queryString_sql = sprintf("&totalRows_sql=%d%s", $totalRows_sql, $queryString_sql);
?>
<html>
.....
<?php do { ?>
<tr>
<td><?php echo $row_sql['id']; ?></td>
<td><?php echo $row_sql['name']; ?></td>
</tr>
<?php } while ($row_sql = mysqli_fetch_assoc($sql)); ?>
.....
<a href="<?php printf("%s?pageNum_sql=%d%s", $currentPage, max(0, $pageNum_sql - 1), $queryString_sql); ?>" title="previous page">Previous</a>
<a href="<?php printf("%s?pageNum_sql=%d%s", $currentPage, min($totalPages_sql, $pageNum_sql + 1), $queryString_sql); ?>" title="next page">Next</a>
.....
</html>
How can I make this??? Imean if I put all the php code (above the html tags) inside a function, what should my function return back, how will the output data inside the while loop will appear properly and also the pagin links???
