Page 1 of 3 123 LastLast
Results 1 to 10 of 27
  1. #1
    Join Date
    Jun 2004
    Location
    USA
    Posts
    59

    Is this possible?

    I have a folder with a bunch of jpg pics. Is there a way for a script to read them, count how many there are and put them in alphabetical order?

    Example:

    Folder/ Jpg pics

    There are 34 pics in this page.

    PIC 1
    Valley.jpg

    Pic 2
    Ocean.jpg

  2. #2
    Join Date
    Oct 2003
    Posts
    35
    are you makeing a photo gallery script? ME too! : ) mine totaly ROCKS. any-ways here read this post i replyed to not too long ago.

    http://sv3.3dbuzz.com/vbforum/showth...threadid=75678

  3. #3
    Join Date
    Jun 2004
    Location
    USA
    Posts
    59
    Hey that's cool. Thanks.

    I put it on the server but it doesn't count anything at all. It just gives me a directory listing of all files.

    I don't know much php script to understand what the script does much less changing it to work for me.

    I will try to mess arround with it hopefully I get something working.

    Can anyone else hop in and give this question another push towards some light?

  4. #4
    Join Date
    Oct 2003
    Posts
    35
    looks like your really new to php huh? not that i'm a pro at php or anything i'm still learing too... just watch the vtms and maybe get some books (i recomend this one "PHP and MySQL Web Development" cheap on amazon.com). any-ways here is the same code what the count thing you mention.

    PHP Code:
    <?php

    $count 
    0;
    $dir "dir_name/";
    if (
    is_dir($dir)) {
    if (
    $dh opendir($dir)) {
    while (
    $file readdir($dh)) {   
    if (
    $file != '.' && $file != '..') {   
    echo 
    '<a href="'.$_SERVER['DOCUMENT_ROOT'].'/'.$file.'">'.$file.'</a><br>'
    $count++;  
    }   
    }   
    closedir($dh);   
    }   
    }   
    echo 
    'total files = '.$count;
    ?>

  5. #5
    Join Date
    Jun 2004
    Location
    USA
    Posts
    59
    Yup. I am new to the entire thing. I know a little html and still learning it. About the vtm.... *cough* I am still working to get that star so I can learn how to install a server.

    My main reason to be here was to learn how to install the Apache/php/mysql so I can test stuff up. Then after that go after php so I can learn the scripts. I would like to do a script much like the one you just did.

    If I could take a little bit more of your time... is there a way to take each file that the dir already got and maybe put them in an img tag?

    Example:

    - SCRIPT that got the file (mypic.jpg) -

    (Then on html)

    [IMG TAG MYPIC.jpg]


    I do thank you for the time you took to make the script and wish I could help back. However, with my current knowledge it might be impossible to do so... at least for the moment.

  6. #6
    Join Date
    Oct 2003
    Posts
    35
    i'm not entirely sure what u men dude. but i'll stab at it.

    replace this:
    echo '<a href="'.$_SERVER['DOCUMENT_ROOT'].'/'.$file.'">'.$file.'</a><br>';

    with this:
    echo '<a href="'.$_SERVER['DOCUMENT_ROOT'].'/'.$file.'"><img src="'.$_SERVER['DOCUMENT_ROOT'].'/'.$file.'"></a><br>';

  7. #7
    Join Date
    Mar 2003
    Location
    Santa Rosa, CA
    Posts
    49
    If you want to print them alpha just use an array. The following peice will also print them out in a table.

    PHP Code:
    <?php
    $IMGS 
    = array();

    $d dir("yourFolder/");
    while (
    false !== ($entry $d->read())) {
        if (
    $entry != "..") if ($entry != "."$IMGS[] = $entry;
    // while d->read
    $d->close();

    sort($IMGS); // Alpha Sort
    $count sizeof($IMGS); // Count IMGs

    if ($count 0) {
        
    ?><table cellpadding="5" cellspacing="0" border="0"><tr><?php
        
    for ($i 1; list($key$val) = each($IMGS); $i++) {
            if (
    $i == 1) echo "</tr><tr>";

            
    ?><td>
            <a href="<?php echo $_SERVER['DOCUMENT_ROOT'].'/'.$val?>">
            <img src="<?php echo $_SERVER['DOCUMENT_ROOT'].'/'.$val?>"></a><br />
            </td>
            <?php
            
        
    // for each IMGS
        
    ?></tr></table><?php
    // count > 0
    ?>
    - shamabaghodia!

  8. #8
    Join Date
    Jun 2004
    Location
    USA
    Posts
    59
    Woooow!!

    Its exactly how I wanted. Man you guys RULE!!
    shamabaghodia & mikemmx Thanks alot!

    Now I am going to save it and then take it appart and figure out just exactly how this works. (other then magic =P )


    Shamabaghodia... I have no clue what you mean by

    "If you want to print them alpha just use an array."

    Whats an array?

    Here is what I know of php so far

    PHP Code:
    $name "Teske";
    print 
    $name

  9. #9
    Join Date
    Mar 2003
    Location
    Santa Rosa, CA
    Posts
    49
    hehe... thats a good start.

    Arrays are just a way to organize things. Think of an array like a folder: you can put a lot of things into that folder (pushing onto an array)... you can give the folder a name (in our case, I named the array "$IMGS" because it will contain images)... you can even put other folders in a folder (multi-dimensional arrays, ask me later down your PHP road, heh).

    An array is useful for our problem, retrieving the filenames of images in a directory then printing them out alphabetically. If we put the image filenames into an array, we can do all sorts of things to the array that will affect the contents of the array. One of the most useful things is sorting.

    Here is some explanation of lines of the code:
    PHP Code:
    // We create a new array to use and give it the name $IMG.
    $IMG = array();

    // $IMG[] = $entry; We are pushing items onto the array.
    if ($entry != "..") if ($entry != "."$IMGS[] = $entry;

    // Sorts the array alphabetically
    sort($IMGS); // Alpha Sort

    // Gets the number of items in the array
    $count sizeof($IMGS); // Count IMGs 


    // Arrays have bookmarks... when the array was created
    // the bookmark is at page one

    // This for loop will grab the page number (key) and what is 
    // on the page (value) then move the bookmark to the next page

    // The key is like the page number of book, it's useful when we need 
    // to find a specific item in the array. Which is not the case in this
    // example because we are just printing everything.

    for ($i 1; list($key$val) = each($IMGS); $i++) { 
    - shamabaghodia!

  10. #10
    Join Date
    Mar 2003
    Location
    Santa Rosa, CA
    Posts
    49
    I was trying to keep the array explanation small but I'm sure someone will kick me if I don't talk more about "keys" in arrays. I am going to continue to use the book example to explain what keys in an array are.

    Lets create an array to use:
    $BOOK = array();

    Let's add some pages to our book (push items onto an array):
    $BOOK[] = "Contents of this page.";
    $BOOK[] = "Different content for this page.";
    $BOOK[] = "Even more content for this page.";

    Now let's say we want to read page 2, what do we do? (Remeber in programming numbers start with 0)
    echo $BOOK[1]; // Will print: Different content for this page.

    We haven't written page 4 of our book yet but we have written page 5 and want to add it:
    $BOOK[4] = "This is page 5 in our book.";

    Now for the glossary of our book we want to be able to find pages based on names and not numbers right? (This is known as associative arrays)
    $BOOK['Tutorials'] = "Info about tutorials.";
    echo $BOOK['Tutorials']; // Will print: Info about tutorials.


    Let me know if the book explanation helped you learned about keys in arrays, I've never tried teaching someone yet using it.
    - shamabaghodia!

Page 1 of 3 123 LastLast

Posting Permissions

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