468,268 Members | 1,549 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,268 developers. It's quick & easy.

Pagination from a directory...

Hello. Pretty new to PHP(and any sort of programming), I've been trying to modify this script that was originally written to display mysql data so it will paginate images from a directory. The code below is simplified to just show the filenames so I could see the structure more clearly. I can get the initial page "index4.php" to show up with the first 8 filenames, but when I attempt to proceed by clicking one of the page links I get this error:

Warning: array_slice() [function.array-slice]: The first argument should be an array in C:\Documents and Settings\Owner\My Documents\Websites\samcalef\test\images4.php on line 44

Although the page links do proceed accordingly, I don't get get any content. [HTML]Line 44 is in quotes[/HTML]

Here's the code and thanks in advance for any help:


<?
// Test to see if $_GET gets passed:
echo '<pre>'; print_r($_GET); echo '</pre>';

// Number of records to show per page:
$display = 8;

// Determine how many pages there are:
if (isset($_GET['np'])) { // Already been determined.

$num_pages = $_GET['np'];

} else { // Need to determine.

// Open the directory:
$handle = opendir('uploads/');
while (false !== ($file = readdir($handle))) {
$files[] = $file;
}

$num_records = count($files); // Get number of files in directory.


closedir($handle);// Close the directory.


// Calculate the number of pages.
if ($num_records > $display) { // More than 1 page.
$num_pages = ceil ($num_records/$display);
} else {
$num_pages = 1;
}

}// End of np else.

// Recieve file #.
if (isset($_GET['s'])) {
$start = $_GET['s'];

} else {
$start = 0;
}

$list = array_slice($files, $start, $display);

print_r($list);

if ($num_pages > 1) {

echo '<br /><p>';
// Determine what page the script is on.
$current_page = ($start/$display) + 1;

// If it's not the first page, make a Previous button.
if ($current_page != 1) {
echo '<a href="images4.php?s=' . ($start - $display) . '&np=' . $num_pages . '">Previous</a> ';
}

// Make all the numbered pages.
for ($i = 1; $i <= $num_pages; $i++) {
if ($i != $current_page) {
echo '<a href="images4.php?s=' . (($display * ($i - 1))) . '&np=' . $num_pages . '">' . $i . '</a> ';
} else {
echo $i . ' ';
}
}

// If it's not the last page, make a Next button.
if ($current_page != $num_pages) {
echo '<a href="images4.php?s=' . ($start + $display) . '&np=' . $num_pages . '">Next</a>';
}

echo '</p>';

}


?>
Oct 27 '06 #1
6 3838
ronverdonk
4,258 Expert 4TB
Read the Postiong Guidelines before you post any code in your thread!!
Especially the part about enclosing code within code, php or htl tags!! Who will try to underatnd this mess?

Ronald :cool:
Oct 28 '06 #2
Sorry. I suck at times. Here's the revised version:


Hello. Pretty new to PHP(and any sort of programming), I've been trying to modify this script that was originally written to display mysql data so it will paginate images from a directory. The code below is simplified to just show the filenames so I could see the structure more clearly. I can get the initial page "index4.php" to show up with the first 8 filenames, but when I attempt to proceed by clicking one of the page links I get this error:

Warning: array_slice() [function.array-slice]: The first argument should be an array in C:\Documents and Settings\Owner\My Documents\Websites\samcalef\test\images4.php on line 44

Although the page links do proceed accordingly, I don't get get any content.

Here's the code and thanks in advance for any help:


[PHP]<?
// Test to see if $_GET gets passed:
echo '<pre>'; print_r($_GET); echo '</pre>';

// Number of records to show per page:
$display = 8;

// Determine how many pages there are:
if (isset($_GET['np'])) { // Already been determined.

$num_pages = $_GET['np'];

} else { // Need to determine.

// Open the directory:
$handle = opendir('uploads/');
while (false !== ($file = readdir($handle))) {
$files[] = $file;
}

$num_records = count($files); // Get number of files in directory.


closedir($handle);// Close the directory.


// Calculate the number of pages.
if ($num_records > $display) { // More than 1 page.
$num_pages = ceil ($num_records/$display);
} else {
$num_pages = 1;
}

}// End of np else.

// Recieve file #.
if (isset($_GET['s'])) {
$start = $_GET['s'];

} else {
$start = 0;
}

$list = array_slice($files, $start, $display);

print_r($list);

if ($num_pages > 1) {

echo '<br /><p>';
// Determine what page the script is on.
$current_page = ($start/$display) + 1;

// If it's not the first page, make a Previous button.
if ($current_page != 1) {
echo '<a href="images4.php?s=' . ($start - $display) . '&np=' . $num_pages . '">Previous</a> ';
}

// Make all the numbered pages.
for ($i = 1; $i <= $num_pages; $i++) {
if ($i != $current_page) {
echo '<a href="images4.php?s=' . (($display * ($i - 1))) . '&np=' . $num_pages . '">' . $i . '</a> ';
} else {
echo $i . ' ';
}
}

// If it's not the last page, make a Next button.
if ($current_page != $num_pages) {
echo '<a href="images4.php?s=' . ($start + $display) . '&np=' . $num_pages . '">Next</a>';
}

echo '</p>';

}


