Welcome Guest, Not a member yet? Register   Sign In
Database Insert Question
#1

[eluser]Jesse Schutt[/eluser]
Hello All,

I have a question regarding the input of some information into my database.

Here is the situation. I am registering a user and I want to be able to assign certain privileges to each user. So I am inputting the information like username, email, pw, and then assigning the categories to them.

My db structure is as follows:
admin_members (admin_id, username, screen_name, email, password)
admin_privs (priv_id, priv_title, priv_desc)
admin_priv_link (priv_id, admin_id)

This is the view
Code:
<form action="http://localhost:8888/ci1.7/index.php/admin/new_admin" method="post"><br />
<label for="Username">Username: </label><br />&lt;input type="text" name="username" value=""  /&gt; <br />
<label for="Screen Name">Screen Name: </label><br />&lt;input type="text" name="screen_name" value=""  /&gt; <br />
<label for="Password">Password: </label><br />&lt;input type="password" name="password" value=""  /&gt; <br />
<label for="Confirm Password">Confirm Password: </label><br />&lt;input type="password" name="passwordconf" value=""  /&gt; <br />
<label for="Email">Email: </label><br />&lt;input type="text" name="email" value=""  /&gt; <br />

<h1>Administrative Privileges</h1>
&lt;input type="checkbox" name="1" value="1"  /&gt;&lt;label for="Registration"> Registration</label>
<br />&lt;input type="checkbox" name="2" value="2"  /&gt;&lt;label for="Food Service"> Food Service</label>
<br />&lt;input type="checkbox" name="3" value="3"  /&gt;&lt;label for="Accommodations"> Accommodations</label>
<br />&lt;input type="checkbox" name="4" value="4"  /&gt;&lt;label for="Staffing"> Staffing</label>
<br />&lt;input type="checkbox" name="5" value="5"  /&gt;&lt;label for="Accounting"> Accounting</label>
<br />&lt;input type="checkbox" name="6" value="6"  /&gt;&lt;label for="Media"> Media</label>
<br />&lt;input type="checkbox" name="7" value="7"  /&gt;&lt;label for="Development"> Development</label>
<br />&lt;input type="checkbox" name="8" value="8"  /&gt;&lt;label for="Super Admin"> Super Admin</label>

Can someone help me with the logic that processes the privileges selected and inputs them into the "admin_priv_link" table? I am assuming that this has something to do with a loop...

Thanks in advance!
#2

[eluser]Jesse Schutt[/eluser]
So here is some more of my thoughts towards this

Code:
LOOP SOMETHING()
{            
    $admin_privs = array(
            'admin_id' => '???',
            'priv_id' => '???'
            );
                            
    $this->dashboard_model->new_admin_privs($admin_privs);
}

I am guessing that I will need to loop through each of the privileges selected and input a new record.

Can someone help with what this should look like?

Thanks!
#3

[eluser]Michael Wales[/eluser]
I kind of rushed over this, but you get the general idea.

A few things here - first we're going to change our checkbox's return value to TRUE; since, if a checkbox isn't clicked, it won't be part of the POST array and the Input class will return FALSE.

Secondly, your labels aren't doing anything for your right now as they aren't referencing an ID. We're going to fix that.

I would pull all of the possible permissions down into a variable in my controller and pass it to this view, so I could loop through the various admin privileges:
Code:
<h1>Administrative Priviliges</h1>
&lt;?php foreach ($admin_privs as $a): ?&gt;
  &lt;input type="checkbox" name="&lt;?php echo $a-&gt;priv_id; ?&gt;" id="priv-&lt;?php echo $a->priv_id; ?&gt;" value="TRUE" /><label for="priv-&lt;?php echo $a->priv_id; >">&lt;?php echo $a->priv_title; ?&gt;</label>
&lt;?php endforeach; ?&gt;


Now that our form is all fixed up - let's do the form processor:

Code:
$errors = array();

if ($this->validation->run()) {
  // Setup our user object
  $user = array('username' => $this->input->post('username'),
                'screen_name' => $this->input->post('screen_name'),
                'email' => $this->input->post('email'),
                'passwrd' => $this->input->post('password'));
  // Send it to the model to be created
  if ($this->user->create('user')) {
    $user_id = $this->db->insert_id();
    
    // Get all of our possible privileges
    $admin_privs = $this->privileges->get_privileges();

    // Loop through them all to see if we're giving them that privilege
    if ($admin_privs !== FALSE) {
      foreach ($admin_privs as $a) {
        if ($this->input->post($a->priv_id)) {
          // We are, make a call to the model to insert the data
          if (!$this->user->add_privilege($user_id, $a->priv_id)) {
            // We couldn't add the priv for some reason, save this error
            $errors[] = 'Could not add ' . $a->priv_title . ' privilege.';
          }
        }
      }

      // Check for errors
      if (count($errors) !== 0) {
        show_error('There were errors:<br /><br />' . implode('<br />', $errors));
      }
    }

    // We're done creating this user - go somewhere else
    redirect('user');
    return;
  }
  show_error('Could not create user.');
}


Now, we'll create our user model - not really focusing on create() much since it's not the topic here. Make sure you hash out the password and protect it with a salt or something:
Code:
function create($user) {
  $this->db->insert('users', $user);
  if ($this->db->affected_rows() > 0) {
    return TRUE;
  }
  return FALSE;
}

function add_privilege($user, $priv) {
  $this->db->insert('admin_priv_link', array(
                    'admin_id' => $user,
                    'priv_id' => $priv));
  if ($this->db->affected_rows() > 0) {
    return TRUE;
  }
  return FALSE;
}



Now, the privileges model:
Code:
function get_privileges() {
  $query = $this->db->get('admin_privs');
  if ($query->num_rows() > 0) {
    return $query->result();
  }
  return FALSE;
}
#4

[eluser]Jesse Schutt[/eluser]
Michael!

Terrific! I really appreciate you taking the time to guide me through this. I am learning a ton!

Jesse
#5

[eluser]Jesse Schutt[/eluser]
Michael,

I am getting an error stating that there is an undefined variable called Errors. I assume that is from the line right under

// Check for errors

How can I remedy that?
#6

[eluser]Jesse Schutt[/eluser]
Oops. I missed the

Code:
$errors = array();

at the very top. My bad!




Theme © iAndrew 2016 - Forum software by © MyBB