Sort dynamic list menu

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mrlayance
Forum Commoner
Posts: 31
Joined: Mon Dec 07, 2009 11:53 am

Sort dynamic list menu

Post by mrlayance »

Here is code for building a list from the database culumns. Not sure how to sort each column. I can get one sorted, but I want each list menu sorted.

Code: Select all

function dropdown($field, $table) 
{  
  //initialize variables 
  $oHTML  = ''; 
  $result = ''; 
  
  //check to see if the field is passed correctly 
  if (($field == "")||($table == "")) 
  { 
    die("No column or table specified to create drop down from!"); 
  } 

  $sql = "select distinct($field) from $table"; 
  
  //call the db function and run the query 
  $result = conn($sql); 

  //if no results are found to create a drop down return a textbox 
  if ((!$result) ||(mysql_num_rows($result)==0)) 
  { 
    $oHTML .= "<input type='text' name='$field' value='' size='15'>"; 
  }elseif (($result)&&(mysql_num_rows($result)>0)){ 
    
    //build the select box out of the results 
    $oHTML .= "<select name='$field'>\n<option value='all'>All</option>\n"; 
    while ($rows = mysql_fetch_array($result)) 
    { 
      $oHTML .= "<option value='".$rows[$field]."'>".$rows[$field]."</option>\n"; 
    } 
    $oHTML .= "</select>\n"; 
  } 
  
  //send the value back to the calling code 
  return $oHTML; 
}//end function 
Thanks again for the help.
mrlayance
Forum Commoner
Posts: 31
Joined: Mon Dec 07, 2009 11:53 am

Re: Sort dynamic list menu

Post by mrlayance »

Just had to post the question to figure it out....

Code: Select all

$sql = "select distinct($field) from $table ORDER by $field ASC";
This topic can be deleted if you want.
Post Reply