admin管理员组文章数量:1328020
I am dumping some results from the database directly into tag in the html, how I can make the first option value to be null, and after that the database results to be sorted? This way I will have this drop down menu an option to not choose anything from it.
this is my code here:
<?php
$select = new Users;
$stmt = $select->selecttema();
while( $row = $stmt->fetch()){
echo '<option value="' . $row['id'] . '">' . $row['topic'] . '</option>';
}
?>
I am dumping some results from the database directly into tag in the html, how I can make the first option value to be null, and after that the database results to be sorted? This way I will have this drop down menu an option to not choose anything from it.
this is my code here:
<?php
$select = new Users;
$stmt = $select->selecttema();
while( $row = $stmt->fetch()){
echo '<option value="' . $row['id'] . '">' . $row['topic'] . '</option>';
}
?>
Share
Improve this question
edited Apr 26, 2015 at 2:44
sfletche
49.8k31 gold badges108 silver badges120 bronze badges
asked Apr 26, 2015 at 2:40
John SinigerJohn Siniger
8852 gold badges17 silver badges41 bronze badges
3 Answers
Reset to default 3Just
echo '<option value="">Select a topic</option>'
before the while loop.
actually Juan Pablo had answered the question. i just add something on his answer. if you add selected in your option it will be first option and you do not need to add data in value
echo '<option selected value="">Select a topic</option>';
To make an empty option:
<select>
<option value="">Select</option>
<?php
$select = new Users;
$stmt = $select->selecttema();
while( $row = $stmt->fetch()){
echo '<option value="' . $row['id'] . '">' . $row['topic'] . '</option>';
}
?>
</option>
You're not clear about what this Users object is or how you're fetching data so I'll just assume you're using a mySQL database, in which case you can use ORDER BY
:
SELECT * FROM `users` WHERE 1 ORDER BY id DESC
For instance the above statement will select all users in the users
database and order them by their id
in descending order.
To sort the <select>
options in JS, take a look at Sort Select Options by Value Attribute Using jQuery and Javascript to sort contents of select element
本文标签: javascriptMake first option value null in select menu coming from MySQL databaseStack Overflow
版权声明:本文标题:javascript - Make first option value null in select menu coming from MySQL database - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742236298a2438142.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论