concat在php代码中 [英] concat in php codeigniter

查看:71
本文介绍了concat在php代码中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我理解正确的连接语法。



我有一个名为的库表,其中包含:

  trans_id 
trans_items items - > item_id
trans_user employees - > person_id
trans_date
trans_comment
trans_inventory

,trans_items是items表中的外键,trans_user是employees表中的外键,employee id是people表的外键。



现在我想做什么






p> EDIT ............................................... 。
所以我使用这个代码只显示雇员的姓氏:

  $ this-> ; db-> select('inventory。*,items.name,people.last_name'); 
$ this-> db-> from('inventory');
$ this-> db-> join('items','inventory.trans_items = items.item_id','left');
$ this-> db-> join('people','inventory.trans_user = people.person_id','left');
$ this-> db-> where('deleted',0);
$ this-> db-> order_by('trans_date desc');

使用型号代码:

  foreach($ report_data as $ row)
{
$ tabular_data [] = array($ row ['name'],$ row ['last_name'],$ row ['trans_date'],$ row ['trans_inventory'],$ row ['trans_comment']);
}

但我需要它是名字和姓氏,

  $ this-> db-> select('inventory。*,items.name,CONCAT(people.first_name, ,people.last_name)as employee'); 
$ this-> db-> from('inventory');
$ this-> db-> join('items','inventory.trans_items = items.item_id','left');
$ this-> db-> join('people','inventory.trans_user = people.person_id','left');
$ this-> db-> where('deleted',0);
$ this-> db-> order_by('trans_date desc');

使用型号代码:

  foreach($ report_data as $ row)
{
$ tabular_data [] = array($ row ['name'],$ row ['employee'],$ row ['trans_date'],$ row ['trans_inventory'],$ row ['trans_comment']);
}



如果我使用concat函数,请帮助。

解决方案

您的选择必须是这样的($ this-中的第二个参数 - > db-> select part',FALSE)):

  $ this-> db-> select('inventory。*,items.name ,CONCAT(people.first_name,,people.last_name)as employee',FALSE); 
$ this-> db-> from('inventory');
$ this-> db-> join('items','inventory.trans_items = items.item_id','left');
$ this-> db-> join('people','inventory.trans_user = people.person_id','left');
$ this-> db-> where('deleted',0);
$ this-> db-> order_by('trans_date desc');

从Codeigniter手册报价:


如果将其设置为FALSE,CodeIgniter
将不会尝试保护您的字段或
表格名称与反引号。这是
有用的,如果你需要一个复合选择
语句。



Please help me to understand proper join syntax.

I have table named inventory which has:

trans_id    
trans_items items -> item_id         
trans_user  employees -> person_id           
trans_date                   
trans_comment                
trans_inventory     

As you can see above, trans_items is a foreign key in items table, and trans_user is a foreign key in employees table and employee id is foreign key to people table.

Now what I want to do is to display in HTML the inventory table, but instead of displaying the employee id, I want the employee NAME to be displayed.


EDIT................................................ so i was enable to display only the last name of the employee with this code:

$this->db->select('inventory.*, items.name ,people.last_name');
$this->db->from('inventory');
$this->db->join('items', 'inventory.trans_items = items.item_id' , 'left');
$this->db->join('people', 'inventory.trans_user = people.person_id' , 'left');
$this->db->where('deleted', 0);
$this->db->order_by('trans_date desc');

with the model code:

foreach($report_data as $row)
        {
            $tabular_data[] = array($row['name'], $row['last_name'],$row['trans_date'], $row['trans_inventory'], $row['trans_comment']);
        }

but i need it to be first name and last name so i did these:

$this->db->select('inventory.*, items.name ,CONCAT(people.first_name, " ",people.last_name) as employee');
$this->db->from('inventory');
$this->db->join('items', 'inventory.trans_items = items.item_id' , 'left');
$this->db->join('people', 'inventory.trans_user = people.person_id' , 'left');
$this->db->where('deleted', 0);
$this->db->order_by('trans_date desc');

with the model code:

foreach($report_data as $row)
        {
            $tabular_data[] = array($row['name'], $row['employee'],$row['trans_date'], $row['trans_inventory'], $row['trans_comment']);
        }

it would error if i would use concat function. please help.

解决方案

Your select must be like this (second parameter in $this->db->select('your select part', FALSE) ):

$this->db->select('inventory.*, items.name ,CONCAT(people.first_name, " ",people.last_name) as employee', FALSE);
$this->db->from('inventory');
$this->db->join('items', 'inventory.trans_items = items.item_id' , 'left');
$this->db->join('people', 'inventory.trans_user = people.person_id' , 'left');
$this->db->where('deleted', 0);
$this->db->order_by('trans_date desc');

Quote from Codeigniter manual:

If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks. This is useful if you need a compound select statement.

这篇关于concat在php代码中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