【问题标题】:sort varchar column codeigniter排序 varchar 列 codeigniter
【发布时间】:2016-01-30 12:01:01
【问题描述】:

我首先检查是否有像我一样的问题,我没有找到任何东西。 都在排序混合了数字数据的字母数字列。

这是我的问题。 我有一个表,其中包含这样的 A 列数据。

WRG-01 WRG-39 WRG-22 WRG-45 WRG-43

需要将其排序为

WRG-01 WRG-22 WRG-39 WRG-43 WRG-45

这是我目前在 codeigniter 框架中使用的代码

$data['products'] = $this->db->order_by('product_id', 'asc')->get('products');

在 mysql 中我可以使用这个查询来完成我的工作

preg_replace("/[^\d]/", "",'product_id'), 'asc')

如何将它应用到我上面的 codeigniter 代码中?

这里是搜索功能

public function search()
{
    $data['title'] = 'Search Product';
    $product_name       = $this->input->get('product_name');
    $product_id         = $this->input->get('product_id');
    $product_category   = $this->input->get('product_category');
    $secondCategory     = $this->input->get('secondCategory');
    $thirdCategory  = $this->input->get('thirdCategory');

$data['category'] = $this->db->order_by('id', 'asc')->get_where('categories', ['parent' => 0]);
if($product_category != '')
{
    $data['secondCategory'] = $this->db->get_where('categories', ['parent' => $product_category]);
}
if($secondCategory != '')
{
    $data['thirdCategory'] = $this->db->get_where('categories', ['parent' => $secondCategory]);
}


if($product_name != '')
    {
        $this->db->like('product_name', $product_name);
    }
    if($product_id != '')
    {
        $this->db->where('product_id', $product_id);
    }
    if($product_category != '')
    {
        $this->db->where('product_category', $product_category);
    }
    if($secondCategory != '')
    {
        $this->db->where('secondCategory', $secondCategory);
    }
    if($thirdCategory != '')
    {
        $this->db->where('thirdCategory', $thirdCategory);
    }



    $data['products'] = $this->db->order_by('product_id' 'asc')->get('products');


    theme('all_product', $data);

}

我不能在这里使用 sql 查询,因为 products 是来自产品表的结果数组。

【问题讨论】:

  • 你试过 $this->db->order_by(preg_replace("/[^\d]/", "",'product_id'), 'asc')->get('products ');
  • @JasbirSinghSohanpal 谢谢。但我添加了完整的搜索功能供您考虑.. 不能在那里直接使用 sql 查询。因为它是包含搜索结果的数据数组。

标签: php mysql codeigniter


【解决方案1】:

使用 MySQL cast

cast(product_id as SIGNED)

cast(product_id as UNSIGNED)

尝试这样的查询:-

select * from products cast(product_id as UNSIGNED) ASC|DESC

【讨论】:

  • 我试过它在 sql 查询上工作并给出正确的结果。但是如何在我的代码上实现它。我在那里提供了完整的搜索功能。
  • $query = $this->db->query(" select * from products cast(product_id as UNSIGNED) ASC")
【解决方案2】:

试试这个

$query= $this->db->query("SELECT * FROM products WHERE ??==?? ORDER BY product_id ASC");
$result= $query->result_array();
return $result;

默认数据将按升序

排序

这是模型。因此,如果您将其传递给控制器​​,它将以 Objective Array 的形式返回数据。

所以在控制器中你可以访问

$result = $this->model_name->method_for_above_code();
$name = $result[0]['name'];
$id = $result[0]['id'];

如果在视图中

$result['this_for_view'] = $this->model_name->method_for_above_code();
foreach ($this_for_view as $new_item) {
    echo "Name is ".$new_item['name'];
    echo "ID is ".$new_item['id'];
}

【讨论】:

  • 谢谢。但还有另一个问题。 products 是结果集数据数组。
  • 它的目标数组。你知道如何访问它吗??
  • @hasan 检查编辑 01
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-09
  • 1970-01-01
  • 2021-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多