Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Help with a Robust File List/Index

    This year I told my mom that I'd try to type up her recipes that she'd collected over several years as a Christmas present of sorts. However, due to some computer issues, and the sheer volume of recipes she had, I'm not going to have them all done by Christmas time, so I figured the second best option I had until I could get them all typed was to make a well-organized listing of the scans I made.

    While I have the basic file listing made up, it's not easily searchable and kind of bland.

    My ultimate goals are the following:
    1. Get the files from the directory and store them in an array (Done)
    2. Sort The Files Alphabetically (Done)
    3. Remove The File Extensions From The Display Name, While Retaining It For The Link (Done)
    4. Replace the underscores in the displayed file name with spaces
    5. Turn Each Entry into a Link To The HTML or JPG file (Done)
    6. Subdivide Links By The First Letter of the File Name
    7. Add A "To Top" Link at the bottom of each category (I know how to do this, but I need the subdivision first)
    8. Create A List of each Letter at the top of the page with the anchor link to the start of that category (I can do this once I have the subdivision worked out.)


    After looking online for a while, I have had absolutely no luck finding code that will do the subdivision by first letter that will work with the rest of my code. (There is code out there, but either the code I've pieced together doesn't call information in the right way, or I'm trying to merge the two codes incorrectly.)

    I haven't yet looked for any code to replace the _ with spaces in the file name, because, to be honest, that's really just me being anal retentive. It's the least important entry in this list, but all the same, any help would be greatly appreciated.

    Considering it's been a couple of years since my PHP class and I haven't used it regularly, I'm a bit rusty. I've been thinking that perhaps my best option is a bunch of functions, and then calling them at the very end, but I'm definitely up for constructive criticism on that thought.

    While I love challenges, and normally don't like asking for help because I don't learn through doing that way (which is the way I learn), I work retail, and I know that the rest of this month is going to take it out of me both mentally and physically so I'm begging for mercy here.

    This is the code that I do have, taken and modified from multiple sites online.

    PHP Code:
    <?php
    function RemoveExtension($strName)  
    {  
         
    $ext strrchr($strName'.');  

         if(
    $ext !== false)  
         {  
             
    $strName substr($strName0, -strlen($ext));  
         }  
         return 
    $strName;  
    }  

    // Creates an array that will store all of the files
    $dirFiles = array();
    // opens the folder
    if ($handle opendir('./directory/HTML/chris_oslers_recipes')) {
        while (
    false !== ($file readdir($handle))) {
    // Strips out the parent directory and the index file.
            
    if ($file != "." && $file != ".." && $file != "index.php" && $file != "Thumbnails") {
                
    $dirFiles[] = $file;
            }
        }
        
    closedir($handle);
    }

    // Sorts the files alphabetically
    sort($dirFiles);

    // Creates a link for each file.
    foreach($dirFiles as $file)
    {
        echo 
    "<li><a href=\"http://www.brianosler.com/recipes/directory/HTML/chris_oslers_recipes/$file\">" RemoveExtension($file) . "</a></li>";
    }
    ?>
    Last edited by bosler; 12-02-2012 at 05:37 PM.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Here's how I'd do it. It's called a control-break, and what it does is evaluate the first letter of the link and compare it to the first letter of the link before it. If the letter has changed, then crank out the HTML coding for the new letter's heading. If it's the same, then spit out just the link.

    Here's the code:
    PHP Code:
    // Creates a link for each file.
    //Initialize the hold variable
    $previousLetter "";
    foreach(
    $dirFiles as $file)
    {
        
    //compare the first letter of the current $file to the previous $file
        //Note I'm also going to want to catch the very first iteration and echo a heading for that letter group
        
    if (substr($file01) != $previousLetter || $previousLetter == "")
        {
            
    //echo out whatever HTML you want to display at the beginning of each letter group; this is just an example.  You'll want to include an in-page anchor tag for your shortcut links you're going to put at the top of the page
            
    echo "<h1>Recipes that start with the letter ".substr($file01)."</h1>";
        }
        echo 
    "<li><a href=\"http://www.brianosler.com/recipes/directory/HTML/chris_oslers_recipes/$file\">" RemoveExtension($file) . "</a></li>";
        
    //Don't forget to assign the new letter to $previousLetter for the next iteration's comparison
        
    $previousLetter substr($file01);


  • Users who have thanked Fumigator for this post:

    bosler (12-03-2012)

  • #3
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank You, Fumigator! The page is pretty much exactly how I want it now.


  •  

    Tags for this Thread

    Posting Permissions

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