Jump to content

Paging with controllers [HELP]


KenjiBailly

Recommended Posts

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 "&nbsp... <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 ", ";
      }
      }
      }
      }
 
?>


Link to comment
Share on other sites

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 by KenjiBailly
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.