return 使用 CodeIgniter 来自数据库的字符串

return String from database with 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();        
    }

这是我的控制器

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']);
            }
        }

当我去获取用户名时,我遇到了数组到字符串转换的问题。我正在尝试找到一种解决方案来仅将字符串插入数组。

这就是结果:

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

            )

使用下面的代码你将得到直接数组

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->row_array();        
}

这个应该搞定了:)

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;  
}