首页 > 解决方案 > Codeigniter - 返回与特定位置关联的成员

问题描述

我正在构建一个合唱团领队可以运行多个合唱团的软件。

我正在尝试返回与特定合唱团相关的所有成员。因此,当合唱团领袖选择特定的合唱团位置时,他们会看到与该位置关联的所有成员。

我在 MySQL 数据库中有一个 aauth_users 表和一个 choir_locations_to_users 表。

aauth_users 表包含有关每个合唱团成员的所有数据:

用户表

choir_locations_to_users 表是一个单独的表,其中列出了用户 ID 和合唱团位置的 ID。

位置给用户

我正在执行查询以获取显示所有成员所需的必要数据。例如成员姓名、头像等显示在合唱团位置页面上。

函数中的 $id 参数是合唱位置 ID。

public function get_choirlocation_members($id) {

        // $query = 'SELECT aauth_users.full_name, aauth_users.avatar, aauth_users.last_login, choir_location_to_users.location_id FROM aauth_users, choir_location_to_users WHERE aauth_users.id = choir_location_to_users.user_id ';
        $this->db->select('aauth_users.full_name, aauth_users.avatar, aauth_users.last_login, aauth_users.id, choir_location_to_users.location_id');
        $this->db->from('aauth_users, choir_location_to_users');
        $this->db->where('aauth_users.id = choir_location_to_users.user_id');
        $this->db->order_by('aauth_users.last_login', 'DESC');
        $members = $this->db->get();

        foreach ($members->result() as $row) {
            if($row->location_id == $id){
                return $members;
            }
        }

    }

上面的查询返回以下结果: 结果

问题出在查询后的函数中,它似乎返回了所有成员,而不仅仅是与该位置相关的成员。当我在我的视图文件中执行另一个 foreach 时,我仍然看到所有成员。

foreach ($members->result() as $row): ?>
    <li>
        <img class="profile-user-img img-responsive img-circle" src="<?php echo BASE_URL.'uploads/user/'.$row->avatar ?>" alt="User Image">
        <a class="users-list-name" href="#"><?php echo $row->full_name ?></a>
        <span class="users-list-date">Last Login: <?php echo date ( "d.m.Y " , strtotime ($row->last_login  )); ?></span>
    </li>
    <?php endforeach; ?> 

我非常感谢任何帮助指出我在尝试过滤查询结果以仅显示与基于位置 ID 的特定合唱团位置相关联的成员时可能出错的地方。


来自@Dinesh Ali 的更新 Anwser 不在这里工作是我所拥有的:

我测试了您在 phpMyAdmin 中建议的新加入查询,结果如下:

phpmyadmin

如您所见,它在结果表中返回了正确的数据,这很好,所以我添加了您建议的函数,如下所示:

public function get_choirlocation_members($id) {

    $this->db->select('aauth_users.full_name, aauth_users.avatar, aauth_users.last_login, aauth_users.id, choir_location_to_users.location_id');
    $this->db->from('aauth_users');
    $this->db->join('choir_location_to_users', 'aauth_users.id = choir_location_to_users.user_id');
    $this->db->where('aauth_users.id = choir_location_to_users.user_id');
    $this->db->order_by('aauth_users.last_login', 'DESC');
    $members = $this->db->get()->result();



    if(isset($members) && count($members) > 0){
      return $members;      
    }else{
       return false;
    }  

}

然后在我的视图文件中,我使用以下内容将每个用户显示为 LI 列表项:

  foreach ($members as $row): ?>



        <li>
            <img class="profile-user-img img-responsive img-circle" src="<?php echo BASE_URL.'uploads/user/'.$row->avatar ?>" alt="User Image">
            <a class="users-list-name" href="<?php echo BASE_URL.'administrator/user/view/'.$row->id ?>"><?php echo $row->full_name ?></a>
            <span class="users-list-date">Last Login: <?php echo date ( "d.m.Y " , strtotime ($row->last_login  )); ?></span>
        </li>


<?php endforeach; ?>  

但是每个用户仍然显示我有 3 个用户,并且每个位置只有一个用户关联,所以我希望在视图中只看到一个结果?

标签: phpmysqlcodeigniterforeach

解决方案


试试这个,

public function get_choirlocation_members($id) {

        // $query = 'SELECT aauth_users.full_name, aauth_users.avatar, aauth_users.last_login, choir_location_to_users.location_id FROM aauth_users, choir_location_to_users WHERE aauth_users.id = choir_location_to_users.user_id ';
        $this->db->select('aauth_users.full_name, aauth_users.avatar, aauth_users.last_login, aauth_users.id, choir_location_to_users.location_id');
        $this->db->from('aauth_users');
        $this->db->join('choir_location_to_users','aauth_users.id = choir_location_to_users.user_id','inner');
        //$this->db->where('aauth_users.id = choir_location_to_users.user_id');
        $this->db->order_by('aauth_users.last_login', 'DESC');
        $members = $this->db->get();

        foreach ($members->result() as $row) {
            if ($row->location_id == $id) {
                return $members;
            }
        }
    }

推荐阅读