How to bind and output data to codeigniter 3 - php

I have two tables in the database, one stores a list of departments (id and name), and the other a list of doctors, and each doctor is attached to the department, while in the table of doctors, the doctor is attached to the department by department id.
My task is to display a list of doctors in a bootstrap table and so that the department field displays not the department ID but the name of the department, how can I do it?
view
<!-- Table with stripped rows -->
<table class="table datatable" id="example">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Name</th>
<th scope="col">Email</th>
<th scope="col">Department</th>
<th scope="col">Phone</th>
</tr>
</thead>
<tbody>
<?php $i = 1; ?>
<?php foreach ($doctors_list as $dl) : ?>
<tr>
<th scope="row"><?= $i; ?></th>
<td><?= $dl['name']; ?></td>
<td><?= $dl['email']; ?></td>
<td><?=$dl['department']; ?> </td>
<td><?= $dl['phone']; ?></td>
</tr>
<?php $i++; ?>
<?php endforeach; ?>
</tbody>
</table>
controller
public function doctors_list()
{
$data['title'] = 'Dashboard';
$data['user'] = $this->db->get_where('user', ['email' => $this->session->userdata('email')])->row_array();
$data['doctors_list'] = $this->db->get_where('user', ['role_id' => 2])->result_array();
$data['department_list'] = $this->db->get('departments')->result_array();
$this->form_validation->set_rules('name', 'Name', 'required|trim');
$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email|is_unique[user.email]', [
'is_unique' => 'This email has already registered!'
]);
$this->form_validation->set_rules('password1', 'Password', 'required|trim|min_length[3]|matches[password2]', [
'matches' => 'Password dont match!',
'min_length' => 'Password too short!'
]);
$this->form_validation->set_rules('password2', 'Password', 'required|trim|matches[password1]');
if ($this->form_validation->run() == false) {
$this->load->view('templates/header', $data);
$this->load->view('templates/sidebar_admin', $data);
$this->load->view('admin/doctors_list', $data);
$this->load->view('templates/footer');
} else {
$email = $this->input->post('email', true);
$role_id = $this->input->post('role_id', true);
$department = $this->input->post('department', true);
$data = [
'name' => htmlspecialchars($this->input->post('name', true)),
'email' => htmlspecialchars($email),
'image' => 'default.jpg',
'password' => password_hash($this->input->post('password1'), PASSWORD_DEFAULT),
'role_id' => htmlspecialchars($role_id),
'department' => htmlspecialchars($department),
'phone' => htmlspecialchars($this->input->post('phone', true)),
'is_active' => 1,
'date_created' => time()
];
$this->db->insert('user', $data);
$this->session->set_flashdata('message', '<div class="alert alert-success" role="alert">Congratulation! your account has been created. Please activate your account </div>');
redirect('admin/doctors_list');
}
i am trying to do so
<?=$department_list[$dl['department']]; ?>
but i get an error

After the fix
Controller
public function doctors_list()
{
$data['title'] = 'Dashboard';
$data['user'] = $this->db->get_where('user', ['email' => $this->session->userdata('email')])->row_array();
$data['doctors_list'] = $this->db->get_where('user', ['role_id' => 2])->result_array();
$departments = $this->db->get('departments')->result_array();
$data['department_list'] = array_combine(array_column($departments, 'id'),array_column($departments, 'dep_name') );
$this->form_validation->set_rules('name', 'Name', 'required|trim');
$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email|is_unique[user.email]', [
'is_unique' => 'This email has already registered!'
]);
$this->form_validation->set_rules('password1', 'Password', 'required|trim|min_length[3]|matches[password2]', [
'matches' => 'Password dont match!',
'min_length' => 'Password too short!'
]);
$this->form_validation->set_rules('password2', 'Password', 'required|trim|matches[password1]');
if ($this->form_validation->run() == false) {
$this->load->view('templates/header', $data);
$this->load->view('templates/sidebar_admin', $data);
$this->load->view('admin/doctors_list', $data);
$this->load->view('templates/footer');
} else {
$email = $this->input->post('email', true);
$role_id = $this->input->post('role_id', true);
$department = $this->input->post('department', true);
$data = [
'name' => htmlspecialchars($this->input->post('name', true)),
'email' => htmlspecialchars($email),
'image' => 'default.jpg',
'password' => password_hash($this->input->post('password1'), PASSWORD_DEFAULT),
'role_id' => htmlspecialchars($role_id),
'department' => htmlspecialchars($department),
'phone' => htmlspecialchars($this->input->post('phone', true)),
'is_active' => 1,
'date_created' => time()
];
$this->db->insert('user', $data);
$this->session->set_flashdata('message', '<div class="alert alert-success" role="alert">Congratulation! your account has been created. Please activate your account </div>');
redirect('admin/doctors_list');
}
}
And View
<tr>
<th scope="row"><?= $i; ?></th>
<td><?= $dl['name']; ?></td>
<td><?= $dl['email']; ?></td>
<td><?= $department_list[$dl['department']]; ?> </td>
<td><?= $dl['phone']; ?></td>
<td>
<a type="button" class="btn btn-success btn-sm" data-bs-toggle="modal" data-bs-target="#editmodal<?= $dl['id']; ?>"><i class="bi bi-pencil-square"></i></a>
<button type="button" class="btn btn-danger btn-sm" data-bs-toggle="modal" data-bs-target="#delmodal<?=$dl['id']?>">
<i class="bi bi-trash"></i>
</button>
</td>
</tr>
View of the site before fixing
enter image description here
and after fix
enter image description here

Related

Data does not save and not displaying any error - Codeigniter

The data doesn't save without prompting any errors in my code
Here is my code in my Controller
function saveDebit(){
$jevseries=$this->input->post('series');
$account='debit';
$count='1';
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->form_validation->set_rules('accountName', 'Account Name', 'required');
$this->form_validation->set_rules('amountDebit', 'Amount', 'required|numeric');
$this->formValidation();
if ($this->form_validation->run() == FALSE) {
$this->entryAccount($jevseries);
} else {
$debitData = array(
'Series' => $jevseries,
'AccountCode' => $this->input->post('accountName'),
'Account' => $account,
'Amount' => $this->input->post('amountDebit'),
'Count' => $count);
$this->Jev_model->entryDebit($debitData);
}
}
Model
function entryDebit($debitData){
$this->db->insert('generalaccount', $debitData);
}
This is my View
<?php echo form_open('Jev/saveDebit'); ?>
<table class="jev_entry" id="jev_entry">
<tr>
<td>Debit</td>
<?php echo form_hidden('series', $series);?>
<td><?php
$account_array = array();
foreach($accountNames as $account ){
$account_array []= $account->accountName;
}
echo form_dropdown('accountName', $account_array,'', 'class="form-control"');
?></td>
</tr>
<tr>
<td>Amount</td>
<td><?php echo form_input('amountDebit','', 'class="form-control"');?><div class="error"><?php echo form_error('amountDebit'); ?></div></td>
</tr>
<tr>
<td></td>
<td align="center"><?php echo '<center>'. form_submit('submit','Save').'</center>'; ?> </td>
</tr>
</table>
I don't know where is the error in this lines.. Your help is very much appreciated.
Okay i have found out what is wrong, The form_input of codeigniter only gives the form with a label. It does not give a name therefore we cannot post it since it doesn't have a name. Do this :)
$data = array(
'name' => 'username',
'id' => 'username',
'value' => 'johndoe',
'maxlength' => '100',
'size' => '50',
'style' => 'width:50%'
);
echo form_input($data);
It's like that and we post the name. Of course you can remove some parameters. More info in the form helper user guide in codeigniter :)
Godluck meyt

Fatal error: Call to undefined method User_m::get() in C:\xampp\htdocs\cms\application\controllers\admin\user.php on line 18

when i open the link 'http://127.0.0.1/cms/index.php/admin/user/edit/1'
this is my user_m model
<?php
class User_m extends CI_Model{
protected $_table_name = 'users';
protected $_order_by = 'name';
public $rules = array(
'email' => array(
'field' => 'email',
'label' => 'Email',
'rules' => 'trim|required|valid_email|xss_clean'
),
'password' => array(
'field' => 'password',
'label' => 'Password',
'rules' => 'trim|required'
)
);
public $rules_admin = array(
'name' => array(
'field' => 'name',
'label' => 'Name',
'rules' => 'trim|required|valid_email|xss_clean'
),
'order' => array(
'field' => 'order',
'label' => 'Order',
'rules' => 'trim|is_natural'
),
'email' => array(
'field' => 'email',
'label' => 'Email',
'rules' => 'trim|required|valid_email|callback__unique_email|xss_clean'
),
'password' => array(
'field' => 'password',
'label' => 'Password',
'rules' => 'trim|matches[password_confirm]'
),
'password_confirm' => array(
'field' => 'password',
'label' => 'Password',
'rules' => 'trim|matches[password]'
)
);
public function __construct()
{
parent::__construct();
}
public function login()
{
$user = $this->db->get_where('users', array(
'email' => $this->input->post('email'),
'password' =>$this->hash($this->input->post('password'))
), TRUE);
if (count($user)) {
$data = array(
'name' => $user->name,
'email' => $user->email,
'id' => $user->id,
'loggedin' => TRUE,
);
$this->session->set_userdata($data);
}
}
public function logout()
{
$this->session->sess_destroy();
}
public function loggedin()
{
return (bool) $this->session->userdata('loggedin');
}
public function hash($string)
{
return hash('sha512', $string . config_item('encryption_key'));
}
}
this is my user controller
<?php
class User extends Admin_Controller{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->data['users'] = $this->db->get_where('users');
$this->data['subview'] = 'admin/user/index';
$this->load->view('admin/_layout_main', $this->data);
}
public function edit($id=NULL)
{
$id == NULL || $this->data['user'] = $this->user_m->get($id);
$this->data['subview'] = 'admin/user/edit';
$this->load->view('admin/_layout_main', $this->data);
}
public function delete($id)
{
}
public function login()
{
$dashboard = 'admin/dashboard';
$this->user_m->loggedin() == FALSE || redirect($dashboard);
$rules = $this->user_m->rules;
$this->form_validation->set_rules($rules);
if ($this->form_validation->run()==true) {
if ($this->user_m->login() == TRUE) {
redirect($dashboard);
}else{
$this->session->set_flashdata('error', 'That email and password combination does not exit');
redirect('admin/user/login', 'refresh');
}
}
$this->data['subview'] = 'admin/user/login';
$this->load->view('admin/_layout_modal', $this->data);
}
public function logout()
{
$this->user_m->logout();
redirect('admin/user/login');
}
}
this is my edit file.
<div class="modal-header">
<h3><?php echo empty($user->id) ? 'Add a new user' : 'Edit user' . $user->name; ?></h3>
</div>
<div class="modal-body">
<?php echo '<pre>' . print_r($this->session->userdata, TRUE) . '</pre>'; ?>
<?php echo validation_errors(); ?>
<?php echo form_open(); ?>
<table class="table">
<tr>
<td>Email</td>
<td><?php echo form_input('email'); ?></td>
</tr>
<tr>
<td>Password</td>
<td><?php echo form_password('password'); ?></td>
</tr>
<tr>
<td></td>
<td><?php echo form_submit('submit', 'Log in', 'class="btn btn-primary"'); ?></td>
</tr>
</table>
<?php echo form_close(); ?>
</div>
this is my index file
<section>
<h2>Users</h2>
<?php echo anchor('admin/user/edit', '<i class="icon-plus"></i> Add a user'); ?>
<table class="table table-striped">
<thead>
<tr>
<th>Email</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php if(count($users)): foreach ($users->result() as $user): ?>
<tr>
<td><?php echo anchor('admin/user/edit/' . $user->id, $user->email); ?><td>
<td><?php echo btn_edit('admin/user/edit/'. $user->id); ?><td>
<td><?php echo btn_delete('admin/user/delete/' . $user->id); ?><td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td colspan="3">We could not find any user</td>
</tr>
<?php endif; ?>
</tbody>
</table>
</section>
In your parent construct area on controller try loading model that way all the function in that controller have access to the model else you can load model in the edit function
I can see there is no get function in model also.
public function __construct()
{
parent::__construct();
$this->load->model('user_m'); // Filename should be User_m.php same as class
}
Or
public function edit($id=NULL)
{
$this->load->model('user_m'); // Filename should be User_m.php same as class
$id == NULL || $this->data['user'] = $this->user_m->get($id);
$this->data['subview'] = 'admin/user/edit';
$this->load->view('admin/_layout_main', $this->data);
}
Also I see your IP address is loaded in your url. If your using CI3 versions in the config.php you must set this value.
$config['base_url'] = '';
To
$config['base_url'] = 'http://localhost/your_project_name/';

Pass a variable from controller to view when using form_open

