more on portfolio









August 2008
M T W T F S S
« May «-»  
 123
45678910
11121314151617
18192021222324
25262728293031






 


How to: PHP Sort folders in order

Log entry of April 6th 2008 03:33:07 pm
Filed under: Codes Related, How To
« The Ultimate Defence Trainer
An Elephant Silhouette »

A few days back, someone from Yahoo! Answers is having a problems with sorting folders in order. What she said is that the folder won’t list out in order. She’d tried rename the folders to numeric, using date formats from DD-MM-YYYY to MM-DD-YYYY and the recent one which I suggested, YYYY-MM-DD. But it still fails to sort accordingly. I don’t see why on her end, it don’t work, cause I’ve did it before and it works perfectly.

I wish I can upload that file to show all, but unfortunately I don’t have a backup for that and it is being used for some printing cards website which I have no access to.

Although, I can teach you a better solution for this with 5 easy steps, provided you must have access to phpMyAdmin. It is much more stable then the one I did for that printing site.

STEP ONE: Create the SQL Table
  • Create a table call sort_folder (or whatever name you want) in phpMyAdmin.
  • The fields are:
  1. folder_id : INT (255) auto-increment Primary
  2. folder_name : TEXT
  3. folder_date : VARCHAR (255)

STEP TWO: Creating the forms
  • Create a form with text fields of Name, Description and Date.
  • On the date field, set it to READONLY by editing it’s source.
  • Also on the date field, enter this in the value: <?php echo date(Ymd); ?>
  • Add a submit button.

STEP THREE: Validating your form

This is the MOST important part of the tutorial. Do not pass go… do not collect 200 dollars.
  • Validate your Name field so it will accept ONLY AlphaNumerics.

STEP FOUR: Submit and Creates
  • When you submit the form, it should :-
    1. INSERT to sort_folder TABLE accordingly
    2. Don’t set folder_id to any value as it is auto-increment.
  • Once the data have been successfully submitted, use makedir [ see function ] to create the folder based on the submitted value for $_POST['Name'].

STEP FIVE: List and Link em’
  • Add a few more entry to the database.
  • QUERY the data in order of folder_date: SELECT * FROM sort_folder ORDER BY folder_date
  • On your $row['folder_name'] value, link it the directory where you store all your created folder.
    eg: <a href=”../folderstorage/<?php echo $row['folder_name']; ?>” target=”_blank”><?php echo $row['folder_name']; ?></a>
…and the tutorial ends here.


There are few things you have to take note of:
  • It is wise not to have folder names with spaces or any form of symbols.
  • It is wise to have folder names in lowercase. Uppercase and Capitals will still work though.
  • If these folders are meant to be private, create an index.php in each of them with the content: <?php echo ‘hacking attempt’;exit; ?>
  • For folder deletion methods, have a go at my other how to.
Final note:
Hey CM, hope this tutorial will work out for you since all else fails.






Leave a Reply