Pagination from opendir to glob

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Pagination from opendir to glob

Post 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";
}
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Post 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!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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) {
#
(#10850)
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Post 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. :(
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Pagination from opendir to glob

Post 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";
(#10850)
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Post by ozzy »

Now it just dosnt return with any files. :?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Pagination from opendir to glob

Post 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.
(#10850)
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Post 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 :)
Post Reply