Search code examples
phpcodeignitercodeigniter-3codeigniter-query-builder

Display data from three tables with common id in foreach


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.


Solution

  • 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 ?>