Welcome Guest, Not a member yet? Register   Sign In
Avoid duplicate records on join
#1

(This post was last modified: 08-06-2020, 05:01 AM by jreklund.)

Hi coders, i have a table name usersĀ  id, id_role and mooore i have a table named roles id_role, name and more im using codelgniter .
my poblem is that after join both tables im keep on having duplicate recored on display.

im using this code for output usersĀ“s role option :

Code:
<div class="form-group ">
    <label class="control-label" >Roles</label>
    <select class="form-control" name="role_id" >
        <?php foreach ($getroles as $roles): ?>
            <?php if ($roles['id_role'] === $role_id['role_id']): ?>
                <option value="<?php echo $roles['id_role'] ?>" selected><?php echo $roles['name'] ?></option>
            <?php else: ?>
                <option value="<?php echo $roles['id_role'] ?>"><?php echo $roles['name'] ?></option>
            <?php endif; ?>
        <?php endforeach; ?>
    </select>
</div>

// Model

PHP Code:
public function userRoles(){
    
$this->db->select('*, users.id as user_id, roles.id_role as roles_id');
    
$this->db->from('users');
    
//$this->db->where('users.id');
    
$this->db->join('roles''users.role_id= roles.id_role');
    
//$this->db->group_by('users.role_id');
    
return $this->db->get()->result_array();



thanks in advance
Reply
#2

@jose martin mbomio,

So basically you want to get rid of duplicate records in your query. You need to uncomment the group_by line and change 'users.role_id' to 'users.id'. Users can have many roles but there can only be one specific user id.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB