View Full Version : help with adding pagination to existing php

DJ Hands3
11-19-2010, 07:38 PM
Hello I have this existing php that will look in a directory then get the files and display the links along with a downloads counter per file, but as you can imagine the page is getting quite long, I have found a pagination php script that looks good, but im stuck on adding it into my existing php,

heres my current set up


// Error reporting:

// Including the DB connection file:
require 'connect.php';

$files_array = array();

/* Opening the thumbnail directory and looping through all the thumbs: */

$dir_handle = @opendir($directory) or die("There is an error with your file directory!");

while ($file = readdir($dir_handle))
/* Skipping the system files: */
if($file{0}=='.') continue;

/* end() returns the last element of the array generated by the explode() function: */
$extension = strtolower(end(explode('.',$file)));

/* Skipping the php files: */
if($extension == 'php') continue;


/* Sorting the files alphabetically */


$result = mysql_query("SELECT * FROM download_manager");

/* The key of the $file_downloads array will be the name of the file,
and will contain the number of downloads: */



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP &amp; MySQL File Download Counter | Tutorialzine demo</title>

<link rel="stylesheet" type="text/css" href="styles.css" />
<link rel="stylesheet" type="text/css" href="fancybox/jquery.fancybox-1.2.6.css" media="screen" />

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="script.js"></script>



<div id="file-manager">

<ul class="manager">

foreach($files_array as $key=>$val)
echo '<li><a href="download.php?file='.urlencode($val).'">'.$val.'
<span class="download-count" title="Times Downloaded">'.(int)$file_downloads[$val].'</span> <span class="download-label">download</span></a>




And heres the pagination i wish to put in


* PHP Array Pagination Copyright 2007 - Derek Harvey
* www.lotsofcode.com
* This file is part of PHP Array Pagination .
* PHP Array Pagination is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* PHP Array Pagination is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with PHP Array Pagination ; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<meta http-equiv="content-type" content="text/html; charset=windows-1250">
<title>PHP Array Pagination</title>
body {
font-family: Tahoma, Verdana, Arial, Sans-serif;
font-size: 11px;
hr {
border: 1px #ccc;
border-style: none none solid none;
margin: 20px 0;
a {
color: #333;
text-decoration: none;
a:hover {
text-decoration: underline;
a.selected {
font-weight: bold;
text-decoration: underline;
.numbers {
line-height: 20px;
word-spacing: 4px;

<h1>PHP Array Pagination</h1>
<hr />
// Include the pagination class
include 'pagination.class.php';
// Create the pagination object
$pagination = new pagination;

// some example data
foreach (range(1, 100) as $value) {
$products[] = foreach($files_array as $key=>$val)

// If we have an array with items
if (count($products)) {
// Parse through the pagination class
$productPages = $pagination->generate($products, 10);
// If we have items
if (count($productPages) != 0) {
// Create the page numbers
echo $pageNumbers = '<div class="numbers">'.$pagination->links().'</div>';
// Loop through all the items in the array
foreach ($productPages as $productArray) {
// Show the information about the item
echo '<p><b>'.urlencode($val).'">'.$val.'</b> &nbsp; &pound;'.(int)$file_downloads[$val].'</p>';
// print out the page numbers beneath the results
echo $pageNumbers;
<hr />
<p><a href="http://www.lotsofcode.com/php/projects/php-array-pagination" target="_blank">PHP Array Pagination</a> provided by <a href="http://www.lotsofcode.com/" target="_blank">Lots of Code</a></p>

11-19-2010, 07:46 PM
If your database contains the filenames and number of downloads,
why do you need to open the directory and load the names of the files?

I would think you only need to work with the database, not the directory.

What am I not seeing here?