我正在使用CodeIgniter v2.2.4
考虑以下代码导出/下载表示数据库查询结果的CSV文件。
CONTROLLER :
public class AuthProvider {
private static final AWSCredentials credentials = new BasicAWSCredentials("key1", "");
protected AWSCredentials obtainAuth() {
return credentials;
}
MODEL :
public function export_csv($id = NULL)
{
$this->load->dbutil();
$this->load->helper('download');
$query = $this->my_model->create_csv($id); // call the model
$data = $this->dbutil->csv_from_result($query, ',');
force_download( $id . '.csv', $data );
}
上面的代码正常运行,但public function create_csv($id)
{
$this->db->from('mytable');
$this->db->where('id', $id);
$this->db->select('
id AS `ID`,
full_name AS `Full Name`,
company_name AS `Company Name`,
phone AS `Phone Number`,
select_list AS `User Options`
', FALSE);
return $this->db->get();
}
值为select_list
,0
或1
并且已插入CSV导出中。如何将这些值操作为最终CSV文件的更有意义的文本?
示例:
2
答案 0 :(得分:1)
最简单的方法是沿着这些行使用select语句
$this->db->select(
"id AS `ID`,
full_name AS `Full Name`,
company_name AS `Company Name`,
phone AS `Phone Number`,
CASE select_list WHEN = 1 THEN 'foo' WHEN = 2 THEN 'bar' ELSE 'N/A' END
AS `User Options`", FALSE);
取消!这是错误的。
在CASE之后使用可选的expression
时应该如下,即CASE select_list ...
$this->db->select(
"id AS `ID`,
full_name AS `Full Name`,
company_name AS `Company Name`,
phone AS `Phone Number`,
CASE select_list WHEN 1 THEN 'foo' WHEN 2 THEN 'bar' ELSE 'N/A' END
AS `User Options`", FALSE);
我在第一个(不正确的)示例中混合了语句可以使用的不同形式。
答案 1 :(得分:0)
这是对我有用的确切语法......
$this->db->select("
id AS `ID`,
full_name AS `Full Name`,
company_name AS `Company Name`,
phone AS `Phone Number`,
CASE
WHEN select_list = 1 THEN 'foo'
WHEN select_list = 2 THEN 'bar'
ELSE 'N/A'
END AS `User Options`
", FALSE);
这种变化也有效......
$this->db->select("
id AS `ID`,
full_name AS `Full Name`,
company_name AS `Company Name`,
phone AS `Phone Number`,
CASE select_list
WHEN 1 THEN 'foo'
WHEN 2 THEN 'bar'
ELSE 'N/A'
END AS `User Options`
", FALSE);