I am very new to codeigniter and I was wondering if someone can help me. I want to pass a variable from my controller to my view, but the way I am do it it always give a undefined variable error. Also, if I keep the load->view call give an error too!
So, how can I pass the variable to the view? Thanks in advance to any help!
<?php <!-- view -->
echo form_open('match/setDefafultEmail', array(
'class' => '',
));
?>
<div class="well">
<div class="text-center">
<h4>Set Default Email</h4>
</div>
<?php
echo("<div>");
echo form_label('Email:');
echo form_input(array(
'id' => 'email_address',
'name' => 'email_address',
'type' => 'email',
'placeholder' => 'email#example.com',
'value' => set_value('email_address'),
'required' => '',
'title' => 'Email address'
));
echo("</div>");
echo("<div>");
echo form_submit(array(
'id' => 'btn',
'name' => 'min',
'type' => 'Submit',
'class' => 'btn btn-info',
'value' => 'Set Default Email'
));
echo("</div>");
echo form_close()
?>
<br>
<br>
<table class="table" >
<thead>
<tr>
<th>Name</th>
<th>Default Email</th>
</tr>
</thead>
<tr class="info">
<!--display current default name and email-->
<?php foreach($info as $row):?>
<td> <?php echo $row->full_name ?> </td>
<td> <?php echo $row->email ?> </td>
<?php endforeach;?>
</tr>
</table>
public function setDefafultEmail(){
$this->load->library('form_validation');
$this->form_validation->set_rules('email_address', 'Email Address', 'valid_email');
if ($this->form_validation->run( ) == true){
$name = $this->input->post('full_name');
$default_email = $this->input->post('email_address');
$this->spw_vm_request_model->setEmailToDefault($name,$default_email);
}
$data['info'] = $this->request_model->getDefaultEmailAndName();
$this->load->view('admin/admin_dashboard',$data);
redirect('admin/admin_dashboard');
}
1) After you load the view you don't need to redirect. When you redirect('admin/admin_dashboard') you actually call the admin_dashboard Controller.
2) On your View you are trying to access an array as an object( $row->full_name ) I think you should use $row['full_name'].
3) In case your Model returns an object, you have to serialize it in your Controller and unserialize it in your View.

Error updating the user settings in ionauth : "This form post did not pass our security checks."

