473,396 Members | 2,082 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 4343
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

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

Similar topics

2
by: Jason S | last post by:
Group, I'm hoping someone can shed some light on active directory search pagination for me. For the DirectorySearcher class there are several methods for paging (.PageSize,...
2
by: Chris H | last post by:
I am having a problem with pagination, basically the problem is happening in the "PREV / NUMBERS / NEXT" links, it appears as if the reason is becasue the increment and decrement operators aren't...
11
by: ste | last post by:
Hi there, Further to my recent posts where I've received excellent help from Rik and Jerry, I've ended up with an image gallery on my website that displays images in a table, 3 images per row. ...
4
by: comp.lang.php | last post by:
'll try to explain this as clearly as possible, sorry if it's unclear. You have in your directory /foo 42 images You have in your database metadata for 30 out of those 42 images You have to...
0
by: bindslind | last post by:
The script does most of what it's supposed to do, but I have what seems to be a simple problem that I can't figure out. When the page intitially loads the first page does not display images. The same...
1
by: shalini jain | last post by:
Hi, I want to know how can we do pagination using XSL. There are number of tutorials available on pagination using PHP but nothing with XSL. i am really stuck with my code. Below is the code that...
16
by: gnawz | last post by:
I have a pagination function I am using in a file called functions.php as below<? //Pagination functions function getPagingQuery($sql, $itemPerPage = 10) { if (isset($_GET) && (int)$_GET > 0) ...
4
by: ArizonaJohn | last post by:
Hello, The code below works great. The user enters a name into an HTML form, the code looks up a table with that name, and then that table is displayed. I am trying to use pagination with it,...
2
by: kkshansid | last post by:
this is my search page on which i am getting two parameters from previous page but the problem is that as soon as i click any other next pages my sql query fails as it doesnt get these two parameters...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.