doylefelix
doylefelix

Reputation: 117

How to get a specific array value on a passed array in codeigniter?

I have this controller where all the available users and its respective information are passed in the view through an array:

function view() {

    $data = array();

    if($query = $this->m_user->get_user()) {
        $data['user'] = $query;
    }

    $this->load->view('v_view_user', $data);

    }

In my view I used this method (the norm) to view all that was passed:

<?php echo "user_name here" ?>
<?php if(isset($user)) : foreach ($user as $row) : 
echo $row->user_name;

   end foreach;
end if;
?>

What I want to do is to print a specific index (the name to be specific) before the code given above.

For the model:

function get_employees() {
        $query = $this->db->get('user');
        return $query->result();
    }

By the way, the array contains user_id, user_name, user_family_name, ..., [and other more].

Your help will be highly appreciated.

Upvotes: 1

Views: 169

Answers (1)

Kumar V
Kumar V

Reputation: 8838

$query->result(); will return the array of objects. So you can get user_name as below:

<?php if(isset($user)) : foreach ($user as $row) :

   echo $row->user_name;

   end foreach;
end if;
?>

EDIT: After question updated with my answer

you can use below code to get outside the loop:

echo $user[0]->user_name; // returns the first user_name

Upvotes: 1

Related Questions