I am using Ionauth library in codeigniter and edited edit_user() method in Auth controller to enable individual user updating his/her own user settings. So when a logged in user goes to : siteurl/auth/edit_user it shows the user settings just fine. But when I hit the save button I got an error: "This form post did not pass our security checks". Though the default url (siteurl/auth/edit_user/userID) works fine, For individual non-admin user I want to keep the url without userID at the end.
here is my edit_user() method:
//edit a user
function edit_user($id=NULL)
{
$this->data['title'] = "Edit User";
if (!$this->ion_auth->logged_in() || (!$this->ion_auth->is_admin() && !($this->ion_auth->user()->row()->id == $id) && !($id==NULL )))
//if (!$this->ionauth->logged_in() || !$this->ion_auth->is_admin())
{
redirect('auth', 'refresh');
}
if($id==NULL){
$user = $this->ion_auth->user()->row();
}else{
$user = $this->ion_auth->user($id)->row();
}
$groups=$this->ion_auth->groups()->result_array();
$currentGroups = $this->ion_auth->get_users_groups($id)->result();
//process the phone number
/**if (isset($user->phone) && !empty($user->phone))
{
$user->phone = explode('-', $user->phone);
} **/
//validate form input
$this->form_validation->set_rules('first_name', $this->lang->line('edit_user_validation_fname_label'), 'required|xss_clean');
$this->form_validation->set_rules('last_name', $this->lang->line('edit_user_validation_lname_label'), 'required|xss_clean');
if(!($this->input->post('email')==$user->email)){
$this->form_validation->set_rules('email', $this->lang->line('edit_user_validation_email_label'), 'required|valid_email|is_unique[users.email]');
}else{
$this->form_validation->set_rules('email', $this->lang->line('edit_user_validation_email_label'), 'required|valid_email');
}
/** $this->form_validation->set_rules('phone2', $this->lang->line('edit_user_validation_phone2_label'), 'required|xss_clean|min_length[3]|max_length[3]');
$this->form_validation->set_rules('phone3', $this->lang->line('edit_user_validation_phone3_label'), 'required|xss_clean|min_length[4]|max_length[4]');
$this->form_validation->set_rules('company', $this->lang->line('edit_user_validation_company_label'), 'required|xss_clean'); **/
$this->form_validation->set_rules('groups', $this->lang->line('edit_user_validation_groups_label'), 'xss_clean');
//$this->form_validation->set_message('is_unique[users.email]','Email already exists or Invalid');
if (isset($_POST) && !empty($_POST))
{
// do we have a valid request?
if ($this->_valid_csrf_nonce() === FALSE || $id != $this->input->post('id'))
{
show_error($this->lang->line('error_csrf'));
}
$data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'email' => $this->input->post('email'),
/** 'phone' => $this->input->post('phone1') . '-' . $this->input->post('phone2') . '-' . $this->input->post('phone3'), **/
);
//if($this->ion_auth->is_admin()){
//Update the groups user belongs to
$groupData = $this->input->post('groups');
if (isset($groupData) && !empty($groupData)) {
$this->ion_auth->remove_from_group('', $id);
foreach ($groupData as $grp) {
$this->ion_auth->add_to_group($grp, $id);
}
}
//}
//update the password if it was posted
if ($this->input->post('password'))
{
$this->form_validation->set_rules('password', $this->lang->line('edit_user_validation_password_label'), 'required|min_length[' . $this->config->item('min_password_length', 'ion_auth') . ']|max_length[' . $this->config->item('max_password_length', 'ion_auth') . ']|matches[password_confirm]');
$this->form_validation->set_rules('password_confirm', $this->lang->line('edit_user_validation_password_confirm_label'), 'required');
$data['password'] = $this->input->post('password');
}
if ($this->form_validation->run() === TRUE)
{
$this->ion_auth->update($user->id, $data);
//check to see if we are creating the user
//redirect them back to the admin page
$this->session->set_flashdata('message', "User Saved");
redirect("auth", 'refresh');
}
}
//display the edit user form
$this->data['csrf'] = $this->_get_csrf_nonce();
//set the flash data error message if there is one
$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));
//pass the user to the view
$this->data['user'] = $user;
//if($this->ion_auth->is_admin()){
$this->data['groups'] = $groups;
$this->data['currentGroups'] = $currentGroups;
//}
$this->data['first_name'] = array(
'name' => 'first_name',
'id' => 'first_name',
'type' => 'text',
'value' => $this->form_validation->set_value('first_name', $user->first_name),
);
$this->data['last_name'] = array(
'name' => 'last_name',
'id' => 'last_name',
'type' => 'text',
'value' => $this->form_validation->set_value('last_name', $user->last_name),
);
$this->data['email'] = array(
'name' => 'email',
'id' => 'email',
'type' => 'text',
'value' => $this->form_validation->set_value('email', $user->email),
);
/** $this->data['phone1'] = array(
'name' => 'phone1',
'id' => 'phone1',
'type' => 'text',
'value' => $this->form_validation->set_value('phone1', $user->phone[0]),
);
$this->data['phone2'] = array(
'name' => 'phone2',
'id' => 'phone2',
'type' => 'text',
'value' => $this->form_validation->set_value('phone2', $user->phone[1]),
);
$this->data['phone3'] = array(
'name' => 'phone3',
'id' => 'phone3',
'type' => 'text',
'value' => $this->form_validation->set_value('phone3', $user->phone[2]),
); **/
$this->data['password'] = array(
'name' => 'password',
'id' => 'password',
'type' => 'password'
);
$this->data['password_confirm'] = array(
'name' => 'password_confirm',
'id' => 'password_confirm',
'type' => 'password'
);
$this->load->view('header');
$this->_render_page('auth/edit_user', $this->data);
$this->load->view('footer');
}
and this is my view file (edit_user.php):
<h1><?php echo lang('edit_user_heading');?></h1>
<div id="body">
<p><?php echo lang('edit_user_subheading');?></p>
<div id="infoMessage"><?php echo $message;?></div>
<?php echo form_open(uri_string());?>
<p>
<?php echo lang('edit_user_fname_label', 'first_name');?> <br />
<?php echo form_input($first_name);?>
</p>
<p>
<?php echo lang('edit_user_lname_label', 'last_name');?> <br />
<?php echo form_input($last_name);?>
</p>
<p>
<?php echo lang('edit_user_email_label', 'email');?> <br />
<?php echo form_input($email);?>
</p>
<!--
<p>
<?php echo lang('edit_user_phone_label', 'phone');?> <br />
<?php echo form_input($phone1);?>-<?php echo form_input($phone2);?>-<?php echo form_input($phone3);?>
</p>
-->
<p>
<?php echo lang('edit_user_password_label', 'password');?> <br />
<?php echo form_input($password);?>
</p>
<p>
<?php echo lang('edit_user_password_confirm_label', 'password_confirm');?><br />
<?php echo form_input($password_confirm);?>
</p>
<?php //if($this->ion_auth->is_admin()){ ?>
<h3><?php echo lang('edit_user_groups_heading');?></h3>
<?php foreach ($groups as $group):?>
<label class="checkbox">
<?php
$gID=$group['id'];
$checked = null;
$item = null;
foreach($currentGroups as $grp) {
if ($gID == $grp->id) {
$checked= ' checked="checked"';
break;
}
}
?>
<input type="checkbox" name="groups[]" value="<?php echo $group['id'];?>"<?php echo $checked;?>>
<?php echo $group['name'];?>
</label>
<?php endforeach?>
<?php //} ?>
<?php echo form_hidden('id', $user->id);?>
<?php echo form_hidden($csrf); ?>
<p><?php echo form_submit('submit', lang('edit_user_submit_btn'));?></p>
<?php echo form_close();?>
The csrf check is failing.
Try taking out $id = NULL in the method declaration (you don't need it anyway if you're sending the id via POST). Or explicitly set $id = $this->input->post('id'); before doing the csrf check.
In my case I was using relative URLs for the images and css files used in the site. Using base_url() to all the URLs present in the site fixed the problem. No issue now.

