Files being read in (seemingly) random order

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • guile
    New Member
    • Apr 2007
    • 18

    Files being read in (seemingly) random order

    hi, i'm using opendir( ) to create resource to a directory, and readdir( ) thereon to read the files one by one. I then add the file info into a database, and they are listed back in the very order that they were added. This order is a must-have.

    But because of some odd reason, the files are not being added either
    - alphabetically
    - date and time of uploading

    This is defeating my purpose. Moreover, it seems completely random. Why is this so? Is it something with Apache. Or does it depend on the OS my webhost is running on? I'm using apache on windows, and smething like this never occurred on my side.
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    The order they are added to the database is meaningless to the database. You use ORDER BY to read the data back.

    Comment

    • guile
      New Member
      • Apr 2007
      • 18

      #3
      Originally posted by code green
      The order they are added to the database is meaningless to the database. You use ORDER BY to read the data back.
      using ORDER BY is the recommended thing, but what do I order by?
      If I order by the file name, then things might go wrong as file uploads later might not exactly follow the same naming scheme but would be shown first based on their name.
      If I order by the file title/caption, then things go absolutely wrong.

      So only way for me was ordering them by the way they are added, which I expected to follow some alphabetical order, or the order in which files are uploaded.

      Any idea why its breaking on my serve while working fine on my side (WAMP)?

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Why not use a serial on the table that stores the images?

        E.g.,

        Code:
        CREATE TABLE `MyFiles`(
         `fileid` serial,
         `dirid` bigint(20) unsigned not null,
         `filename` varchar(256) not null,
         UNIQUE KEY `dirFile(`dirid`, `filename`)
        );
        or

        Code:
        ALTER TABLE `MyFiles` ADD `fileid` serial FIRST;
        'serial' is an alias for 'bigint(20) unsigned not null primary key auto_increment' .

        By default, MySQL will return your results ordered by the primary key, which would represent the order they were added to the table.

        Comment

        • gr8sale
          New Member
          • Aug 2007
          • 1

          #5
          You can order by any field you want, number, alphabetical, whatever.
          You don't have to add another field.
          You can also specify how you want the field sorted with asc & desc.
          Steph

          Comment

          Working...