?>[/PHP]
Oct 28 '06 #3
ronverdonk
4,258 Expert 4TB
When calling your script again, you are re-loading the page. So $files (and other variables) are not set! Save these variables in the $_SESSION array, then you can pick them up again after the reload.

Ronald :cool:
Oct 29 '06 #4
When calling your script again, you are re-loading the page. So $files (and other variables) are not set! Save these variables in the $_SESSION array, then you can pick them up again after the reload.

Ronald :cool:

Thanks a lot. I actually figured it out by having the script reread the directory before the line 44.

[PHP]// Open the directory:
$handle = opendir('uploads/');

while (false !== ($file = readdir($handle))) {
$files[] = $file;
}

$list = array_slice($files, $start, $display);


print_r($list);[/PHP]

I guess I could use $_SESSION to simplify the script, but I'm not exactly sure how to do that. Now I'm actaully in the process of trying to get the images to show instead of just the filenames. Here's the new code that's not working:

[PHP]<?
require_once ('functions.php'); // Connect to the db.

// Number of records to show per page:
$display = 8;

// Determine how many pages there are:
if (isset($_GET['np'])) { // Already been determined.

$num_pages = $_GET['np'];

} else { // Need to determine.

// Open the directory:
$handle = opendir('uploads/');
while (false !== ($file = readdir($handle))) {
$files[] = $file;
}

$num_records = count($files); // Get number of files in directory.


closedir($handle);// Close the directory.


// Calculate the number of pages.
if ($num_records > $display) { // More than 1 page.
$num_pages = ceil ($num_records/$display);
} else {
$num_pages = 1;
}

}// End of np else.

// Recieve file #.
if (isset($_GET['s'])) {
$start = $_GET['s'];

} else {
$start = 0;
}

$dir = 'uploads'; // Define the directory to view.

// Open the directory:
$dp = opendir($dir);

while (false !== ($file = readdir($dp))) {
$files[] = $file;
}

$images = array_slice($files, $start, $display);

// Somehow split up the array into parts.

if ( (is_file ("$dir/$image")) && (substr($image, 10 != '.')) ) {

// Print the information.
echo "<img src=\"$dir/$image\"/>";

} // End of the IF.


if ($num_pages > 1) {

echo '<br /><p>';
// Determine what page the script is on.
$current_page = ($start/$display) + 1;

// If it's not the first page, make a Previous button.
if ($current_page != 1) {
echo '<a href="images5.php?s=' . ($start - $display) . '&np=' . $num_pages . '">Previous</a> ';
}

// Make all the numbered pages.
for ($i = 1; $i <= $num_pages; $i++) {
if ($i != $current_page) {
echo '<a href="images5.php?s=' . (($display * ($i - 1))) . '&np=' . $num_pages . '">' . $i . '</a> ';
} else {
echo $i . ' ';
}
}

// If it's not the last page, make a Next button.
if ($current_page != $num_pages) {
echo '<a href="images5.php?s=' . ($start + $display) . '&np=' . $num_pages . '">Next</a>';
}

echo '</p>';

}


?>[/PHP]
Oct 29 '06 #5
ronverdonk
4,258 Expert 4TB
You have this part of code [php]// Print the information.
echo "<img src=\"$dir/$image\"/>"; [/php]
but where do you assign the file to the variable $image? Not in the script you show.

About the session array. You can start your script with the session_start(); command. Then you have an array $_SESSION available where you can store any variables that you want to keep during the session. Assigning a variable to the array is e.g.
$_SESSION['mydata'] = $MyVar;
next time you can assign it back or just leave it in the $_SESSION array.

Ronald :cool:
Oct 29 '06 #6
You have this part of code [php]// Print the information.
echo "<img src=\"$dir/$image\"/>"; [/php]
but where do you assign the file to the variable $image? Not in the script you show.

About the session array. You can start your script with the session_start(); command. Then you have an array $_SESSION available where you can store any variables that you want to keep during the session. Assigning a variable to the array is e.g.
$_SESSION['mydata'] = $MyVar;
next time you can assign it back or just leave it in the $_SESSION array.

Ronald :cool:
I got the images showing now and I'm just tweaking it. Thanks for your help.
Oct 29 '06 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Jason S | last post: by
2 posts views Thread by Chris H | last post: by
11 posts views Thread by ste | last post: by
4 posts views Thread by comp.lang.php | last post: by
1 post views Thread by shalini jain | last post: by
16 posts views Thread by gnawz | last post: by
2 posts views Thread by kkshansid | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.