首页 > 解决方案 > 有没有将两列数据组合在一个数组中的解决方案?

问题描述

我有一个问题,我想使用数组将数据库中的FirstnameLastname列显示为一个数据。顺便说一句,我是数组代码的新手。我试图浏览任何类似的结果,但无法提出解决方案。任何对我有帮助的事情都会受到高度赞赏。谢谢。

这是我当前的代码:

// DB table to use
$table = 'tbl_employee';

// Table's primary key
$primaryKey = 'id_ref';

// Array of database columns which should be read and sent back to DataTables.
// The `db` parameter represents the column name in the database, while the `dt`
// parameter represents the DataTables column identifier. In this case simple
// indexes

$columns = array(
    array( 'db' => 'emp_id_no', 'dt' => 0 ),

    //this is my problem!! I want to combine this two different column.
    array( 'db' => 'emp_lname' . "" .'db' => 'emp_fname', 'dt' => 1 ),


    array( 'db' => 'emp_dep', 'dt' => 2 ),
    array( 'db' => 'emp_job_name',  'dt' => 3 ),
    array( 'db' => 'emp_status',  'dt' => 4 ),


);

标签: phparrays

解决方案


无需使用数组进行连接emp_lnameemp_fname您可以使用 sql 查询,这样可以加快处理速度并提高代码的可理解性。

 $fullName = SELECT 
    emp_fname,
    emp_lname,
    CONCAT(emp_fname, ' ', emp_lname) full_name
FROM 
    tbl_employee
ORDER BY 
    full_name;

我在这里尝试做的是,从表中获取emp_fnameandemp_lname然后将其分配给一个名为fullName.so 的变量,因此您可以在数组中使用此变量,而不是连接数组中的列。

您可以使用此链接对sql server中的 concat 进行额外参考: http ://www.sqlservertutorial.net/sql-server-string-functions/sql-server-concat-function/

希望这将帮助您解决您的问题。


推荐阅读