• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Update multiple users at once?

#1
[eluser]codejack[/eluser]
Hello.

On my site, users can create teams (clans) and invite other users to join them.

I'm working on a page at the moment where team leaders can edit the "ranks" of each user in the team. These options are Founder, Captain, Member, Inactive and Kick (which will remove the user from the team altogether).

I've included a quick screenshot of how the page looks to help explain what I'm hoping to achieve.

http://cl.ly/E9Gb

So team leaders can go through and change the rank of each user and then hit update.

What would be the best way to go about this?

Here are some extracts from my code at present:

CONTROLLER:

Code:
if ($this->input->post('update_roster'))

{
  
  $member_id = $this->input->post('user_id');
  $member_rank = $this->input->post('member_rank');
  $this->clan_model->update_roster($clan_id, $member_id, $member_rank);

}

MODEL:

Code:
public function update_roster($clan_id, $user_id, $rank)

{

  $data = array(
  
           'rank' => $rank
        
        );
        
        $this->db->where('clan_id', $clan_id);
        $this->db->where('user_id', $user_id);

  if ($this->db->update('clan_joined', $data))
  
  {
  
   return TRUE;
  
  }

}

VIEW:

Code:
<h3>Roster</h3>
    
    &lt;?php
    
    $member_rank = array(
  '1' => 'Founder',
  '2' => 'Captain',
  '3' => 'Member',
  '4' => 'Inactive',
  '5' => 'Kick'
    );
    
    ?&gt;

&lt;?php $attributes = array('id' => 'update_roster', 'class' => 'nice'); ?&gt;

&lt;?php echo form_open('clan/manage/' . $this->uri->segment(3) . '#roster', $attributes); ?&gt;

<table width="100%">

<thead>

  <tr>
   <th class="text-center">#</th>
   <th>Username</th>
   <th>Rank</th>
  </tr>
  
</thead>

&lt;?php form_open('clan/manage/' . $this->uri->segment(3) . '#roster', $attributes); ?&gt;
&lt;?php foreach ($members AS $member) : ?&gt;
<tr>
  <td class="text-center"><a href="&lt;?php echo site_url('people') . '/' . $member-&gt;username; ?&gt;">&lt;?php echo avatar(32, $member->username, $member->avatar_uploaded); ?&gt;</a></td>
  <td>&lt;?php echo $info->clan_tag; ?&gt; / &lt;?php echo $member->username; ?&gt;</td>
  <td>&lt;?php echo form_dropdown('member_rank', $member_rank, $member->rank); ?&gt;</td>
</tr>
&lt;?php echo form_hidden('user_id', $member->username); ?&gt;
&lt;?php endforeach; ?&gt;

</table>

&lt;?php echo form_submit('update_roster', 'Update'); ?&gt;
&lt;?php echo form_close(); ?&gt;

Hopefully that all makes sense. I'm new to PHP/CI and I could really use some guidance on this!

Thanks.

#2
[eluser]elverion[/eluser]
I'm not sure, but I think you can set the name of your dropdown to 'member_rank[]', which would then cause $this->input->post('member_rank') to be an array. That'll make it easy to run a for-loop to update each user.

Additionally, if you think you're going to have a lot of members being updated at once and want to limit your SQL queries, you should separate the users into different arrays. Each array will be a list of user IDs for each group, so one array for Founder, one for Captain, and so on. Use this in your query:
Code:
$this->db->where_in('id', $captains);
$this->db->set('member_rank', 'captain');
$this->db->update('clan_members');

$this->db->where_in('id', $members);
$this->db->set('member_rank', 'member');
$this->db->update('clan_members');

This would restrict your query count to the number of different ranks you have (~4), rather than running potentially hundreds of queries.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.