By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,304 Members | 1,254 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,304 IT Pros & Developers. It's quick & easy.

Files being read in (seemingly) random order

P: 18
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.
May 3 '07 #1
Share this Question
Share on Google+
4 Replies


code green
Expert 100+
P: 1,726
The order they are added to the database is meaningless to the database. You use ORDER BY to read the data back.
May 4 '07 #2

P: 18
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)?
May 4 '07 #3

pbmods
Expert 5K+
P: 5,821
Why not use a serial on the table that stores the images?

E.g.,

Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE `MyFiles`(
  2.  `fileid` serial,
  3.  `dirid` bigint(20) unsigned not null,
  4.  `filename` varchar(256) not null,
  5.  UNIQUE KEY `dirFile(`dirid`, `filename`)
  6. );
  7.  
or

Expand|Select|Wrap|Line Numbers
  1. ALTER TABLE `MyFiles` ADD `fileid` serial FIRST;
  2.  
'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.
May 4 '07 #4

P: 1
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
Aug 6 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.