1

So I currently have three tables like this given below:

AI = auto increment.

ci_users: user_id(AI), username, slug, email, biography.

ci_terms: term_id(AI), title, slug, body.

ci_relationship: id(AI), term_id, user_id, type.

I'm trying to display all the users from a specific term_id(where type is skill),

for example, let's say term_id 3(where term_id has the title of 'CSS' from ci_terms).

To make it more clear,the above paragraph basically says show me all users with skill of CSS but I want the displayed users to show all of their other skills which are stored in the ci_relationship table.

PARAGRAPHS BELOW ARE AFTER CLICKING A SPECIFIC TERM_ID(SKILL).

This is the function which shows me all of the users that have the term_id as their skill.(from my previous Stackoverflow question. Thanks to pradeep who helped me solve that query:

// All users with specific skill
public function get_user_skill($id)
{

    $this->db->select('*, count(ci_relationship.term_id)');
    $this->db->from($this->table);
    $this->db->join('ci_relationship', 'ci_relationship.user_id = ci_users.id', 'INNER');
    $this->db->order_by('ci_relationship.user_id', 'DESC');
    $this->db->group_by('ci_relationship.user_id');
    $this->db->where('ci_relationship.term_id', $id);
    $this->db->where('ci_relationship.type', 'skill');

    $query = $this->db->get();

    if($query->num_rows() > 0)
    {
        return $query->result();
    } 
    else 
    {
        return false;
    }
}

As I said before the code above just display the users with the specific term_id. Now this is the method in the controller which shows the expected data(users with specific term_id):

This is my controller :

public function skill($id){

    // Get data
    $data['users'] = $this->User_model->get_user_skill($id);
    $term = $this->Terms_model->get($id);

    // Get skill name
    $data['skill'] = $this->Terms_model->get($id);

    // Get skills per foreach --- HERE IS WHERE I NEED HELP
    $data['skills'] = $this->User_model->skills($id);

    // Meta
    $data['title']  = $this->settings->title.' | '. ucfirst($term->title);

    // Load template
    $this->template->load('public', 'default', 'users/skill', $data);
}

This is the method that I'm trying to create to displayed the term_id per user_id:

// Skill for user foreach
public function skills($id){

    $this->db->select('*');
    $this->db->from($this->relationship);
    $this->db->where('term_id', $id);
    $this->db->where('type', 'skill');
    $this->db->order_by('id', 'DESC');

    $query = $this->db->get();

    if($query->num_rows() >= 1){
        return $query->result();
    } else {
        return false;
    }

}

The view is the following code:

<?php if($users) : ?>
    <div class="row">
      <?php foreach($users as $user) : ?>
        <div class="col-lg-3 col-sm-6">
            <div class="card text-center panel">
                <div class="cardheader panel-heading" style="background: url(<?php if($user->user_id) : echo base_url('assets/img/users/covers/'.get_username($user->user_id).'/'.get_username_cover($user->user_id)); else : echo base_url().'assets/img/noavatar.jpg'; endif ?>)no-repeat center center;"></div>
                <div class="avatar">
                    <a target="_self" href="<?= base_url('users/profile/'.get_username($user->user_id)); ?>"><img class="img-circle" alt="<?= get_username($user->user_id); ?> profile picture" title="<?= get_username($user->user_id); ?> profile picture" src="<?php if($user->user_id) : echo base_url('assets/img/users/avatars/'.get_username($user->user_id).'/'.get_username_avatar($user->user_id)); else : echo base_url().'assets/img/noavatar.jpg'; endif ?>"></a>
                </div>
                <div class="info panel-body">
                    <div class="title">
                        <a target="_self" href="<?= base_url('users/profile/'.get_username($user->user_id)); ?>"><?= get_username($user->user_id); ?></a>
                    </div>
                    <div class="desc"><?php if(get_occupation($user->user_id)) : ?><?= get_occupation($user->user_id); ?><?php else : echo 'No Job'; endif ?> <?php if(get_company($user->user_id)) : ?> - <b><?= get_company($user->user_id); ?></b><?php else : echo '- <b>No Company</b>'; endif ?></div>
                    <!-- BEGINNING OF HERE IS WHERE I NEED HELP -->
                    <?php if($skills) : ?>
                        <?php foreach($skills as $skill) : ?>
                            <span class="label label-orange"><?= get_term($skill->term_id) ?></span>
                        <?php endforeach ?>
                    <?php endif ?>
                    <!-- END OF HERE IS WHERE I NEED HELP -->
                </div>
            </div>
        </div>
      <?php endforeach; ?>
    </div>
  <?php else : ?>
    <div class="alert alert-danger">No user with <?= $skill->title ?> skill found</div>
<?php endif; ?>

This is the actual output:

actual output of displayed users with their own skills

And this is the expected output:

expected output of displayed users with their own skills

I hope I could explain myself well enough to make it easy to understand,thanks in advance.

Pradeep
  • 9,667
  • 13
  • 27
  • 34
Kirasiris
  • 523
  • 10
  • 37
  • add `user_id` also in where condition in model's `skills` method – Pradeep Jul 20 '18 at 10:26
  • well I tried this `$this->db->where('user_id, $id);` but it then displays nothing so I decided to just use `$this->db->where('user_id');` without the second parameter and still does not show anything – Kirasiris Jul 20 '18 at 10:39
  • which makes sense as the $id is from the term_id not the user_id – Kirasiris Jul 20 '18 at 10:41
  • ya right ! pass `user_id` in your `skills` method so in turn your `where` clause should be like this : `$this->db->where('user_id, $user_id);` – Pradeep Jul 20 '18 at 10:49
  • it is better to use a `helper` method `get_skill_by_user` and use it in your view just like `get_username` or `get_occupation` – Pradeep Jul 20 '18 at 10:53
  • Yes, I already have the two helper functions, one that is called `get_term()` and I pass the `$skill->term_id` inside the parentheses which end up like this `get_term($skill->term_id)`..... the problem is not that, what I'm having troubles with is in getting the correct data. – Kirasiris Jul 20 '18 at 11:01
  • ya of course correct query is all you want, either pass user id in your model's `skills` method or use a helper method like in my answer , both require correct data – Pradeep Jul 20 '18 at 11:08

1 Answers1

1

Hope this will help you :

Create a helper method get_skill_by_user just like get_username or get_occupation and use it in your view

function get_skill_by_user($user_id, $id)
{
    $ci = & get_instance();
    $ci->db->select('*');
    $ci->db->from('ci_relationship');
    $ci->db->where('term_id', $id);
    $ci->db->where('user_id', $user_id);
    $ci->db->where('type', 'skill');
    $ci->db->order_by('id', 'DESC');

    $query = $ci->db->get();

    if($query->num_rows() > 0)
    {
        return $query->result();
    } else 
    {
        return false;
    }
}

In your view your skills loop should be like this :

<?php $user_skills = get_skill_by_user($user->user_id, $user->term_id); if($user_skills) : ?>
    <?php foreach($user_skills as $skill) : ?>
         <span class="label label-orange"><?= get_term($skill->term_id) ?></span>
    <?php endforeach ?>
<?php endif ?>
Pradeep
  • 9,667
  • 13
  • 27
  • 34
  • If I process like this, the method in the controller will not know where the $user_id is coming from... – Kirasiris Jul 20 '18 at 11:11
  • you have the user_id , term_id in your user's foreach loop , and i think this is all you required to query the data, moreover this is the helper method , there is no need of any controller it will get data without any controller for you, if have trouble with helpers pls read docs – Pradeep Jul 20 '18 at 11:15
  • Could have said that before haha, thanks man, both the answers that you have provide to me are way too helpful and way too important for the app that I'm doing. I love you... seriously I have always have had hard times when doing relationships. – Kirasiris Jul 20 '18 at 11:19
  • pleased to hear these words for me , happy coding – Pradeep Jul 20 '18 at 11:28