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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 = "";
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.