Order array based on other properties of the values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • beary
    New Member
    • Nov 2006
    • 170

    Order array based on other properties of the values

    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
  • hoopy
    New Member
    • Feb 2009
    • 88

    #2
    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.

    Comment

    • beary
      New Member
      • Nov 2006
      • 170

      #3
      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

      Originally posted by hoopy
      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.

      Comment

      • hoopy
        New Member
        • Feb 2009
        • 88

        #4
        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.

        Comment

        • beary
          New Member
          • Nov 2006
          • 170

          #5
          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!
          Originally posted by hoopy
          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.

          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.

          Comment

          Working...