www.webdeveloper.com
+ Reply to Thread
Results 1 to 2 of 2

Thread: sorting db data

  1. #1
    Join Date
    Feb 2005
    Location
    Middle Village, NY
    Posts
    203

    sorting db data

    hey guys, was wonder how i can sort information alphabetically that im getting from a db that stores, id, first,last email ... i want to sort it by last name.

    heres the function i created to pull the info:
    Code:
    /*show all adjuncts in the database system*/
    function show_adjuncts(){
    	//set to global so we can use it
    	global $adj_count;
    	$color = 1;
    	$color_str = "";
    	
    	//query the db for adjuncts
    	$query = "SELECT id, first, last, email, contact_num, campus_hours FROM Teachers"; 
        $result = mysql_query($query);
    	if($result && mysql_num_rows($result) > 0){
    			while ($list = mysql_fetch_array($result)) { 
    			  //increment count
    			  $adj_count += 1;
    			  //color row swap
    			  if($color == 1){
    				$color_str = "#DCE9F8";
    				$color = 2;
    			  }
    			  else{
    				$color_str = "#ffffff";
    				$color = 1;
    			  }
    			  //html table rows to print
    			  $str = $str . 
    						"<tr bgcolor=\"$color_str\" class=\"regular_text\">
    								<td>$list[0]</td>
    								<td>$list[1]</td>
    								<td>$list[2]</td>
    								<td>$list[3]</td>
    								<td>$list[4]</td>
    								<td>$list[5]</td>
    								<td><a href=\"$php_self?update_faculty=update&adj_id=$list[0]&adj_first=$list[1]&adj_last=$list[2]
    												&adj_email=$list[3]&adj_contact=$list[4]&adj_hours=$list[5]\">edit</a><a 
    												class=\"edit\" href=\"javascript:void(0)\" onClick=\"remove_prompt($list[0])\"  >/delete</a></td>
    							</tr>";
    		   }//end while loop
    	}else{
    		$color_str = "#DCE9F8";
    		$str = "<tr bgcolor=\"$color_str\" class=\"regular_text\">
    						<td colspan=\"7\">Sorry, there are no Adjunct to display.</td>
    				</tr>";
    	}
      
       return $str;
    }
    ...?

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,049
    PHP Code:
    $query "SELECT id, first, last, email, contact_num, campus_hours FROM Teachers ORDER BY last"
    PS: If you want to sort by last name and then by first name:
    PHP Code:
    $query "SELECT id, first, last, email, contact_num, campus_hours FROM Teachers ORDER BY last, first"
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



Recent Articles