suttercain Posted March 29, 2007 Share Posted March 29, 2007 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. Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted March 29, 2007 Share Posted March 29, 2007 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 Quote Link to comment Share on other sites More sharing options...
suttercain Posted March 29, 2007 Author Share Posted March 29, 2007 Does MySQL have a NATSORT ? Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted March 29, 2007 Share Posted March 29, 2007 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 = ""; ?> Quote Link to comment Share on other sites More sharing options...
suttercain Posted March 29, 2007 Author Share Posted March 29, 2007 Hi Lolly, I tried the code above and I still got a blank page. Is there an error in the code somewhere, because it looks good to me. Shannon Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted March 29, 2007 Share Posted March 29, 2007 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. Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted March 29, 2007 Share Posted March 29, 2007 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 Quote Link to comment Share on other sites More sharing options...
suttercain Posted March 29, 2007 Author Share Posted March 29, 2007 Hi guys, I tried Post #2 and that seemed to work. Thank you all for the help. Seacrest out! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.