Jump to content

Using natsort on a MySQL populated drop down menu


suttercain

Recommended Posts

Hello,

 

I am trying to use the natsort function on a dropdown menu which is populated by the mysql database. I am having no luck :(

 

Here is the original code:

<?php
//Populate the drop down list        
        for ($i = 0; $i < $rows; $i++) 
        {
          $tempmfr = mysql_result($sql_result,$i,'MODEL');
          print "<option value=\"" .$tempmfr ."\">".$tempmfr ."</option>\n";
        }  
        $sql = "";
?>

 

Here is the code I tried but only got a blank page:

<?php
//Populate the drop down list        
        for ($i = 0; $i < $rows; $i++) 
        {
          $tempmfr = mysql_result($sql_result,$i,natsort ('MODEL'));
          print "<option value=\"" .$tempmfr ."\">".$tempmfr ."</option>\n";
        }  
        $sql = "";
?>

Any ideas?

Thanks in advance.

maybe

 

<?php
$results = array();
for ($i = 0; $i < $rows; $i++) 
{
$results[] = mysql_result($sql_result, $i, 'MODEL');
}

natsort($results);

foreach($results as $result)
{
print "<option value=\"" . $result . "\">" . $result . "</option>\n";
}
?>

 

but if I were you I'd just refine my query to do the sorting, it's much more efficient that way

generic's code will work. or you could do this as well.

<?php
//Populate the drop down list        
        while(natsort($row) = mysql_result($sql, 'MODEL')){
                print "<option value=\"". $row ."\">". $row ."</option>\n";
        }  
        $sql = "";
?>

this might be a dumb question, but are you establishing your dropdown menu at all?

<?php
        echo "<select name\"drop_down_menu\">\n";
        //Populate the drop down list
        while(natsort($row) = mysql_result($sql, 'MODEL')){
                print "<option value=\"". $row ."\">". $row ."</option>\n";
        }
        echo "</select>\n";
        $sql = "";
?>

 

also, check the source code and see what it's printing.

while(natsort($row) = mysql_result($sql, 'MODEL')){

 

Will only sort the values in that one particular result row...which doesn't help in this case.

 

Why not use an "ORDER BY" clause on your SQL query and cast the sorted column?

 

http://dev.mysql.com/doc/refman/5.0/en/cast-functions.html

 

Otherwise use genericnumber1's code from post 2

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.