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. Link to comment https://forums.phpfreaks.com/topic/44824-using-natsort-on-a-mysql-populated-drop-down-menu/ 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 Link to comment https://forums.phpfreaks.com/topic/44824-using-natsort-on-a-mysql-populated-drop-down-menu/#findComment-217635 Share on other sites More sharing options...
suttercain Posted March 29, 2007 Author Share Posted March 29, 2007 Does MySQL have a NATSORT ? Link to comment https://forums.phpfreaks.com/topic/44824-using-natsort-on-a-mysql-populated-drop-down-menu/#findComment-217641 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 = ""; ?> Link to comment https://forums.phpfreaks.com/topic/44824-using-natsort-on-a-mysql-populated-drop-down-menu/#findComment-217642 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 Link to comment https://forums.phpfreaks.com/topic/44824-using-natsort-on-a-mysql-populated-drop-down-menu/#findComment-217726 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. Link to comment https://forums.phpfreaks.com/topic/44824-using-natsort-on-a-mysql-populated-drop-down-menu/#findComment-217727 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 Link to comment https://forums.phpfreaks.com/topic/44824-using-natsort-on-a-mysql-populated-drop-down-menu/#findComment-217729 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! Link to comment https://forums.phpfreaks.com/topic/44824-using-natsort-on-a-mysql-populated-drop-down-menu/#findComment-217742 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.