【发布时间】: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