Buchead Posted May 6, 2006 Share Posted May 6, 2006 Hello....I have an array that consists of multiple class items. The number of items depends upon what's read in from the database. I'm having trouble sorting the array based on one particular class item.The class item consists of personal information (name, address, time of service, dept) and the array is populated depending upon which dept is required. I'd like to have the output then sorted by time of service. I can get it displaying fine as in the order it's read from the database and stored in the array.Due to the information being in different tables I can't find a way to simple sort it using a mySql command.I've tried creating a new array and populating it each of the original array using the time of service as a key. But that doesn't work, and I'd think it's a very bad route to take. I "think" I know how to solve it but just know the commands!Thanks for any help,Clive. Quote Link to comment Share on other sites More sharing options...
Barand Posted May 7, 2006 Share Posted May 7, 2006 So there are two possible solution - order it in the original query, or - sort the arrayFor the first, we need to know the layouts of the tables.For the second we need to know the array structure. If you use var_export($arrayname) then post the output here it makes it easy to recreate the array locally. Quote Link to comment Share on other sites More sharing options...
Buchead Posted May 7, 2006 Author Share Posted May 7, 2006 The tables (5 of them) all have the same structure:Name - varchar(35)Address - varchar(200)Service - tinyintDept - char(10)I use the following to obtain a list of all the tables: $tbls = @mysql_query("SHOW TABLES");A loop then goes through retrieving the data and populating the array:- $result=@mysql_query("SELECT * FROM `$tbl` WHERE (`Name` LIKE '%$search%' OR `Dept` LIKE '%$search%') ORDER BY `Name`,`Service`",$link);I'm unsure how to get this command to put the output into the correct order as it's simply looping through the tables, ratherthan having the ability to jump between tables.Where about should I put the var_export as it throws up an error wherever I seem to put it.Thanks. Quote Link to comment Share on other sites More sharing options...
Barand Posted May 7, 2006 Share Posted May 7, 2006 I can't resist asking why 5 identically structured tables instead of 1?Solution 1[code]SELECT * FROM table1 WHERE (`Name` LIKE '%$search%' OR `Dept` LIKE '%$search%') UNIONSELECT * FROM table2 WHERE (`Name` LIKE '%$search%' OR `Dept` LIKE '%$search%') UNIONSELECT * FROM table3 WHERE (`Name` LIKE '%$search%' OR `Dept` LIKE '%$search%') UNIONSELECT * FROM table4 WHERE (`Name` LIKE '%$search%' OR `Dept` LIKE '%$search%') UNIONSELECT * FROM table5 WHERE (`Name` LIKE '%$search%' OR `Dept` LIKE '%$search%') ORDER BY `Service` [/code]Solution 2[code]$data = array ( array ( 'Name' => 'aaa', 'Address' => 'London', 'Service' => 30, 'Dept' => 'Dept A' ), array ( 'Name' => 'bbb', 'Address' => 'London', 'Service' => 10, 'Dept' => 'Dept B' ), array ( 'Name' => 'ccc', 'Address' => 'London', 'Service' => 20, 'Dept' => 'Dept C' ) );function service_sort ($a, $b) { if ($a['Service'] == $b['Service']) return 0; return $a['Service'] < $b['Service'] ? -1 : 1;}usort($data, 'service_sort');// check sort resultecho '<pre>', print_r($data, true), '</pre>';[/code] Quote Link to comment Share on other sites More sharing options...
Buchead Posted May 7, 2006 Author Share Posted May 7, 2006 Thanks for that - gives me somewhere to start.Unfortunately I didn't create the database. It's been long-existant in work and they won't let me change it. And my manager, like all managers, thinks it's easy to get the desired results! Though I guess it is if you've used php before.Thanks again......... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.