1

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.

5
  • specific index means you want to display only user_name? Commented Jan 26, 2014 at 15:05
  • @kumar_v indeed, yes. Commented Jan 26, 2014 at 15:07
  • can you add te content of $query in question? Commented Jan 26, 2014 at 15:09
  • @kumar_v updated the question. The array contains user_id, user_name, user_family_name, ..., [and other more]. I need to print the user_name outside the loop. Commented Jan 26, 2014 at 15:14
  • @kumar_v your answer does help, but I updated the question. Hope you can give an answer. Commented Jan 26, 2014 at 15:21

1 Answer 1

1

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

1 Comment

yes you can do like echo $user[0]->user_name; This will print first user name. If you want all, the you have to use loop.

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.