What is the purpose of the createPages function in the PHP code provided?
The purpose of the createPages function in the provided PHP code is to generate a specified number of page links based on the total number of items and items per page. This function helps in creating pagination for a list of items displayed on a web page.
function createPages($totalItems, $itemsPerPage) {
$totalPages = ceil($totalItems / $itemsPerPage);
$pages = [];
for ($i = 1; $i <= $totalPages; $i++) {
$pages[] = $i;
}
return $pages;
}
// Example usage
$totalItems = 100;
$itemsPerPage = 10;
$pages = createPages($totalItems, $itemsPerPage);
foreach ($pages as $page) {
echo "<a href='page.php?page=$page'>$page</a>";
}
Keywords
Related Questions
- How can the use of LIKE in SQL queries with PDO prepared statements be optimized to handle fuzzy searches for multiple results?
- Are there specific data types in MySQL that are better suited for storing text input from a TEXTAREA in PHP?
- What best practices should be followed when checking email addresses in PHP scripts?