#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    21
    Rep Power
    0

    open directory, limit results w/NEXT and BACK links? NO MySQL database


    I've looked everywhere on this forum and other forums for a similar thread, but everything is written for use with MySQL.

    I've built a gallery that opens a directory and outputs thumbs 4 across and 'x' down (however many are in the directory). What I need to do is limit the thumbs to 20 per page and if they exceed 20 I need NEXT and BACK links to appear. Is this even possible with a directory listing or is this only doable w/a MySQL database.

    Thanks.
  2. #2
  3. // no comment
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2001
    Posts
    1,643
    Rep Power
    43
    you can try storing your filenames in an array and loop through that when you do your pages.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    21
    Rep Power
    0
    Thanks! That makes sense .. I found a similar thread on another forum that 'in theory' seems to work ... but I'm having no luck. I'm so used to working with database results .. this is driving me nuts.
    Last edited by flashvicitm; February 1st, 2004 at 08:44 PM.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    21
    Rep Power
    0
    Here's another stab .. a little closer .. I'm trying to focus on limiting the directory listing before I tackle the next, prev links. Now I'm only knocking one file out of the listing instead of upto that file.
    Last edited by flashvicitm; February 8th, 2004 at 01:25 PM.
  8. #5
  9. Learn logic, not PHP!
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jan 2004
    Location
    Hong Kong SAR
    Posts
    1,006
    Rep Power
    16
    Read the directory into a array (let say $arr).

    assume $ITEMCOUNT = 20 (number of items per page)

    the starting array index $start = ($page - 1) * $ITEMCOUNT
    the ending array index $end = $start + $ITEMCOUNT - 1

    Hence, for page 1, $start = 0, $end = 19, display $arr[$start] to $arr[end]
    page 2, $start = 20, $end = 39
    and so on.....
  10. #6
  11. // no comment
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2001
    Posts
    1,643
    Rep Power
    43
    you can do something like this.

    PHP Code:
    <?php
      
    // open directory

      
    $page = (isset($_GET['page']) ? $_GET['page'] : 1;

      if (
    $dir = @opendir("my/directory/"))
      {
        while ((
    $file readdir($dir)) !== false)
        {
          if (
    $file != ".." && $file != ".")
          {
            
    $filelist[] = $file;
          }
        }
        
    closedir($dir);
      }

      
    // let user know if there are more than 5 files in selected directory

      
    $a 5;
      
    $filecount count($filelist);
      if (
    $filecount $a)
      {
        print 
    "there's more than 5 files here<br><br>";
      }
      else
      {
        print 
    "there's less 5 files here<br><br>";
      }

      print 
    implode("<br>"$filelist)."<br><br>";
      print 
    "total files = " .count($filelist)."<br><br>";

      
    //trying to limit results .. not working

      
    $limit 10;
      if (
    count($filelist) >= $limit)
      {
        
    $filelist[$limit] = $file;
        print 
    implode("<br>"$filelist)."<br><br>";
      }


      
    $from $limit $page;
      
    $to $from $limit;
      
    $pages $filecount $limit;

      
    // echo your files
      
    for ($i $start$i <= $to$i++)
      {
        echo 
    $filelist[$i-1];
        if (
    $i == $filecount)
        {
          
    $i $to 1;
        }
      }


      
    // echo your prev next links
      
    if ($page 1)
      {
        echo 
    'prev link';
      }

      if (
    $page $pages)
      {
        echo 
    'next link';
      }

      
    // you can even create links for the different pages with a little more coding 
    ?>
    Last edited by roninblade; February 1st, 2004 at 09:08 PM.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    21
    Rep Power
    0
    Thanks, but i'm getting parse errors on line 3 ...
  14. #8
  15. // no comment
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2001
    Posts
    1,643
    Rep Power
    43
    oops. i'm missing a ) there. should be $page = (isset($_GET['page'])) ? $_GET['page'] : 1;
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    21
    Rep Power
    0
    I'm getting closer ...

    Do I need to set a variable for the page # or is that being generated based on the file limit?

    Now I'm getting 11 images, but not from the first image .. I tried to specify 10 images only. It also seems like I've mixed up the 'showing x of x images'.
    Last edited by flashvicitm; February 2nd, 2004 at 09:49 AM.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    21
    Rep Power
    0
    First off THANKS! I finally figured out how to limit my page to 10 results. Now I'm trying to figure out how to set up the next and back links. Gonna try a few more things. Here's what I've got so far:
    Last edited by flashvicitm; February 8th, 2004 at 01:26 PM.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    21
    Rep Power
    0
    I'm still struggling with this .. should I set up the page variable with and if/else statement? Something like

    PHP Code:
    if ($something == $here){
      
    $page = (isset .. 
    } else {
      
    $page $HTTP_GET_VARS[.. // based on my next previous links

    and should the links contain the directory listing as a string .. I'm thinking it needs to, but I'm not positive .. that seems really messy. Thanks.
    Last edited by flashvicitm; February 5th, 2004 at 08:51 PM.
  22. #12
  23. // no comment
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2001
    Posts
    1,643
    Rep Power
    43
    you're already setting up the page variable with this line

    $page = (isset($_GET['page'])) ? $_GET['page'] : 1;

    the ? and : are ternary operators. look it up in the php manual - php comparison operators

    no you should not put the directory listing in the page links as you put it that would be very messy and the url query string has a limitation. it is better to save the directory listing in a session array.
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    21
    Rep Power
    0
    ok .. I'm partially getting this. I always try to hit the manual or search forums befor I post. Half the time I'm not sure what I need to be searching for .. just so you don't think I'm being lazy. I really want to figure this out.

    I'm able to limit my list to 20 and am able to go to the next page. I don't think I need to use a session variable for this. What's confusing me is the $to, $from, $start variables. They work for the actual list, but they aren't the solution to the info 'displaying 'x' of 'x' images'.

    Please tell me if I'm wrong on the session issue.
    Last edited by flashvicitm; February 7th, 2004 at 07:19 PM.
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    21
    Rep Power
    0
    ok .. now i've got it except for two things

    1) the "displaying 'x' to 'x' of 'x' images' needs to be worked out

    2) if you are on the first page and click 'next' it kills the first file and reads the last file from the previous page. I think it has to do w/ $filelist[$i-1]. I've been playing with that, but no success. All my files are labeled 01, 02, 03, etc. for testing.

    Here's where I'm at now.
    Last edited by flashvicitm; February 8th, 2004 at 01:27 PM.
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    21
    Rep Power
    0

    Talking


    Thanks for your help. I figured it out!

IMN logo majestic logo threadwatch logo seochat tools logo