1

I am creating a pagination using a text file as database instead of using mySQL. Already finished most of it, the only problem is the page numbering itself. How can I limit the maximum page numbering at the bottom part? And is there anyway to move to to the next set of pages after i press the last page number?

It's like this PREV 1 2 3 4 5 NEXT

<?php
    
    // connect to txt database
    $file="database.txt";
    $con = file_get_contents($file);
    
    // define how many results you want per page
    $results_per_page = 5;
    
    // find out the number of results stored in database
    $linecount = 0;
    $handle = fopen($file, "r");
    while(!feof($handle)){
      $line = fgets($handle);
      $linecount++;
    }
    fclose($handle);
    $number_of_results = $linecount;
    
    // determine number of total pages available
    $number_of_pages = ceil($number_of_results/$results_per_page);
    
    // determine which page number visitor is currently on
    if (!isset($_GET['page'])) {
      $page = 1;
    } else {
      $page = $_GET['page'];
    }  
    
    // display the links to the pages
    for ($page=1;$page<=5;$page++) {
      echo '<a href="index.php?page=' . $page . '">' . $page . '</a> ';
    }
    
    ?>
TaigaHyaga
  • 40
  • 6
  • 1
    Yes, both is possible. What have you tried so far? – Evert Jun 15 '21 at 17:03
  • Here in my code all page numbering appears. For example i have 10 pages, all of them shows in 1 line. Is there anyway to limit them? – TaigaHyaga Jun 15 '21 at 17:27
  • Yes there is. Did you write this code? `$page<=$number_of_pages` controls the limit, so you will want to do something different with this. – Evert Jun 15 '21 at 17:55
  • The code was originally based on reading data from mySQL database. I just modified it to read text files instead of mySQL. Can you help me with the logic,? I cant figure it out :/ – TaigaHyaga Jun 16 '21 at 01:36
  • If you learn about what each section does in `for(a; b; c)`, the answer should become more obvious. The `b` part should make sure that `$page` never exceeds the maximum you want. I'll help and reply here if you make an attempt to understand this and update your question with what you tried. Show us what _didnt_ work. So far I see no attempt made yet. – Evert Jun 16 '21 at 02:01
  • Yes I get that part. I limit the number of pages to 5 (already edited the code above). Now the next problem is to display the remaining page numbers whenever i press the last one – TaigaHyaga Jun 16 '21 at 06:49
  • Can you state that requirement as a more specific rule? For example, would it be correct to say that "if the page number is 5 or higher, start with 5 and end with 10?". Will that work for all cases you want? Try to phrase an exact rule (in english if you can't express it yet in javascript) – Evert Jun 16 '21 at 06:53
  • Let's say $number_of_pages = 18. The code above shows pages 1 2 3 4 5. Whenever i press 5, i want them to increase like 5 6 7 8 9. If 9 is pressed, pages will be 9 10 11 12 13 and so on. The basis of increase is when the last page number that is pressed. – TaigaHyaga Jun 16 '21 at 07:03
  • 1
    Ok, well you need to have 2 separate `$page` variables at least, one for the loop and one for the "current page" and it sounds that instead of starting with `$page = 1`, you need to start with `$page = floor($currentpage/4)+1` ? – Evert Jun 16 '21 at 07:07

1 Answers1

1

I see you are using a database. This would have been more easy if you were using CMS, but anyway check if you can understand this script or not:

try {

    // Find out how many items are in the table
    $total = $dbh->query('
        SELECT
            COUNT(*)
        FROM
            table
    ')->fetchColumn();

    // How many items to list per page
    $limit = 20;

    // How many pages will there be
    $pages = ceil($total / $limit);

    // What page are we currently on?
    $page = min($pages, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array(
        'options' => array(
            'default'   => 1,
            'min_range' => 1,
        ),
    )));

    // Calculate the offset for the query
    $offset = ($page - 1)  * $limit;

    // Some information to display to the user
    $start = $offset + 1;
    $end = min(($offset + $limit), $total);

    // The "back" link
    $prevlink = ($page > 1) ? '<a href="?page=1" title="First page">&laquo;</a> <a href="?page=' . ($page - 1) . '" title="Previous page">&lsaquo;</a>' : '<span class="disabled">&laquo;</span> <span class="disabled">&lsaquo;</span>';

    // The "forward" link
    $nextlink = ($page < $pages) ? '<a href="?page=' . ($page + 1) . '" title="Next page">&rsaquo;</a> <a href="?page=' . $pages . '" title="Last page">&raquo;</a>' : '<span class="disabled">&rsaquo;</span> <span class="disabled">&raquo;</span>';

    // Display the paging information
    echo '<div id="paging"><p>', $prevlink, ' Page ', $page, ' of ', $pages, ' pages, displaying ', $start, '-', $end, ' of ', $total, ' results ', $nextlink, ' </p></div>';

    // Prepare the paged query
    $stmt = $dbh->prepare('
        SELECT
            *
        FROM
            table
        ORDER BY
            name
        LIMIT
            :limit
        OFFSET
            :offset
    ');

    // Bind the query params
    $stmt->bindParam(':limit', $limit, PDO::PARAM_INT);
    $stmt->bindParam(':offset', $offset, PDO::PARAM_INT);
    $stmt->execute();

    // Do we have any results?
    if ($stmt->rowCount() > 0) {
        // Define how we want to fetch the results
        $stmt->setFetchMode(PDO::FETCH_ASSOC);
        $iterator = new IteratorIterator($stmt);

        // Display the results
        foreach ($iterator as $row) {
            echo '<p>', $row['name'], '</p>';
        }

    } else {
        echo '<p>No results could be displayed.</p>';
    }

} catch (Exception $e) {
    echo '<p>', $e->getMessage(), '</p>';
}

It is commented everywhere so it should be easy to understand.

Credit: Nev Stokes

Tejas Gupta
  • 496
  • 2
  • 8