1

I am trying to display an array or arrays in my view and I am having difficulties on how to do it, so any help will be much appreciated :)

My array of arrays in the model:

$user = array();
...
$user[$id] = array(
            'id' => $userId[$id],
            'match' => $percentage
        );
return array('userInfo' => $user);

that stores info from some users. I am passing it to the controller and from there to the view where I want to display the information in $user array. In the array can be one or many users.

here is my controller:

$result = $this->model->getUserDetails();

$this->load->view('view', array('user' => $result);

in the view I want to print the info but I do not know how to do it since I have never worked with arrays of arrays and I am a bit confused.

here is the view:

<?php foreach ($user['userInfo'] as $index => $value) {
    echo $value[$index]['id'].' '.$value[$index]['match'];
?>

if I do it this way

or

echo $value['id'];

or

echo $index['id'];

the view displays nothing or doesn't like the 'id' index

this is what I have for

var_dump($result); 

array(1) { ["result"]=> array(1) { [0]=> array(11) { 
["id"]=> string(2) "55" 
["fname"]=> string(6) "Dan" ["lname"]=> string(5) "Re" 
["email"]=> string(18) "[email protected]" ["username"]=> string(6) "dan"    
["gender"]=> string(4) "Male" ["DOB"]=> string(10) "1990-07-13" 
["profile_image"]=> string(8) "dan.jpg" 
["short_des"]=> string(147) "I love singing and playing music" 
["pwd"]=> string(4) "dan" ["confirm_pwd"]=> string(4) "dan" } } } 

meaning that there is one user to be displayed

Pls do help as I am very confused.

Thank you very much.

2
  • 1
    Add the contents of var_dump($result); to your question. Commented Apr 3, 2016 at 20:59
  • 1
    I've updated the question with the result for var_dump($result); Commented Apr 3, 2016 at 21:27

1 Answer 1

1

The problem you are facing is that you are expecting $this->model->getUserDetails() to return a single array with a users details. It is in fact returning the array you care about nested within several other arrays.

Change this line:

$this->load->view('view', array('user' => $result);

To:

$this->load->view('view', array('user' => $result['result'][0]);

And I believe your problem will be solved.

Albeit that line doesn't look so pretty! If you intended your getUserDetails() function to return just a single array about a single user (presumably the current one logged in) then I would recommend changing the return value of that function.

Sign up to request clarification or add additional context in comments.

2 Comments

the thing is that the array $result can hold one or many users, depending on the outcome of the function in model, so I want it to return as many as the function returns, not just one; will the new line work with many users returned?
@Nynaeve The problem then would be how you would define "work." The line will compile and the page will render if you change it to that. The issue becomes the [0] in the line above. If you keep that [0] hard coded, $user will always be whatever row happened to be returned first from the query in the getUserDetails() function.

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.