使用CodeIgniter从数据库返回String

dav*_*dev 2 php mysql codeigniter

这是我的模特:

function getAvenuName($id) {
        $this->db->distinct();
        $this->db->select('admin.username');
        $this->db->from('personal_closest');
        $this->db->join('admin', 'admin.id = personal_closest.avenu_id', 'left');
        $this->db->where('personal_closest.avenu_id', $id);
        $query = $this->db->get();
        return $query->result();        
    }
Run Code Online (Sandbox Code Playgroud)

这是我的控制器:

foreach ($listofcloset['postByUser'] as $key => $value) { 
            $listofcloset['postByUser'][$key]['calculatetime']   =  $this->calculatetime(strtotime($value['postdatetime']));
            $listofcloset['postByUser'][$key]['countcomments']   =  $this->countcomments($value['id']); 
            $listofcloset['postByUser'][$key]['comments']        =  $this->comments->sltpostcomments($value['personal_closest_id']); 
            $listofcloset['postByUser'][$key]['countCloset']     =  $this->closet->countCloset($value['id']);            
            $listofcloset['postByUser'][$key]['givencool']       =  $this->checkCoolIsGiven($this->session->userdata('user_id'),$value['user_id'],$value['personal_closest_id']);
            $listofcloset['postByUser'][$key]['givencloset']     =  count($this->closet->selectIdbycloset($this->session->userdata('user_id'),$value['user_id'],$value['personal_closest_id']));
            if (empty($listofcloset['postByUser'][$key]['username'])){
                $listofcloset['postByUser'][$key]['username'] = $this->personal_closest->getAvenuName($value['avenu_id']);
            }
        }
Run Code Online (Sandbox Code Playgroud)

当我去使用用户名时,我遇到了数组到字符串转换的问题.我正在尝试找到一个解决方案,只需将String安装在数组中.

这就是结果:

[username] => Array
            (
                [0] => stdClass Object
                    (
                        [username] => USER1
                    )

            )
Run Code Online (Sandbox Code Playgroud)

Jer*_*son 5

这应该得到它:)

function getAvenuName($id) {
    $this->db->distinct();
    $this->db->select('admin.username');
    $this->db->from('personal_closest');
    $this->db->join('admin', 'admin.id = personal_closest.avenu_id', 'left');
    $this->db->where('personal_closest.avenu_id', $id);
    $query = $this->db->get();
    $result = $query->row();
    return $result->username;  
}
Run Code Online (Sandbox Code Playgroud)