CodeIgniter: Tank Auth, Adding Date of Birth Issues

Ok, So I have added a Date of Birth section for the registration process. I have done it in a weird way probably but it is a logical way to do it in my mind so as long as it works I am fine with it. I have created 3 dob fields (dob1, dob2, dob3 - for month, day, year). The problem I have right now is when a user registers the dob values are not stored properly into the database (it seems the values are just random, they show up as random numbers and letters). So I am curious as to what I am doing wrong. Here is what I have done.
EDIT: I have found the problem, but still am looking for a solution. The ordering is mixed up somewhere. My first 2 letters of email go into dob1, first 2 letters of password go into dob2 (dont know if pass or confirm pass), dob1 goes to firstname, dob2 goes to lastnam, username is fine, dob3 goes to email. So I have mixed up the ordering of my array somewhere but I am nut sure where it counts.
EDIT2: I have found my issue. Answer is below.
Register function in controllers/auth.php
function register()
{
if ($this->tank_auth->is_logged_in()) { // logged in
redirect('');
} elseif ($this->tank_auth->is_logged_in(FALSE)) { // logged in, not activated
redirect('/auth/send_again/');
} elseif (!$this->config->item('allow_registration', 'tank_auth')) { // registration is off
$this->_show_message($this->lang->line('auth_message_registration_disabled'));
} else {
$use_username = $this->config->item('use_username', 'tank_auth');
if ($use_username) {
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|min_length['.$this->config->item('username_min_length', 'tank_auth').']|max_length['.$this->config->item('username_max_le\
ngth', 'tank_auth').']|alpha_dash');
}
$this->form_validation->set_rules('dob1', 'DOB1', 'trim|required|xss_clean|min_length[2]|max_length[2]');
$this->form_validation->set_rules('dob2', 'DOB2', 'trim|required|xss_clean|min_length[2]|max_length[2]');
$this->form_validation->set_rules('dob3', 'DOB3', 'trim|required|xss_clean|min_length[2]|max_length[4]');
$this->form_validation->set_rules('firstname', 'First Name', 'trim|xss_clean|min_length[2]|max_length[50]');
$this->form_validation->set_rules('lastname', 'Last Name', 'trim|xss_clean|min_length[2]|max_length[50]');
$this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean|valid_email');
$this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean|valid_email|callback_is_email_domain[ku.edu]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|min_length['.$this->config->item('password_min_length', 'tank_auth').']|max_length['.$this->config->item('password_max_length', '\
tank_auth').']|alpha_dash');
$this->form_validation->set_rules('confirm_password', 'Confirm Password', 'trim|required|xss_clean|matches[password]');
$captcha_registration = $this->config->item('captcha_registration', 'tank_auth');
$use_recaptcha = $this->config->item('use_recaptcha', 'tank_auth');
if ($captcha_registration) {
if ($use_recaptcha) {
$this->form_validation->set_rules('recaptcha_response_field', 'Confirmation Code', 'trim|xss_clean|required|callback__check_recaptcha');
} else {
$this->form_validation->set_rules('captcha', 'Confirmation Code', 'trim|xss_clean|required|callback__check_captcha');
}
}
$data['errors'] = array();
$email_activation = $this->config->item('email_activation', 'tank_auth');
if ($this->form_validation->run()) { // validation ok
if (!is_null($data = $this->tank_auth->create_user(
$use_username ? $this->form_validation->set_value('username') : '',
$this->form_validation->set_value('dob1'),
$this->form_validation->set_value('dob2'),
$this->form_validation->set_value('dob3'),
$this->form_validation->set_value('firstname'),
$this->form_validation->set_value('lastname'),
$this->form_validation->set_value('email'),
$this->form_validation->set_value('password'),
$email_activation))) { // success
$data['site_name'] = $this->config->item('website_name', 'tank_auth');
$email_activation = $this->config->item('email_activation', 'tank_auth');
if ($this->form_validation->run()) { // validation ok
if (!is_null($data = $this->tank_auth->create_user(
$use_username ? $this->form_validation->set_value('username') : '',
$this->form_validation->set_value('dob1'),
$this->form_validation->set_value('dob2'),
$this->form_validation->set_value('dob3'),
$this->form_validation->set_value('firstname'),
$this->form_validation->set_value('lastname'),
$this->form_validation->set_value('email'),
$this->form_validation->set_value('password'),
$email_activation))) { // success
$data['site_name'] = $this->config->item('website_name', 'tank_auth');
if ($email_activation) { // send "activate" email
$data['activation_period'] = $this->config->item('email_activation_expire', 'tank_auth') / 3600;
$this->_send_email('activate', $data['email'], $data);
unset($data['password']); // Clear password (just for any case)
$this->_show_message($this->lang->line('auth_message_registration_completed_1'));
} else {
if ($this->config->item('email_account_details', 'tank_auth')) { // send "welcome" email
$this->_send_email('welcome', $data['email'], $data);
}
unset($data['password']); // Clear password (just for any case)
$this->_show_message($this->lang->line('auth_message_registration_completed_2').' '.anchor('/auth/login/', 'Login'));
}
} else {
$errors = $this->tank_auth->get_error_message();
foreach ($errors as $k => $v) $data['errors'][$k] = $this->lang->line($v);
}
}
if ($captcha_registration) {
if ($use_recaptcha) {
$data['recaptcha_html'] = $this->_create_recaptcha();
} else {
$data['captcha_html'] = $this->_create_captcha();
}
}
$data['use_username'] = $use_username;
$data['captcha_registration'] = $captcha_registration;
$data['use_recaptcha'] = $use_recaptcha;
$this->load->view('auth/register_form', $data);
}
}
Libraries: Tank_Auth.php - I know this is not the whole dob, but calling this function returns nothing, i figure if I can return 1 dob then i can easily return them all. EDIT: I just realized I did not include all of my changes for this file.
else {
$this->ci->session->set_userdata(array(
'user_id' => $user->id,
'dob1' => $user->dob1,
'dob2' => $user->dob2,
'dob3' => $user->dob3,
'firstname' => $user->firstname,
'lastname' => $user->lastname,
'username' => $user->username,
'status' => ($user->activated == 1) ? STATUS_ACTIVATED : STATUS_NOT_ACTIVATED,
));
.
.
.
function get_dob()
{
return $this->ci->session->userdata('dob1');
}
.
.
.
function create_user($username, $firstname, $lastname, $email, $password, $email_activation, $dob1, $dob2, $dob3)
{
if ((strlen($username) > 0) AND !$this->ci->users->is_username_available($username)) {
$this->error = array('username' => 'auth_username_in_use');
$hashed_password = $hasher->HashPassword($password);
$data = array(
'firstname' => $firstname,
'lastname' => $lastname,
'dob1' => $dob1,
'dob2' => $dob2,
'dob3' => $dob3,
'username' => $username,
'password' => $hashed_password,
'email' => $email,
Views: registration_form.php
<?php
if ($use_username) {
$username = array(
'name' => 'username',
'id' => 'username',
'value' => set_value('username'),
'maxlength' => $this->config->item('username_max_length', 'tank_auth'),
'size' => 30,
);
}
$email = array(
'name' => 'email',
'id' => 'email',
'value' => set_value('email'),
'maxlength' => 80,
'size' => 30,
);
$firstname = array(
'name' => 'firstname',
'id' => 'firstname',
'value' => set_value('firstname'),
'maxlength' => 50,
'size' => 30,
);
$lastname = array(
'name' => 'lastname',
'id' => 'lastname',
'value' => set_value('lastname'),
'maxlength' => 50,
'size' => 30,
);
$password = array(
'name' => 'password',
'id' => 'password',
'value' => set_value('password'),
'maxlength' => $this->config->item('password_max_length', 'tank_auth'),
'size' => 30,
);
$confirm_password = array(
'name' => 'confirm_password',
'id' => 'confirm_password',
'value' => set_value('confirm_password'),
'maxlength' => $this->config->item('password_max_length', 'tank_auth'),
'size' => 30,
);
$dob1 = array(
'name' => 'dob1',
'id' => 'dob1',
'value' => set_value('dob1'),
'maxlength' => 2,
'size' => 30,
);
$dob2 = array(
'name' => 'dob2',
'id' => 'dob2',
'value' => set_value('dob2'),
'maxlength' => 2,
'size' => 30,
);
$dob3 = array(
'name' => 'dob3',
'id' => 'dob3',
'value' => set_value('dob3'),
'maxlength' => 4,
'size' => 30,
);
$captcha = array(
'name' => 'captcha',
'id' => 'captcha',
'maxlength' => 8,
);
?>
<?php echo form_open($this->uri->uri_string()); ?>
<table>
<?php if ($use_username) { ?>
<tr>
<td><?php echo form_label('Username', $username['id']); ?></td>
<td><?php echo form_input($username); ?></td>
<td style="color: red;"><?php echo form_error($username['name']); ?><?php echo isset($errors[$username['name']])?$errors[$username['name']]:''; ?></td>
</tr>
<?php } ?>
<tr>
<td><?php echo form_label('Email Address', $email['id']); ?></td>
<td><?php echo form_input($email); ?></td>
<td style="color: red;"><?php echo form_error($email['name']); ?><?php echo isset($errors[$email['name']])?$errors[$email['name']]:''; ?></td>
</tr>
<tr>
<td><?php echo form_label('First Name', $firstname['id']); ?></td>
<td><?php echo form_input($firstname); ?></td>
<td style="color: red;"><?php echo form_error($firstname['name']); ?><?php echo isset($errors[$firstname['name']])?$errors[$firstname['name']]:''; ?></td>
</tr>
<tr>
<td><?php echo form_label('Last Name', $lastname['id']); ?></td>
<td><?php echo form_input($lastname); ?></td>
<td style="color: red;"><?php echo form_error($lastname['name']); ?><?php echo isset($errors[$lastname['name']])?$errors[$lastname['name']]:''; ?></td>
</tr>
<tr>
<td><?php echo form_label('Password', $password['id']); ?></td>
<td><?php echo form_password($password); ?></td>
<td style="color: red;"><?php echo form_error($password['name']); ?></td>
</tr>
<tr>
<td><?php echo form_label('Confirm Password', $confirm_password['id']); ?></td>
<td><?php echo form_password($confirm_password); ?></td>
<td style="color: red;"><?php echo form_error($confirm_password['name']); ?></td>
</tr>
<tr>
<td><?php echo form_label('Date of Birth', $dob1['id']); ?></td>
<td><?php echo form_input($dob1); ?></td>
<td><?php echo form_input($dob2); ?></td>
<td><?php echo form_input($dob3); ?></td>
<td style="color: red;"><?php echo form_error($dob1['name']); ?><?php echo isset($errors[$dob1['name']])?$errors[$dob1['name']]:''; ?></td>
</tr>
<?php if ($captcha_registration) {
if ($use_recaptcha) { ?>
<tr>
<td colspan="2">
<div id="recaptcha_image"></div>
</td>
<td>
Get another CAPTCHA
<div class="recaptcha_only_if_image">Get an audio CAPTCHA</div>
<div class="recaptcha_only_if_audio">Get an image CAPTCHA</div>
</td>
</tr>
<tr>
<td>
<div class="recaptcha_only_if_image">Enter the words above</div>
<div class="recaptcha_only_if_audio">Enter the numbers you hear</div>
</td>
<td><input type="text" id="recaptcha_response_field" name="recaptcha_response_field" /></td>
<td style="color: red;"><?php echo form_error('recaptcha_response_field'); ?></td>
<?php echo $recaptcha_html; ?>
</tr>
<?php } else { ?>
<tr>
<td colspan="3">
<p>Enter the code exactly as it appears:</p>
<?php echo $captcha_html; ?>
</td>
</tr>
<tr>
<td><?php echo form_label('Confirmation Code', $captcha['id']); ?></td>
<td><?php echo form_input($captcha); ?></td>
<td style="color: red;"><?php echo form_error($captcha['name']); ?></td>
</tr>
<?php }
} ?>
</table>
<?php echo form_submit('register', 'Register'); ?>
<?php echo form_close(); ?>
**ALSO: If you know how to change the width of the td tag for the registration_form.php then please let me know. I am struggling with that also the simple does not work.
I found where I messed up. I was passing the fields in the wrong order when I called the create_user() function in auth.php. I when into the Tank_Auth.php file and reordered my parameters for that function to match what I was passing in the auth.php.
function create_user($username, $firstname, $lastname, $email, $password, $email_activation, $dob1, $dob2, $dob3)
changed to
function create_user($username, $dob1, $dob2, $dob3, $firstname, $lastname, $email, $password, $email_activation)
This matched the create_user() call i had in the auth.php:
if (!is_null($data = $this->tank_auth->create_user(
$use_username ? $this->form_validation->set_value('username') : '',
$this->form_validation->set_value('dob1'),
$this->form_validation->set_value('dob2'),
$this->form_validation->set_value('dob3'),
$this->form_validation->set_value('firstname'),
$this->form_validation->set_value('lastname'),
$this->form_validation->set_value('email'),
$this->form_validation->set_value('password'),
$email_activation)))

Categories