KenjiBailly Posted April 22, 2014 Share Posted April 22, 2014 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: <?php $mysqli = new mysqli("localhost", "wanthave", "wanthavepass", "wanthave"); $count_mem = $mysqli->query("SELECT `id` FROM `wanthave_items`"); $rows = $count_mem->num_rows; $num_pages = ceil($rows / 5); if (isset($_REQUEST['page'])) { $page = $_REQUEST['page']; } else { $page = 1; } if ( empty($_REQUEST['page'])) { $start = 0; } else { $start = ($page - 1) * 5; } if ($num_pages != 1) { if ($num_pages > 5) { echo "<a href='index.php?page=1'>First</a> ... "; for ($n = $page - 2; $n < $page + 2; $n++) { echo "<a href='index.php?page=" . $n . "'>" . $n . "</a>"; } echo " ... <a href='index.php?page=" . $num_pages . "'>Last</a>"; } else { foreach($items as $item) { echo "<li>"; echo "<a href=\"index.php?page=item-detail&id=" . $item['id'] . "\">"; echo $item['name']; echo "</a>"; echo "</li>"; } for ($n = 1; $n < ($num_pages + 1); $n++) { echo "<a href='index.php?page=" . $n . "'>" . $n . "</a>"; if ($n == $num_pages) { echo ""; } else { echo ", "; } } } } ?> Quote Link to comment Share on other sites More sharing options...
KenjiBailly Posted April 22, 2014 Author Share Posted April 22, 2014 (edited) Bump? Could someone please help? Changed my code to this: <?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 ", "; } ?> Edited April 22, 2014 by KenjiBailly Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.