Results 1 to 7 of 7

Thread: sort function and readdir

  1. #1
    Join Date
    Jun 2007
    Posts
    72
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Default sort function and readdir

    I use this code below to update my database when I add new photos, but I want it to sort the pictures by filename before they are added, I know I need to use the sort function but I am not sure of where to place it, any help would be really useful.

    PHP Code:
    // Define the full path to the folder whose contents you want to list
    $path ".";

    //File Types allowed
    $pattern="\.(jpg|jpeg)$";

    // Open the directory
    $dir_handle = @opendir($path) or die("Error opening $path");

    // Loop through the files and filter out no jpgs
        
    while(false !== ($file readdir($dir_handle))){
                   if(
    eregi($pattern$file)){

    $first_letter $file{0};
                   
    $query  "SELECT filename FROM pictures WHERE filename='$file'";
    $result mysql_query($query) or die('Error, query failed one');    
    if (
    mysql_num_rows($result)==''){

    //Insert data into database
    $query "SELECT * FROM pictures WHERE category='$first_letter'";
    $result mysql_query($query) or die('Error, query failed');
    $count mysql_num_rows($result);
    $count $count 1;

    //Insert data into database
    $sql="INSERT INTO pictures (filename, category, seq_num, large, thumb)VALUES('$file', '$first_letter', '$count',  'http://www.calumogg.co.uk/pictures/large/$file', 'http://www.calumogg.co.uk/pictures/thumbs/$file')";
    $result mysql_query($sql) or die('Error, query failed two');

    echo 
    "<p>$file has been added to the database</p>"

    }

    Thanks in advance
    Last edited by calumogg; 02-20-2008 at 08:11 PM. Reason: Spelling!!

  2. #2
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    this script appears to only be able to handle 1 file at a time.

    It also almost sounds as if you want to re-sort your entire database every time a new image file is added. If that is the case that is the wrong thing to do. When grabbing the files from the database to display / process, it is very easy to sort them then, rather then resort the entire database every time a new image is added

    Code:
    SELECT field(s) FROM table(s) WHERE condition(s) ORDER BY field ASC/DESC
    ASC = ascending
    DESC = descending

  3. #3
    Join Date
    Jun 2007
    Posts
    72
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Default

    Sorry I must have phrased my question wrong, I don't want to resort my database every time. I just want the files to be arranged into alphabetical order before they are added into the database so that the script assigns the correct $count value to them. Would I need to use scandir to achieve this?

  4. #4
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    you could load the files into an array and sort the array than call this function/script to upload.

  5. #5
    Join Date
    Jun 2007
    Posts
    72
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Default

    I am not familiar with arrays, how would I go about doing this? Would it be possible for you to show me?

  6. #6
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    well first you would need to know how your script is being called in the first place...

    if you are creating a new function you would need to do something like
    Code:
    function sortImages($load=array(''))
    {
         if(is_array($load) && !empty($load)
         {
              $load = sort($load);
         }
         insertIntoDatabaseFunction($load); 
    }

  7. #7
    Join Date
    Jun 2007
    Posts
    72
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Default

    Thanks very much for that, I will have a play and see what I come up with

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
  •