Welcome Guest, Not a member yet? Register   Sign In
Issue with data return
#1

[eluser]jbads[/eluser]
Hi, I may sound stupid here, I probably am. I've been struggling for hour with a function that gets a list of a persons contacts based on their userid and I would like it to retrieve certain data for each contact on that list.

This is my page view controller
eg:
Code:
//Get members contacts
    $data['contacts']        =    $this->_get_member_contacts($user_id);
        
    //Load Page
    $this->load->view('profile/profile_home_body_view', $data);

And this is the function
Code:
protected function _get_member_contacts($id){
    //Get a list of contact id's relating to members list
        $this->db->select('contact_id');
    $this->db->from('jcc_contacts');
    $this->db->where('user_id', $id);
    $query    =    $this->db->get();
    
    //Check there was a result else return false
    if($query->num_rows() > 0)
    {
    //For each result from that query, we need to get the info    
        foreach($query->result_array() as $row){
            
        //Get the data
        $this->db->select('jcc_users.id, jcc_users.username, jcc_user.user_display_name, jcc_user.user_display_email, jcc_user.user_location');
        $this->db->from('jcc_user');
        $this->db->join('jcc_users', 'jcc_users.id = jcc_user.user_id');
        $this->db->where('id', $row['contact_id']);
        $result        =    $this->db->get();
            
        $res    =    $result->row();
                
        //$data['contacts']                    =    $result->result_array();
                
        $data['contacts']['id']                =     $res->id;
        $data['contacts']['username']                =     $res->username;
        $data['contacts']['name']            =     $res->user_display_name;
        $data['contacts']['email']            =     $res->user_display_email;
        $data['contacts']['location']                =     $res->user_location;
                    
        }
        return $data['contacts'];

        }
        else
        {
            return FALSE;
        }
}
This particular version of my code only returns the last row in the DB.
I've tried every combination of result_array() and row() and row_array() etc I can think of. Its driving me insane because I know it's going to be something small that requires doing.

So If anyone can see what I'm trying to do here and can suggest a way of doing it, it would be greatly appreciated.

Cheers. Jake
#2

[eluser]Rick Jolly[/eluser]
Looks like you're overwriting your results.

Replace this:
Code:
$data['contacts']['id']                =     $res->id;
$data['contacts']['username']                =     $res->username;
$data['contacts']['name']            =     $res->user_display_name;
$data['contacts']['email']            =     $res->user_display_email;
$data['contacts']['location']                =     $res->user_location;
                    
}
return $data['contacts'];

With this:
Code:
$contact['id']                =     $res->id;
$contact['username']                =     $res->username;
$contact['name']            =     $res->user_display_name;
$contact['email']            =     $res->user_display_email;
$contact['location']                =     $res->user_location;

$contacts[] = $contact;
}

return $contacts;
#3

[eluser]jbads[/eluser]
Thanks for the reply. I have tried you method and got further than I had before.

My view is now displaying the records from the database after also listing characters from the field names and error messages like this
Quote:A PHP Error was encountered

Severity: Notice

Message: Uninitialized string offset: 0

Filename: profile/profile_home_body_view.php

Line Number: 85
Also error for line number 81 and 84
Code:
<?php if(!empty($contacts))
{
    echo '<div class="list_contacts">';
    echo '<ul>';
    //print_r($contacts);
    foreach($contacts as $row){
    
    
        echo '<li>';
        
        if(!empty($row['name']))
        {
            echo '<a href="../profile/view/'.$row['username'].'">'.$row['name'].'</a><br />';
        }
        else
        {
    LINE 81        echo '<a href="../profile/view/'.$row['username'].'">'.$row['username'].'</a><br />';
        }
        
    LINE 84    echo $row['email'].'<br />';
    LINE 85    echo $row['location'];
        
        echo '</li>';
    }    
    echo '</ul>';
    echo '</div>';
}
And if I print_r the code instead of return the data I get this:
Quote:Array ( [id] => 39 [username] => Tester 6 [name] => [email] => [email protected] [location] => Hastings [0] => Array ( [id] => 35 [username] => test2 [name] => [email] => [email protected] [location] => [0] => Array *RECURSION* ) [1] => Array ( [id] => 38 [username] => Test5 [name] => Fucker [email] => [email protected] [location] => Wellington City [0] => Array ( [id] => 35 [username] => test2 [name] => [email] => [email protected] [location] => [0] => Array *RECURSION* ) [1] => Array *RECURSION* ) [2] => Array *RECURSION* )

Any suggestions?




Theme © iAndrew 2016 - Forum software by © MyBB