0

How do I add an factory to any of my logged in users? I'm using Tank_auth and I made an account 'admin' I also made a table called 'factoryusers'.

I want to add a factory to a specific user using joins.

My table structure:

users
-----
id
username
email
...
...

factories
---------
idfactories
factoryname
adress
...
...

factoryusers
------------
idfactoryusers
idusers
idfactories

I tried to join these tables and show the records from the factoryusers table.

My controller function:

$data['userbedrijf'] = $this->bedrijven_model->bedrijvenusers();
(translation to english: $data['userfactory'] = $this->factory_model->factoryusers();)

My model:

function bedrijvenusers()
{
    $this->db->join('bedrijven', 'bedrijfusers.idbedrijven = bedrijven.idbedrijven');
    $this->db->join('users', 'bedrijfusers.idusers = users.id');
    $result = $this->db->get('bedrijfusers', 1);
    return $result->result();
}

My views:

    <h4>Gebruikergegevens:</h4>
        <p class="field"><label class="field">Gebruikersnaam:</label> <?= $this->tank_auth->get_username(); ?></p>
        <p class="field"><label class="field">Emailadres:</label> <?= $user_data->email; ?></p>
        <p class="field"><label class="field">Laatste Login:</label> <?= date ("d-M-Y H:i:s",strtotime($user_data->last_login)); ?></p>
        <p class="field"><label class="field">Account aangemaakt:</label> <?= date ("d-M-Y H:m:s",strtotime($user_data->created)); ?></p>
        <p class="field"><label class="field">Laatst aangepast:</label> <?= date ("d-M-Y H:i:s",strtotime($user_data->modified)); ?></p>
        <p class="field"><label class="field">is admin:</label> <?= $user_data->is_admin; ?></p>
    <hr>
    <h4>Bedrijfsgegevens:</h4>
    <? foreach($userbedrijf as $row){ ?>
        <p class="field"><label class="field">Bedrijf_id:</label> <?= $row->idbedrijven; ?></p>
        <p class="field"><label class="field">Bedrijfsnaam:</label> <?= $row->Bedrijfsnaam; ?></p>
        <p class="field"><label class="field">Adres:</label> <?= $row->Adres; ?></p>
        <p class="field"><label class="field">Postcode:</label> <?= $row->Postcode; ?></p>
        <p class="field"><label class="field">Plaats:</label> <?= $row->Plaats; ?></p>
        <p class="field"><label class="field">Telefoonnummer:</label> <?= $row->Telefoonnummer; ?></p>
        <p class="field"><label class="field">Website:</label> <?= $row->Website; ?></p>
        <p class="field"><label class="field">Email:</label> <?= $row->Email; ?></p>
        <p class="field"><label class="field">Profiel:</label> <?= $row->Profiel; ?></p>
    <?}?>

What's wrong with my joins? it shows me factories. but for each user the factory is the same. even when i have multiple factoryuser rows in my database with different user id's and factory id's

Kees Sonnema
  • 5,759
  • 6
  • 51
  • 106
  • It's not quite clear, but try using left joins. – Shomz Apr 25 '13 at 13:46
  • when i login as 'admin' my factory name = lauwersdesign. but when i login as 'keessonnema' my factory name is also lauwersdesign. there's no difference between the factories. even when i join. maybe i''m doing it wrong? – Kees Sonnema Apr 25 '13 at 13:48
  • The question isn't quite clear, you are using dutch and englisch together and even i (i am dutch) get confused – This_is_me Apr 25 '13 at 13:48
  • Have you tried getting only the factories for the current user by specifying the userid to the model function and implementing the where clause in the query? – Shomz Apr 25 '13 at 13:49
  • how do you mean that? @Shomz – Kees Sonnema Apr 25 '13 at 13:53
  • @This_is_me it's a bit tricky to explain too. i did the best i could. but it's really simple. the factory is the same for each user even if either the userid or factoryid is different – Kees Sonnema Apr 25 '13 at 13:54
  • well, this way you're getting all the factories for all the users, see my answer – Shomz Apr 25 '13 at 13:54
  • I can't add text to that document @This_is_me – Kees Sonnema Apr 25 '13 at 14:07

1 Answers1

1

The question is not quite clear, but this should get you the factories for the specified user, try it. I kept your original Dutch/English names.

function bedrijvenuser($user_id)
{
    $this->db->join('bedrijven', 'bedrijfusers.idbedrijven = bedrijven.idbedrijven', 'left');
    $this->db->join('users', 'bedrijfusers.idusers = users.id');
    return $this->db->get_where('bedrijfusers', array('idusers' => $user_id))->result();
}
Shomz
  • 37,421
  • 4
  • 57
  • 85