jeremyapp Posted June 28, 2008 Share Posted June 28, 2008 Hi, I'm somewhat new and getting fairly good at PHP, but I've got a problem that I just can't seem to figure out. I have a database table with the following fields: id, FirstName, LastName, City, State, District Right now, I use the following code to create a table that loops through all the results: (note: I'm using the codeigniter framework so some of the database querying may look strange but you get the idea) $query = $this->db->query("SELECT id, FirstName, LastName, City, State, District FROM info "); echo "<table width='100%' border='2' bordercolor='#000000' cellpadding='2' style='border-collapse: collapse'>"; foreach ($query->result_array() as $row) { $id = $row['id']; $FirstName = $row['FirstName']; $LastName = $row['LastName']; $City = $row['City']; $State = $row['State']; $District = $row['District']; echo " <tr>"; echo " <td>$id</td>"; echo " <td>$FirstName</td>"; echo " <td>$LastName</td>"; echo " <td>$City</td>"; echo " <td>$State</td>"; echo " <td>$District</td>"; echo ' </tr>'; } echo "</table>"; This loops through everything just fine, but I'd like to be able to display the table so that it sorts the data by District (that is, all of the people in the same district are listed together). I can't seem to wrap my head around how to sort this so that it'll work properly. I've tried asort(), ksort(), and a few others, but I may just not be using them correctly. Any advice? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/112373-solved-trouble-sorting-a-database-query-result/ Share on other sites More sharing options...
thatsgreat2345 Posted June 28, 2008 Share Posted June 28, 2008 As I see it you kinda have two options, sort your database every time, or get all the data into one array and then sort it, and then display it rather then looping through each value and then displaying. Quote Link to comment https://forums.phpfreaks.com/topic/112373-solved-trouble-sorting-a-database-query-result/#findComment-576941 Share on other sites More sharing options...
.josh Posted June 28, 2008 Share Posted June 28, 2008 Why are you trying to make php do the sorting? That's the whole point of databases... $query = $this->db->query("SELECT id, FirstName, LastName, City, State, District FROM info order by District"); Quote Link to comment https://forums.phpfreaks.com/topic/112373-solved-trouble-sorting-a-database-query-result/#findComment-576946 Share on other sites More sharing options...
jeremyapp Posted June 28, 2008 Author Share Posted June 28, 2008 Wow! I obviously still have a little more learning to do - I can't tell you how hard I've been working to figure that one out. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/112373-solved-trouble-sorting-a-database-query-result/#findComment-576948 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.