Page 1 of 1
Pagination from opendir to glob
Posted: Wed Apr 12, 2006 3:29 pm
by ozzy
Hey again,
i had this script done for opendir
Code: Select all
$thispage = "a.php";
$dir = getcwd()."/";
$difference = 10;
$start = ($_GET['start'] == "" || !is_numeric($_GET['start'])) ? 0 : $_GET['start'];
$end = ($_GET['end'] == "" || !is_numeric($_GET['end'])) ? 0 : $_GET['end'];
if($end <= $start) $end = $start+10;
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
$i = 1;
while (($file = readdir($dh)) !== false) {
if($i >= $start && $i != $end ) {
echo "filename: ".$file." : filetype: " . filetype($dir . $file) . "<br>\n";
}
if($i == $end) break;
$i++;
}
closedir($dh);
}
}
$back = (($i-$difference)<0) ? 0 : ($i-$difference);
$forward = (($i+$difference) > $i) ? $_GET['forward'] : ($i+$difference);
echo "<a href='".$thispage."?start=".$back."&end=".$i."'>Back</a><br>\n";
echo "<a href='".$thispage."?start=".$forward."&end=".$i."'>Forward</a><br>\n";
How can i get it working with glob?
Code: Select all
foreach (glob("*.txt") as $filename) {
echo "$filename size " . filesize($filename) . "\n";
}
Posted: Thu Apr 13, 2006 7:45 am
by ozzy
When i tryed to do it, my script turned out as follows:
Code: Select all
<?php
$thispage = "index.php";
foreach (glob("files/*.*") as $filename) {
$difference = 10;
$start = ($_GET['start'] == "" || !is_numeric($_GET['start'])) ? 0 : $_GET['start'];
$end = ($_GET['end'] == "" || !is_numeric($_GET['end'])) ? 0 : $_GET['end'];
if($end <= $start) $end = $start+10;
$i = 1;
if($i >= $start && $i != $end ) {
echo "$filename " . filesize($filename) . "<br>";
}
if($i == $end) break;
$i++;
}
$back = (($i-$difference)<0) ? 0 : ($i-$difference);
$forward = (($i+$difference) > $i) ? $_GET['forward'] : ($i+$difference);
echo "<a href='".$thispage."?start=".$back."&end=".$i."'>Back</a><br>\n";
echo "<a href='".$thispage."?start=".$forward."&end=".$i."'>Forward</a><br>\n";
?>
No errors where returned, but the files are returned in a big long list - therefor the pagination didnt work.
Could someone look through the script (see above) and tell me where im going wrong? Thanks in advance!
Posted: Thu Apr 13, 2006 2:44 pm
by Christopher
You need to move these lines to before the loop like in the original. They are being recalculated each time.
Code: Select all
#
$start = ($_GET['start'] == "" || !is_numeric($_GET['start'])) ? 0 : $_GET['start'];
#
$end = ($_GET['end'] == "" || !is_numeric($_GET['end'])) ? 0 : $_GET['end'];
#
You could probably just use the original code, but replace:
Code: Select all
#
while (($file = readdir($dh)) !== false) {
#
with
Code: Select all
#
foreach (glob("files/*.*") as $filename) {
#
Posted: Fri Apr 14, 2006 4:18 pm
by ozzy
Thanks for the reply
I tryed both things you suggested but the output is still displayed in a long list with no error messeges returned.

Re: Pagination from opendir to glob
Posted: Fri Apr 14, 2006 4:29 pm
by Christopher
Does this work?
Code: Select all
$thispage = "a.php";
$dir = getcwd()."/";
$difference = 10;
$start = ($_GET['start'] == "" || !is_numeric($_GET['start'])) ? 0 : $_GET['start'];
$end = ($_GET['end'] == "" || !is_numeric($_GET['end'])) ? 0 : $_GET['end'];
if($end <= $start) $end = $start+10;
if (is_dir($dir)) {
$i = 1;
foreach (glob($dir . '*') as $file) {
if($i >= $start && $i != $end ) {
echo "filename: ".$file." : filetype: " . filetype($dir . $file) . "<br>\n";
}
if($i == $end) break;
$i++;
}
}
$back = (($i-$difference)<0) ? 0 : ($i-$difference);
$forward = (($i+$difference) > $i) ? $_GET['forward'] : ($i+$difference);
echo "<a href='".$thispage."?start=".$back."&end=".$i."'>Back</a><br>\n";
echo "<a href='".$thispage."?start=".$forward."&end=".$i."'>Forward</a><br>\n";
Posted: Fri Apr 14, 2006 5:40 pm
by ozzy
Now it just dosnt return with any files.

Re: Pagination from opendir to glob
Posted: Fri Apr 14, 2006 6:34 pm
by Christopher
In this code the loop works, I don't think the links do:
Code: Select all
$thispage = $_SERVER['SCRIPT_NAME'];
$dir = getcwd().'/';
$difference = 10;
$start = isset($_GET['start']) ? (int)$_GET['start'] : 0;
$end = isset($_GET['end']) ? (int)$_GET['end'] : 0;
if($end <= $start) {
$end = $start+10;
}
if (is_dir($dir)) {
$i = 1;
$files = glob($dir . '*');
foreach ($files as $path) {
$filename = basename($path);
if($i >= $start && $i != $end ) {
echo "filename: $filename : filetype: " . filetype($dir . $filename) . "<br>\n";
}
if($i == $end) break;
$i++;
}
}
$back = (($i-$difference)<0) ? 0 : ($i-$difference);
$forward = (($i+$difference) > $i) ? $_GET['forward'] : ($i+$difference);
echo "<a href='".$thispage."?start=".$back."&end=".$i."'>Back</a><br>\n";
echo "<a href='".$thispage."?start=".$forward."&end=".$i."'>Forward</a><br>\n";
FYI - I have Pager code
here that includes a PageableArray datasource that you could just pass your glob array to.
Posted: Sat Apr 15, 2006 4:12 am
by ozzy
Ah, thanks. I just changed the links at the bottom and now it works. The script:
Code: Select all
<?php
$thispage = $_SERVER['SCRIPT_NAME'];
$dir = getcwd().'/';
$difference = 30;
$start = isset($_GET['start']) ? (int)$_GET['start'] : 0;
$end = isset($_GET['end']) ? (int)$_GET['end'] : 0;
if($end <= $start) {
$end = $start+30;
}
if (is_dir($dir)) {
$i = 1;
$files = glob($dir . '*');
foreach ($files as $path) {
$filename = basename($path);
if($i >= $start && $i != $end ) {
echo "filename: $filename : filetype: " . filetype($dir . $filename) . "<br>\n";
}
if($i == $end) break;
$i++;
}
}
$back = (($i-$difference)<0) ? 0 : ($i-$difference);
$forward = (($i+$difference) > $i) ? $_GET['forward'] : ($i+$difference);
echo "<a href='".$thispage."?start=".$i."&end=".$i."'>Back</a><br>\n";
echo "<a href='".$thispage."?start=".$i."&end=".$i."'>Forward</a><br>\n";
?>
Thanks, thanks, and thanks again! I will check out that other one
