pagination with foreach and glob

I would like to add a simple pagination system to my music player on my site and i am not really sure how to go about it. I have done pagination systems for results from mysql but not when pulling files from a directory.

Don’t need anything super fancy just a simple previous and next links if there is a next or previous page, and only 10 mp3s per page.

Here is my code:
$dir = “mp3/*”;

// Open a known directory, and proceed to read its contents
foreach(glob("$dir{*.mp3}", GLOB_BRACE) as $file)
echo ‘

  • <a class=“inline-playable” title="’ . basename($file,".mp3") . ‘" href="’ . basename($file) . ‘">’ . basename($file,".mp3") . ‘
  • ’;

    There is really no difference between the two. Instead of using pagination to get your database offsets, you use it to get your array key offsets

    I would just use the PHP’s file commands and pull the list of files into an array.
    Then, it would be very easy to paginate it any way you wish. Just have to keep tract of the array index.

    Check out PHP manual on reading filenames into an array… Hope that helps.

    Here is the link for this, check out the samples, the second one might help…

    Sponsor our Newsletter | Privacy Policy | Terms of Service