在foreach中显示三个具有公共ID的表中的数据

时间:2018-07-20 10:08:12

标签: php codeigniter codeigniter-3 codeigniter-query-builder

因此,我目前有以下三个表格:

AI =自动递增。

ci_users: user_id(AI),用户名,用户名,电子邮件,传记。

ci_terms: term_id(AI),标题,子词,正文。

ci_relationship: id(AI),term_id,user_id,类型。

我正在尝试显示特定term_id(类型为Skill)的所有用户

例如,假设term_id 3(其中term_id在ci_terms中具有“ CSS”的标题)。

为了更加清楚,上面的段落基本上说向我展示了所有具有CSS技能的用户,但我希望显示的用户展示他们存储在ci_relationship表中的所有其他技能。

下面的参数是在单击特定的TERM_ID(技巧)之后。

此功能向我显示所有具有term_id作为其技能的用户。(来自我之前的Stackoverflow问题。感谢 pradeep 帮助我解决了该查询:

// 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;
    }
}

正如我之前说的那样,上面的代码仅显示具有特定term_id的用户。现在,这是控制器中显示所需数据(具有特定term_id的用户)的方法:

这是我的控制器:

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);
}

这是我尝试创建的一种显示每个用户ID的term_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;
    }

}

该视图是以下代码:

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

这是实际输出:

actual output of displayed users with their own skills

这是预期的输出:

expected output of displayed users with their own skills

我希望我能很好地解释自己,以便于理解,

1 个答案:

答案 0 :(得分:1)

希望这对您有帮助:

helperget_skill_by_user一样创建get_username方法get_occupation,并在您的视图中使用它

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;
    }
}

在您看来,您的技能循环应如下所示:

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