Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic Pagination Help Needed

Tags:

php

I have this script:

<?php
if(urldecode($_SERVER['REQUEST_URI']) == "/?articles")
{
    include("Articles.php");
}
?>

Which allows me to setup a custom URL for a page. Now, the problem occurs comes when I need URLs like these:

/?articles | Page that contains newest articles which loads when user clicks "< Previous" button
/?articles&1 | Page that contains older articles which loads when user clicks "Next >" button
/?articles&2 | Page that contains older than older articles
/?articles&3 | Page that contains old articles
/?articles&4 | Page that contains old articles
/?articles&5 | Page that contains oldest articles
/?articles&6 | Page that contains oldest articles, also last page in the pagination

And so on. Namely, the code in the script above would be like this:

if(urldecode($_SERVER['REQUEST_URI']) == "/?articles&1")
{
    include("Articles-500.php");
}
if(urldecode($_SERVER['REQUEST_URI']) == "/?articles&2")
{
    include("Articles-499.php");
}
if(urldecode($_SERVER['REQUEST_URI']) == "/?articles&3")
{
    include("Articles-498.php");
}
if(urldecode($_SERVER['REQUEST_URI']) == "/?articles&4")
{
    include("Articles-497.php");
}
if(urldecode($_SERVER['REQUEST_URI']) == "/?articles&5")
{
    include("Articles-496.php");
}
if(urldecode($_SERVER['REQUEST_URI']) == "/?articles&6")
{
    include("Articles-495.php");
}

But each time the page with the newest articles gets filled (contains 10 articles per page) I have to update the script like this:

if(urldecode($_SERVER['REQUEST_URI']) == "/?articles&1")
{
    include("Articles-505.php");
}
if(urldecode($_SERVER['REQUEST_URI']) == "/?articles&1")
{
    include("Articles-504.php");
}
if(urldecode($_SERVER['REQUEST_URI']) == "/?articles&1")
{
    include("Articles-503.php");
}
if(urldecode($_SERVER['REQUEST_URI']) == "/?articles&1")
{
    include("Articles-501.php");
}
if(urldecode($_SERVER['REQUEST_URI']) == "/?articles&1")
{
    include("Articles-500.php");
}

I also need you to tell me how to use it practically in my navigation, id est: <A href="?">Next</A> and <A href="?">Previous</A>

like image 338
De1an Avatar asked May 18 '20 23:05

De1an


People also ask

What is the need for pagination?

Pagination is used in some form in almost every web application to divide returned data and display it on multiple pages within one web page. Pagination also includes the logic of preparing and displaying the links to the various pages. Pagination can be handled client-side or server-side.

Does pagination improve performance?

Thanks to pagination, we can split our large dataset into chunks ( or pages ) that we can gradually fetch and display to the user, thus reducing the load on the database. Pagination also solves a lot of performance issues both on the client and server-side!


1 Answers

What you can do, in order to have this created dynamically is to use the function glob that can list you all the files in a directory.

So given that you would have the files:

  • Articles-1.php
  • Articles-2.php
  • Articles-3.php
  • ...

Now, if your articles are indeed named as above (Articles-1.php and not Articles-001.php) you will face the problem that a simple alphabetical sort won't work.
Indeed, Articles-99.php will always show up earlier than Articles-505.php in a simple alphabetical sort.

But, hopefully, PHP offers us the really neat sorting which is natsort().

This function implements a sort algorithm that orders alphanumeric strings in the way a human being would while maintaining key/value associations. This is described as a "natural ordering". An example of the difference between this algorithm and the regular computer string sorting algorithms (used in sort()) can be seen in the example below.

Sorting via natsort() is nicer than my original usort idea, all credit for this idea goes to dılo sürücü's answer.

Still, we need the sorting to be in a reverse order, and this can be provided using the SORT_NATURAL flag on rsort().

SORT_NATURAL - compare items as strings using "natural ordering" like natsort()

Source: https://www.php.net/manual/en/function.sort.php
Credit for the usage of this flag goes to Seh Horng's answer

// Listing all the articles pages
$articles = glob("Articles-*.php");
// Applying reverse natural sorting, with SORT_NATURAL flag on rsort
rsort($articles, SORT_NATURAL);

/**
 * Extracting the selected page out of the REQUEST_URI, 
 * and transform it an integer
 */
$selectedPage = intval(
  str_replace("/?articles&", "", urldecode($_SERVER['REQUEST_URI']))
);

/**
 * This is a sanity check; 
 * just in case someone tries to access a page that does not exists,
 * like the page 99999999999
 * It also tackles the case when intval fails and returns 0
 * causing the index to be 0 - 1 = -1
 *
 * By the way, doing this check also covers your default behaviour
 * on the URL /?articles
 */
if(array_key_exists($selectedPage - 1, $articles)){

  /** 
   * We need to remove one from the selected page, 
   * remembering an array start at 0 in PHP
   */
  include $articles[$selectedPage - 1];
} else {
  include "Articles.php";
}

This should give you the right include dynamically.

like image 92
β.εηοιτ.βε Avatar answered Oct 14 '22 06:10

β.εηοιτ.βε