SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast helpplease's Avatar
    Join Date
    Apr 2001
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Making Multiple Pages?

    One quick question;

    I have a script (in PERL) that opens a directory, reads all files in it, and prints them out with the total number.

    Code:
    $counter=0;
    opendir (DIR,"$dir") or die(" $dir $!");
            @dir=readdir(DIR);
            closedir(DIR);
            @dir = sort(@dir);
                    foreach $line (@dir) {
            if (-f "$dir/$line" && $line ne "." && $line ne "..") {
    	 $line =~ s/\ / /g;
                   print "$line\n";
                   $counter++;
    	
    	}
    }
    
    print $counter;
    However, say the directory has 1000 files, that's one long page! Is there a way to break it up into multiple pages?


    TIA!

  2. #2
    SitePoint Enthusiast helpplease's Avatar
    Join Date
    Apr 2001
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anyone...?
    Please...?

  3. #3
    Grumpy Mole Man Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,067
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes there is. You'll need to know a bit of Perl for this (my Perl is pretty rusty but I can tell you the basics of how to do it).

    @dir in your script is an array of files in the directory. What you want to do is show only the first 20 elements of that array, and then give the user the option to show the next 20, then the next 20, then the next 20 and so on. You will need to allow a value to be passed to the script indicating what "page" of results to display:

    list.pl?page=1 - displays the first 20 files
    list.pl?page=2 - displays the next 20 files

    You should be able to reduce the array to contain only the 20 files relevant to the current "page" - I cant' remember off the top of my head how you would do that to an array in Perl but it's definitely possible.

    Hope that's given you some idea of how to go about it - if not just say and I'll get out a Perl book and see if I can figure out the code.

  4. #4
    SitePoint Enthusiast helpplease's Avatar
    Join Date
    Apr 2001
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I know that much, my part is getting page 2 to display from 'X' and on.

    Example;

    Code:
    $counter=0;
    opendir (DIR,"$dir") or die(" $dir $!");
            @dir=readdir(DIR);
            closedir(DIR);
            @dir = sort(@dir);
                    foreach $line (@dir) {
            if (-f "$dir/$line" && $line ne "." && $line ne "..") {
    	 $line =~ s/\ / /g;
           if($counter>24){ 
                   print "<a href=\"page.cgi?2\">Next Page</a>"; 
                   exit;
                              }
                   print "$line\n";
                   $counter++;
    	
    	}
    }
    
    print $counter;
    So on the first page, it'll show the first 25 results, then give a link to the next page. But HOW do I tell the next page to start from result 26 and up..??

  5. #5
    Grumpy Mole Man Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,067
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Off the top of my head...
    Code:
    for ($i=25;$i<50;$i++)
    {
        print $dir[$i] . "\n";
    }
    That will print every item in the @dir array starting at item 25 and ending at item 50.

  6. #6
    SitePoint Enthusiast helpplease's Avatar
    Join Date
    Apr 2001
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, I didn't know you could do that.
    I'll give it a try and see what happens.
    TX!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •