【问题标题】:How to create an alphabetically sorted array out of the column in MySQL DB and pass it to the view?如何从 MySQL DB 的列中创建按字母顺序排序的数组并将其传递给视图?
【发布时间】:2012-08-08 15:01:15
【问题描述】:

我正在使用 CodeIgniter,基本上是一个 PHP 菜鸟。 所以我在我的数据库中有这张名为“商店”的表。 我想要一个按字母排序的数组,其中的元素对应于表存储中的“标题”列。

目前我似乎想不出任何可能的解决方案!

有什么帮助吗??

我用的是linux/ubuntu...

PS:我在 PHP 手册中看到 sqlsrv 扩展只能在 Windows 平台上使用 (http://www.php.net/manual/en/sqlsrv.requirements.php) 是否没有 Linux 替代方案?

【问题讨论】:

    标签: php mysql codeigniter


    【解决方案1】:

    控制器:

    $res['output']=$this->modelname->get();
    $this->load->view('viewname',$res);
    

    型号:

    function get()
    {
    $query=$this->db->query("select * from table order by title asc");
    $output=$query->result();
    return $output;
    }
    

    查看:

    <?php
    foreach($output as $obj)
    {
    echo $obj->blah;
    }
    ?>
    

    【讨论】:

      【解决方案2】:

      您可以在 MySQL 查询中使用ORDER BY 子句对记录进行排序:

      SELECT *
      FROM stores.table_name
      ORDER BY title ASC;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-06-24
        • 1970-01-01
        • 2021-06-25
        • 1970-01-01
        相关资源
        最近更新 更多