473,396 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Order array based on other properties of the values

170 100+
Hello everyone

I have a scandir function which returns, as it should, an array of directories and files. The directories are, for example,

tid1, tid4, tid17 etc

But the 1, 4 and 17 are ids of people (the names are in a mysql db). Eg. id 1 is John Williams, id4 is Cathy Barnes and id 17 is Willy Franklin. I need the directories returned in alphabetical name order of the people, rather than in the order id1, tid4, tid17. So it should return in the order Cathy Barnes, Willy Franklin then John Williams.

So I was thinking that after the directories were in the array, I'd have to somehow do a name change, but I'm not sure how.

Does php have anything that might help me here? Or is there some obvious way I'm not aware of to solve this one?

Thanks in advance
Apr 6 '09 #1
4 1307
hoopy
88
Beary,

Can you not reverse the prodecure, pull all the users from the database in alphabetical order, then you can simply list them out and append their User ID to the "tid" directory prefix? You could also check if that directory exists with PHP's file_exists() function and print a warning against that user in the list if it doesn't.

There may be a certain reason you are doing it like this but it seems back to front.

Let us know if you need to do it your way or if you can reverse it.
Apr 6 '09 #2
beary
170 100+
Hoopy

Thanks for your reply. I like your idea a lot in principle. Just not sure it'll work here. I am using the php file tree, found at http://abeautifulsite.net/notebook.php?article=21

It's a wonderful way to list the files, and I've modified it quite a bit already to cope with the different demands of my application. Perhaps your way will work (and I'm hoping it will), so I'll provide more info.

Structure of files on the server is like this:

teacherid/classid/students/stid

eg. tid4/cid7/students/stid13

So php file tree will start with the directory tid4 (which is a teacher's directory) and basically recursively work its way through that directory, grabbing directories and files. So when it gets to the directories in the students directory, these might be stid1, stid4, stid17. But they'd need to be ordered by student name, rather than number.

So I don't think I'd be able to pull from the db first. Do you see a way around this? I really do need to be able to keep using php file tree.

Thanks

@hoopy
Apr 6 '09 #3
hoopy
88
Hi there, I understand now, looks like if you want to use that tree code which looks real nice by the way you need to hack it somewhat to do the following:

The part where it descends into the final directory, so check to match the "tid" part, you need to get the ID by chopping off the prefix, query the DB and match the User Name, pull it out, then you need to make the label, not the link, the User name.

However concerning the ordering if the names are stored like "Willy Franklin" then it will order by the first character, so you may as well need to change the User Name when you pull it out to make the label to "Surname, First Name". Although is it the Jscript which does the ordering or the PHP?

I understand exactly what you are trying to achieve and that method should work if the order is parsed and determined by the javascript and based on the a ref labels, not the real links, if you understand what I mean.

Ill try and help you out with it but at the moment I am busy on something, give it a bash and let us know how you get on and will try and help.

Sorry cant offer much else.
Apr 6 '09 #4
beary
170 100+
Hoopy

Yes, I can easily do the part about querying the DB, everything is stored correctly so I can get surname and first name separately etc. In fact, I've already done that, in order to display the name of the folder for the user (rather than stid46).

However, I think the files and directories are being sorted by php. I'm pretty sure the js is used to get the nice expanding and collapsing look.

As far as sorting, the two functions I can see in there are natcasesort and sort, which seem to be php. So even if I substituted the numbers (eg. stid46) with names (eg. Bill Rogers), I think it's still going to sort by the directory names. Is this right? Is there a way around it.?

Thanks for your (and anybody else's) continued help!
@hoopy
Apr 7 '09 #5

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

Similar topics

27
by: Abdullah Kauchali | last post by:
Hi folks, Can one rely on the order of keys inserted into an associative Javascript array? For example: var o = new Object(); o = "Adam"; o = "Eve";
26
by: JGH | last post by:
How can I check if a key is defined in an associative array? var users = new array(); users = "Joe Blow"; users = "John Doe"; users = "Jane Doe"; function isUser (userID) { if (?????)
47
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
22
by: VK | last post by:
A while ago I proposed to update info in the group FAQ section, but I dropped the discussion using the approach "No matter what color the cat is as long as it still hounts the mice". Over the last...
35
by: VK | last post by:
Whatever you wanted to know about it but always were affraid to ask. <http://www.geocities.com/schools_ring/ArrayAndHash.html>
4
by: emma middlebrook | last post by:
Hi Straight to the point - I don't understand why System.Array derives from IList (given the methods/properties actually on IList). When designing an interface you specify a contract. Deriving...
16
by: mdh | last post by:
May I ask the group the following: (Again, alas , from K&R) This is part of a function: while ( ( array1 = array2 ) != '\0' ); /* etc etc */ Is this the order that this is evaluated? ...
45
by: VK | last post by:
(see the post by ASM in the original thread; can be seen at <http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/3716384d8bfa1b0b> as an option) As that is not in relevance to...
14
by: julie.siebel | last post by:
I've been wrestling with a really complex page. All the data is drawn down via SQL, the page is built via VBScript, and then controlled through javascript. It's a page for a travel company that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.