I'm making a new thread on this, as I'm actually somewhere now.
I've got a paging system, but there's a problem.
I work with DAO's and controllers. Thing is, my second page (of my paging system) goes to index.php?page=2. The major problem here is that whenever you go to a page that doesn't exist in the index.php it redirects automatically to index.php. Page 2 isn't mentioned there as I can't mention every single page there as the pages will increase the more items there will be inserted.
Here's the entire code I'm using for the paging: (edited)
<?php
$mysqli = new mysqli("localhost", "wanthave", "wanthavepass", "wanthave");
$count_mem = $mysqli->query("SELECT `id` FROM `wanthave_items`");
$rows = $count_mem->num_rows;
$perPage = 5; // items to be shown per page
$num_pages = ceil($rows / $perPage);
$visiblePages = 5; // if current page is 7 show: 5,6,7,8,9,...
$visibleOffset = ceil($visiblePages / 2) - 1; // number of pages to show on the left and on the right of the current page
// Where do you use this ???
// $start = $page == 1 ? 0 : (($page - 1) * $perPage);
if ($num_pages > 1) {
$first = $last = $pagesStr = '';
echo $first . $pageStr . $last;
}
foreach($items as $item) {
echo "<li>
<a href=\"index.php?page=item-detail&id={$item['id']}\">{$item['name']}</a>
</li>";
}
for ($n = 1; $n < ($num_pages + 1); $n++) {
echo "<a href='index.php?page=$n'>$n</a>";
if ($n < $num_pages) echo ", ";
}
?>
my index.php
<?php
session_start();
define('DS', DIRECTORY_SEPARATOR);
define('WWW_ROOT', __DIR__ . DS);
$routes = array(
'home' => array(
'controller' => 'Item',
'action' => 'index'
),
'register' => array(
'controller' => 'Users',
'action' => 'register'
),
'login' => array(
'controller' => 'Users',
'action' => 'login'
),
'logout' => array(
'controller' => 'Users',
'action' => 'logout'
),
'item-detail' => array(
'controller' => 'Item',
'action' => 'detail'
),
);
if(empty($_GET['page'])) {
$_GET['page'] = 'home';
}
if(empty($routes[$_GET['page']])) {
header('Location: index.php');
exit();
}
$route = $routes[$_GET['page']];
$controllerName = $route['controller'] . 'Controller';
require_once WWW_ROOT . 'controller' . DS . $controllerName . ".php";
$controllerObj = new $controllerName();
$controllerObj->route = $route;
$controllerObj->filter();
$controllerObj->render();