CodeIgniter Forums

Full Version: HELP joining tables...
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[eluser]chazy (aldever calvo)[/eluser]
hello i really need help in joining tables for my get function
i have 3 tables:


user
-----
user_id(pk)
user_type_id(fk)
user_det_id(fk)

user_type
---------
user_type_id(pk)

user_details
------------
user_det_id(pk)


i had it joined and successfully output the result... but the problem is... i cannot do pagination with it...
my past code is this

Code:
function GetAllDetails()
    {
            
        $q = "SELECT A.user_id, A.uname, A.user_type_id, D.user_det_id, D.lname, D.fname, D.mname, D.email, T.user_type_name FROM user as A LEFT JOIN user_details as D ON A.user_id = D.user_det_id LEFT JOIN user_type as T ON A.user_type_id = T.user_type_id";
        $query = $this->db->query($q);
        
        return $query->result();
    }

and so what i did is change it into:
Code:
function GetAllDetails()
    {
        $this->db->join('choy_user_details', 'choy_user.choy_user_id = choy_user_details.choy_user_det_id');
        $this->db->join('choy_user_type', 'choy_user.choy_user_type_id = choy_user_type.choy_user_type_id', 'left');
        $this->db->where('choy_user_id', $userId);
        $query = $this->db->get('choy_user');
        
    
        return $query->result();
    }

But i have errors:

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: userId

Filename: models/user_model.php

Line Number: 45

A PHP Error was encountered

Severity: Warning

Message: preg_match() expects parameter 2 to be string, object given

Filename: database/DB_driver.php

Line Number: 616


Please help me...

El Forum

[eluser]Ochetski[/eluser]
The first error means that the variable $userId is not defined.
I see it here:
$this->db->where('choy_user_id', $userId);

And the second error means that your preg_match function is receiving a wrong parameter, it should be text but you've sent an object, maybe something like $this->input when it must be $this->input->post('test'), check the second parameter of it somewhere.

El Forum

[eluser]chazy (aldever calvo)[/eluser]
the second error about the pregmatch is already solved... the first error which is about the userId... I still can't fix it....

El Forum

[eluser]chazy (aldever calvo)[/eluser]
Code:
function GetAllDetails()
    {
        $this->db->join('user_details', 'user.user_id = user_details.user_det_id');
        $this->db->join('user_type', 'user.user_type_id = user_type.user_type_id', 'left');
        $this->db->where('user_id', $userId);
        $query = $this->db->get('user');
        
    
        return $query->result();
    }

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: userId

Filename: models/user_model.php

Line Number: 45


this is for my controller...
Code:
function index()
{
$data['users'] = $this->user_model->GetAllDetails();
$this->load->view('users/users_index', $data);
}

i can't seem to find a way why i have error Undefined variable: userId
even if i do it this way

$data['users'] = $this->user_model->GetAllDetails($userId);

El Forum

[eluser]Bart Mebane[/eluser]
Did you do this?
Code:
function GetAllDetails($userId)

El Forum

[eluser]chazy (aldever calvo)[/eluser]
[quote author="Bart Mebane" date="1294790748"]Did you do this?
Code:
function GetAllDetails($userId)
[/quote]

yes, i also did that already...

El Forum

[eluser]chazy (aldever calvo)[/eluser]
i have this in my model now
Code:
function GetAllDetails($userId)
    {
        $this->db->join('user_details', 'user.user_id = user_details.user_det_id');
        $this->db->join('user_type', 'user.user_type_id = user_type.user_type_id', 'left');
        $this->db->where('user_id', $userId);
        $query = $this->db->get('user');
        
    
        return $query->result();
    }

and this in my controller

Code:
function index()
{
$data['users'] = $this->user_model->GetAllDetails($userId);    
$this->load->view('users/users_index', $data);
}

now i have new error.. still about my userId

A PHP Error was encountered
Severity: Notice
Message: Undefined variable: userId
Filename: controllers/users.php
Line Number: 97


line 97 is this
$data['users'] = $this->user_model->GetAllDetails($userId);

El Forum

[eluser]Bart Mebane[/eluser]
Where is $userId coming from? Is it in the URL?

El Forum

[eluser]Ochetski[/eluser]
[quote author="Bart Mebane" date="1294792239"]Where is $userId coming from? Is it in the URL?[/quote]

No it's a model, but i guess he's calling this method without the first parameter or it's null.

El Forum

[eluser]chazy (aldever calvo)[/eluser]
i dont have it in my url... this is my url

Code:
<div class="content">
<table>
<tr>
    <td>ID Number</td>
    <td>Details ID Number</td>
    <td>Username</td>
    <td>Account Type</td>
    <td>Last Name</td>
    <td>First Name</td>
    <td>Middle Name</td>
    <td>Email</td>
</tr>
&lt;?php if(isset($users) && is_array($users) && count($users)>0):?&gt;
&lt;?php foreach($users as $user):?&gt;
<tr>
    <td>&lt;?php echo $user->user_id?&gt;</td>
    <td>&lt;?php echo $user->user_det_id?&gt;</td>
    <td>&lt;?php echo $user->uname?&gt;</td>
    <td>&lt;?php echo $user->user_type_name?&gt;</td>
    <td>&lt;?php echo $user->lname?&gt;</td>
    <td>&lt;?php echo $user->fname?&gt;</td>
    <td>&lt;?php echo $user->mname?&gt;</td>
    <td>&lt;?php echo $user->email?&gt;</td>
</tr>
&lt;?php endforeach?&gt;
&lt;?php else:?&gt;
<tr>
<td colspan="3">There are currently no users.</td>
</tr>
&lt;?php endif?&gt;
Pages: 1 2