Jump to content

Recommended Posts

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!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.