2

This is my Model :

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

This is my Controller :

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

When i go to take the username i've a problem with array to string conversion. I'm trying to found a solution to take just the String instaed the array.

That's the result:

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

            )
1
  • try replacing $query->result() with $query->result_array() Commented Apr 27, 2015 at 14:44

2 Answers 2

5

This should get it :)

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;  
}
Sign up to request clarification or add additional context in comments.

1 Comment

I changed result to $result to avoid confusion for anyone else that may need this. And you're very welcome. I'm glad I could help you!
0

Use following code you will get direct 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();
    return $query->row_array();        
}

1 Comment

This is good but print me always an array and i need a String [username] => Array ( [username] => Mywardrobe ) Infact in the View i got this error: Severity: Notice Message: Array to string conversion

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.