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.
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:
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:
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:
<h3>Roster</h3>
<?php
$member_rank = array(
'1' => 'Founder',
'2' => 'Captain',
'3' => 'Member',
'4' => 'Inactive',
'5' => 'Kick'
);
?>
<?php $attributes = array('id' => 'update_roster', 'class' => 'nice'); ?>
<?php echo form_open('clan/manage/' . $this->uri->segment(3) . '#roster', $attributes); ?>
<table width="100%">
<thead>
<tr>
<th class="text-center">#</th>
<th>Username</th>
<th>Rank</th>
</tr>
</thead>
<?php form_open('clan/manage/' . $this->uri->segment(3) . '#roster', $attributes); ?>
<?php foreach ($members AS $member) : ?>
<tr>
<td class="text-center"><a href="<?php echo site_url('people') . '/' . $member->username; ?>"><?php echo avatar(32, $member->username, $member->avatar_uploaded); ?></a></td>
<td><?php echo $info->clan_tag; ?> / <?php echo $member->username; ?></td>
<td><?php echo form_dropdown('member_rank', $member_rank, $member->rank); ?></td>
</tr>
<?php echo form_hidden('user_id', $member->username); ?>
<?php endforeach; ?>
</table>
<?php echo form_submit('update_roster', 'Update'); ?>
<?php echo form_close(); ?>
I understand this code is wrong, as it’s not working, but thought I would share what I have done so far.
On your HTML form change the dropdown and your hidden input to be arrays:
and
Then $this->input->post(‘member_rank’) and ‘user_id’ will each receive an array. You can loop through the arrays in PHP and call the UPDATE code for each iteration of the loop: