GROUP_CONCAT in CodeIgniter

我是研究僧i 提交于 2021-01-29 07:24:31

问题


I'm just blocking to how create my group_concat with my sql request in CodeIgniter. All my queries are listed in a table, using Jtable library.

All work fine, except when I try to insert GROUP_CONCAT.

Here's my model page :

function list_all()
    {
        $login_id = $this->session->userdata('User_id');
        $this->db->select('p.project_id, p.Project, p.Description, p.Status, p.Thumbnail, t.Template');
        $this->db->from('assigned_projects_ppeople a');
        $this->db->where('people_id', $login_id);
        $this->db->join('projects p', 'p.project_id = a.project_id');
        $this->db->join('project_templates t', 't.template_id = p.template_id');
        $this->db->select('GROUP_CONCAT(u.Asset SEPARATOR ",") as assetslist', FALSE);
        $this->db->from('assigned_assets_pproject b');
        $this->db->join('assets u', 'u.asset_id = b.asset_id');
        $query = $this->db->get();

        $rows = $query->result_array();

        //Return result to jTable
        $jTableResult = array();
        $jTableResult['Result'] = "OK";
        $jTableResult['Records'] = $rows;
        return $jTableResult;
    }

My controller page :

function listRecord(){
        $this->load->model('project_model');
        $result = $this->project_model->list_all();
        print json_encode($result);
    }

And to finish my view page :

<table id="listtable"></table>
        <script type="text/javascript">
            $(document).ready(function () {
            $('#listtable').jtable({
                title: 'Table test',
                actions: {
                    listAction: '<?php echo base_url().'project/listRecord';?>',
                    createAction: '/GettingStarted/CreatePerson',
                    updateAction: '/GettingStarted/UpdatePerson',
                    deleteAction: '/GettingStarted/DeletePerson'
                },
                fields: {
                    project_id: {
                        key: true,
                        list: false
                    },
                    Project: {
                        title: 'Project Name'
                    },
                    Description: {
                        title: 'Description'
                    },
                    Status: {
                        title: 'Status',
                        width: '20px'
                    },
                    Thumbnail: {
                        title: 'Thumbnail',
                        display: function (data) {
                            return '<a href="<?php echo base_url('project');?>/' + data.record.project_id + '"><img class="thumbnail" width="50px" height="50px" src="' + data.record.Thumbnail + '" alt="' + data.record.Thumbnail + '" ></a>';
                        }
                    },
                    Template: {
                        title: 'Template'
                    },
                    Asset: {
                        title: 'Assets'
                    },
                    RecordDate: {
                        title: 'Record date',
                        type: 'date',
                        create: false,
                        edit: false
                    }
                }
            });
            //Load person list from server
            $('#listtable').jtable('load');
        });
        </script>

I read lot of posts talking about that, like replace ',' separator by ",", or use OUTER to the join, or group_by('p.project_id') before using get method, don't work.

Here is a the output of the query in json :

{"Result":"OK","Records":[{"project_id":"1","Project":"Adam & Eve : A Famous Story","Description":"The story about Adam & Eve reviewed in 3D Animation movie !","Status":"wip","Thumbnail":"http:\/\/localhost\/assets\/images\/thumb\/projectAdamAndEve.png","Template":"Animation Movie","assetslist":"Apple, Adam, Eve, Garden of Eden"}]} 

We can see the GROUP_CONCAT is here (after "assetslist"), but the column stills empty.

If asked, I can post the database SQL file.


回答1:


Can you please try this (Edited):

$this->db->select('GROUP_CONCAT(u.Asset) AS assetslist');




回答2:


So simple, under my nose but I didn't see it... GROUP_CONCAT uses an alias name for the new coresponding column (as "assetslist"). I had to use this name, and not the default name of my non concatened table column "Asset" in the Jtable config :

<table id="listtable"></table>
        <script type="text/javascript">
            $(document).ready(function () {
            $('#listtable').jtable({
                title: 'Table test',
                actions: {
                    listAction: '<?php echo base_url().'project/listRecord';?>',
                    createAction: '/GettingStarted/CreatePerson',
                    updateAction: '/GettingStarted/UpdatePerson',
                    deleteAction: '/GettingStarted/DeletePerson'
                },
                fields: {
                    project_id: {
                        key: true,
                        list: false
                    },
                    Project: {
                        title: 'Project Name'
                    },
                    Description: {
                        title: 'Description'
                    },
                    Status: {
                        title: 'Status',
                        width: '20px'
                    },
                    Thumbnail: {
                        title: 'Thumbnail',
                        display: function (data) {
                            return '<a href="<?php echo base_url('project');?>/' + data.record.project_id + '"><img class="thumbnail" width="50px" height="50px" src="' + data.record.Thumbnail + '" alt="' + data.record.Thumbnail + '" ></a>';
                        }
                    },
                    Template: {
                        title: 'Template'
                    },
                    assetslist: {
                        title: 'Assets'
                    },
                    RecordDate: {
                        title: 'Record date',
                        type: 'date',
                        create: false,
                        edit: false
                    }
                }
            });
            //Load person list from server
            $('#listtable').jtable('load');
        });
        </script>

thank you !



来源:https://stackoverflow.com/questions/19871330/group-concat-in-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!