【问题标题】:How can I put a dropdown list in a ascendant?如何将下拉列表置于上升位置?
【发布时间】:2013-11-19 04:43:59
【问题描述】:

我需要把名字放在升序列表中。

这是我的看法……

 <select name="characters" id="characters">
            <option value="">-</option>
            <?php foreach($characters as $character):?>
            <option value="<?php echo $character->id; ?>"><?php echo $character->name; ?></option>
            <?php endforeach; ?>

这是我的模型。我用它来获取数据表 DB。

 public function get_users_by_type ($user_type_tag, $count='', $offset='', $searchvalue='')
{
    if (empty ($user_type_tag))
        return FALSE;

    $name   = !isset ($searchvalue['name']) ? '' : $searchvalue['name'];

    $this->db->select ('users.*, tag, UNIX_TIMESTAMP(joined) AS timestamp, user_types.name AS user_type_name, user_types.modified AS user_type_modified');
    $this->db->from ('users');
    $this->db->join ('user_types', 'users.user_type_id = user_types.id', 'inner');
    $this->db->where ('user_types.tag', $user_type_tag);
    $this->db->limit($count, $offset);
    $this->db->like ('users.name',$name );
    $result = $this->db->get();

【问题讨论】:

  • 如果您从数据库中获取$characters,则基于名称字段。顺便分享更多代码并说明问题所在。
  • 这是此下拉菜单的完整代码。
  • @JonathanPadilla - $characters 来自哪里?
  • @JonathanPadilla - 然后按照 Fallen 的建议 - 从数据库中将 ORDER BY 子句添加到您的 SELECT 中,您可以通过这种方式指定顺序。
  • 我不能使用 sort()???

标签: php html codeigniter


【解决方案1】:

根据您提供的代码,您正在循环遍历对象数组。 sort() 方法将对基本数组进行排序。

php.net sort

对数据进行排序的最有效方法是在从数据库中检索数据时使用 Fallen & andrewsi 推荐的 ORDER BY 子句对其进行排序。

【讨论】:

  • 如何放入 ASC?
猜你喜欢
  • 1970-01-01
  • 2010-12-06
  • 2015-05-21
  • 2015-12-21
  • 1970-01-01
  • 2020-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多