472,986 Members | 3,031 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,986 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 4318
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.