How to enable disable dropdown based on user logged in? - php

i want to disable my dropdown based on the user login.
I store the user type in session.
Now admin creates user. and then that user can login and edit his details.
I want to allow only some fields to be allowed to be edited by that user.
Like i don't want to allow him to edit his user_type.
Now if i remove them from my form field there comes an error on data truncated in mysql. and if i disable it through html it shows same error.
Please help me
the is controller code:
public function edit ($id = NULL)
{
$user_type = $this->session->userdata('user_type');
if($user_type =="admin")
{
if ($id)
{
$this->data['user'] = $this->user_m->get($id);
count($this->data['user']) || $this->data['errors'][] = 'User could not be found';
// Set up the form
$rules = $this->user_m->rules_admin;
$id || $rules['password']['rules'] .= '|required';
$this->form_validation->set_rules($rules);
// Process the form
if ($this->form_validation->run() == TRUE)
{
$data = $this->user_m->array_from_post(array('emp_id','name','last_name','email','password','phone','gender','designation','user_type','blood_group','date_birth','status','address'));
if(!empty($data['password']))
{
$data['password'] = $this->user_m->hash($data['password']);
} else {
// We don't save an empty password
unset($data['password']);
}
$key=$this->user_m->save($data, $id);
redirect('admin/user/index');
}
}
// Load the view
$this->data['subview'] = 'admin/users/edit';
$this->load->view('admin/_layout_main', $this->data);
}
elseif($user_type=="employee")
{
if ($id)
{
$this->data['user'] = $this->user_m->get_emp($id);
count($this->data['user']) || $this->data['errors'][] = 'User could not be found';
$rules = $this->user_m->rules_admin;
$id || $rules['password']['rules'] .= '|required';
$this->form_validation->set_rules($rules);
// Process the form
if ($this->form_validation->run() == TRUE)
{
$data = $this->user_m->array_from_post(array('emp_id','name','last_name','email','password','phone','gender','designation','user_type','blood_group','date_birth','status','address'));
//$data['password'] = $this->user_m->hash($data['password']);
if(!empty($data['password']))
{
$data['password'] = $this->user_m->hash($data['password']);
} else {
// We don't save an empty password
unset($data['password']);
}
$id = $this->session->userdata('id');
$this->user_m->save($data, $id);
redirect('admin/user/index');
}
}
// Load the view
$this->data['subview'] = 'employee/profile/edit';
$this->load->view('employee/_layout_main', $this->data);
}
else
{
echo "You Seem To Be Lost";
}
}
The Edit View
<h3><?php echo empty($user->id) ? '<i class="glyphicon glyphicon-user"></i> Add a User' : 'Edit User ' . $user->name; ?></h3>
<?php echo validation_errors(); ?>
<?php echo form_open(); ?>
<table class="table">
<tr>
<td>Employoee ID</td>
<td><?php echo form_input('emp_id', set_value('emp_id', $user->emp_id)); ?></td>
</tr>
<tr>
<td>Name</td>
<td><?php echo form_input('name', set_value('name', $user->name)); ?></td>
</tr>
<tr>
<td>Last Name</td>
<td><?php echo form_input('last_name', set_value('last_name', $user->last_name)); ?></td>
</tr>
<tr>
<td>Email</td>
<td><?php echo form_input('email', set_value('email', $user->email)); ?></td>
</tr>
<tr>
<td>Password</td>
<td><?php echo form_password('password'); ?></td>
</tr>
<tr>
<td>Phone</td>
<td><?php echo form_input('phone', set_value('phone', $user->phone)); ?></td>
</tr>
<tr>
<td>Gender</td>
<td><?php echo form_dropdown('gender', array('Male' => 'Male', 'Female' => 'Female'), $this->input->post('gender') ? $this->input->post('gender') : $user->gender ); ?></td>
</tr>
<tr>
<td>Designation</td>
<td><?php echo form_input('designation', set_value('designation', $user->designation)); ?></td>
</tr>
<tr>
<td>User Type</td>
<td><?php echo form_dropdown('user_type', array('admin' => 'admin', 'employee' => 'employee','support_staff'=>'support_staff'), $this->input->post('user_type') ? $this->input->post('user_type') : $user->user_type ); ?></td>
</tr>
<tr>
<td>Blood Group</td>
<td><?php echo form_input('blood_group', set_value('blood_group', $user->blood_group)); ?></td>
</tr>
<tr>
<td>Date Of Birth</td>
<td><?php echo form_input('date_birth', set_value('date_birth', $user->date_birth)); ?></td>
</tr>
<tr>
<td>Status</td>
<td><?php echo form_dropdown('status', array('Active' => 'Active', 'Inactive' => 'inactive', 'Delete' => 'delete'), $this->input->post('status') ? $this->input->post('status') : $user->status ); ?></td>
</tr>
<tr>
<td>Address</td>
<td><?php echo form_textarea('address', set_value('address', $user->date_birth)); ?></td>
</tr>
<tr>
<td></td>
<td><?php echo form_submit('submit', 'Save', 'class="btn btn-primary"','onClick="image()"'); ?></td>
</tr>
</table>
<script>
$(function(){
$('#spinnerInput').spinner();
});
</script>
<?php echo form_close();?>

You can apply check for logged in user type.
If (admin) {
// SHOW DROP DOWN
}
else {
// SHOW JUST TEXT (SELECTED ONE FROM DROP DOWN)
// HIDE THE DROP DOWN BOX USING CSS PROPERTY: `display:none`
}

If ($user_type =="admin") {
$select = '';
}
elseif($user_type=="employee"){
$select = 'disabled';
}
// to prevent warning use this<?php isset($select) ? $select : ''; ?>
// Above is shortcode for if(isset($select)){echo $select;}else{echo '';}
<select <?php isset($select) ? $select : ''; ?> >
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>

Related

Join Tables in codeigniter

I have two tables: Company & Users.
I have a form in which i insert Company name and others details. in that same form i have a sub form Sales info and Tech info.
The data i insert in sales and tech info gets stored into users tables.and their id are stored into company table in two fields called sales_id and tech_id.
Now for a view i want to fetch company name its sales person and its tech person,How to do it?
The Code in model:
public function get_company()
{
$this->db->select('*');
$this->db->join('users','users.id = company.sales_id','users.id = company.tech_id');
$this->db->from('company');
$query = $this->db->get();
$result = $query->result();
return $result;
}
IN The View:
<?php if(count($companys)): foreach($companys as $company): ?>
<td><?php echo $company->first_name; ?></td>
how to differentiate which is sales and which is tech person?
the Controller:
public function add_company($id = NULL)
{
$this->data['company'] = $this->company_m->get_new();
$this->data['user'] = $this->user_m->get_new();
$rules = $this->company_m->rules_admin;
$this->form_validation->set_rules($rules);
if ($this->form_validation->run() == TRUE)
{
/*Inserting Sales Person Information*/
$data['first_name'] = $this->input->post('first_name_s');
$data['last_name'] = $this->input->post('last_name_s');
$data['email'] = $this->input->post('email_s');
$data['user_type'] ="sales";
$this->user_m->save($data,$id);
$sales_id = $this->db->insert_id();
/*Inserting Tech Person Information*/
$data_tech =$this->user_m->array_from_post(array('first_name','last_name','email'));
$this->user_m->save($data_tech,$id);
$tech_id = $this->db->insert_id();
/*Insert Company Information*/
$data = $this->company_m->array_from_post(array('org_name','dba','addr1','addr2','city','state','country','pin','sales_id','tech_id','tax_number','comment','url'));
$data['sales_id']= $sales_id;
$data['tech_id']= $tech_id;
$org_id = $this->company_m->save($data, $id);
redirect('admin/company');
}
// Load the view
$this->data['subview'] = 'admin/company/add';
$this->load->view('admin/_layout_main', $this->data);
}
I hope you understood my issue.
The Company table
The Users table
the View code:
<table class="table table-striped ">
<thead>
<tr class="warning">
<th>Organization Name</th>
<th>Sales Person</th>
<th>Tech Person</th>
<th>Tax No</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php if(count($companys)): foreach($companys as $company): ?>
<tr class="active">
<td contenteditable="true" onClick="edit_company(this)" onBlur="save_company(this,'org_name','<?php echo $company->id; ?>')" ><?php echo $company->org_name; ?></td>
<td contenteditable="true" onClick="edit_company(this)" onBlur="save_company(this,'sales_id','<?php echo $company->id; ?>')" ><?php echo $company->first_name; ?></td>
<td contenteditable="true" onClick="edit_company(this)" onBlur="save_company(this,'tech_id','<?php echo $company->id; ?>')" ><?php echo $company->first_name; ?></td>
<td contenteditable="true" onClick="edit_company(this)" onBlur="save_company(this,'tax_number','<?php echo $company->id; ?>')" ><?php echo $company->tax_number; ?></td>
<td><?php echo btn_edit('admin/company/edit/' . $company->id); ?></td>
<td><?php echo btn_delete('admin/company/delete/' . $company->id); ?></td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td colspan="3">We could not find any users.</td>
</tr>
<?php endif; ?>
</tbody>
</table>
In model Use
public function get_company()
{
$this->db->select('*');
$this->db->from('company');
$query = $this->db->get();
$result = $query->result();
return $result;
}
For view :-
<?php if(count($companys)): foreach($companys as $company):
$tech_person = $this->db->get_where("users",array("id"=>$company->tech_id))->row();
$sales_person = $this->db->get_where("users",array("id"=>$company->sales_id))->row();
?>
<tr class="active">
<td contenteditable="true" onClick="edit_company(this)" onBlur="save_company(this,'org_name','<?php echo $company->id; ?>')" ><?php echo $company->org_name; ?></td>
<td contenteditable="true" onClick="edit_company(this)" onBlur="save_company(this,'sales_id','<?php echo $company->id; ?>')" ><?php echo $tech_person->first_name; ?></td>
<td contenteditable="true" onClick="edit_company(this)" onBlur="save_company(this,'tech_id','<?php echo $company->id; ?>')" ><?php echo $sales_person->first_name; ?></td>
<td contenteditable="true" onClick="edit_company(this)" onBlur="save_company(this,'tax_number','<?php echo $company->id; ?>')" ><?php echo $company->tax_number; ?></td>
<td><?php echo btn_edit('admin/company/edit/' . $company->id); ?></td>
<td><?php echo btn_delete('admin/company/delete/' . $company->id); ?></td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td colspan="3">We could not find any users.</td>
</tr>
<?php endif; ?>
</tbody>
In this method you even do not need join to get tech and sales person.
Edited According to your view
At first move the code your have in the controller to a model. If you use a MVC framework try and use it for what it's built, it will help keep everything clean.
So now in the model you should have the following:
function getCompany(){
$query = $this->db->select('users.first_name, company.org_name')
->from('company')
->join('users','users.id = company.sales_id','users.id = company.tech_id')
->get()
->result_array();
return $query;
}
In your view you would do the following:
<?php if(count($companys)): foreach($companys as $company): ?>
<tr>
<td><?php echo $company['first_name']; ?></td>
<td><?php echo $company['org_name']; ?></td>
</tr>
<?php endforeach; endif;?>

Codeigniter inserts partial CSV data into MYSQL only id and date

Hi i am trying to insert data into mysql table by uploading the CSV. But i am getting an error. the upload shows me error and inserts only the id and date. I dont know the possible reasons for it. Firstly i exported the users table from MYSQL to csv format. Now i upload same using my code but does not work Below is my code :
I did Print_r($file_data);
so got this
Array ( [file_name] => users_(2)3.csv [file_type] => text/plain [file_path] => /var/www/Test/uploads/ [full_path] => /var/www/Test/uploads/users_(2)3.csv [raw_name] => users_(2)3 [orig_name] => users_(2).csv [client_name] => users (2).csv [file_ext] => .csv [file_size] => 3.83 [is_image] => [image_width] => [image_height] => [image_type] => [image_size_str] => )
The Controller :
function importcsv() {
$data['users'] = $this->csv_m->get_users();
$data['error'] = ''; //initialize image upload error array to empty
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'csv';
$config['max_size'] = '1000';
$this->load->library('upload', $config);
// If upload failed, display error
if (!$this->upload->do_upload()) {
$data['error'] = $this->upload->display_errors();
print_r($_FILES);
$this->load->view('csv', $data);
} else {
$file_data = $this->upload->data();
$file_path = './uploads/'.$file_data['file_name'];
if ($this->csvimport->get_array($file_path))
{
$csv_array = $this->csvimport->get_array($file_path);
foreach ($csv_array as $row)
{
$insert_data = array(
'id'=>$row['id'],
'sip_id'=>$row['sip_id'],
'sip_pass'=>$row['sip_pass'],
'key'=>$row['key'],
'name'=>$row['name'],
'status'=>$row['status'],
'email'=>$row['email'],
'password'=>$row['password'],
'phone'=>$row['phone'],
'balance'=>$row['balance'],
'created'=>$row['created'],
'modified'=>$row['modified'],
'date_inactive'=>$row['date_inactive'],
'reset_date'=>$row['reset_date'],
);
print_r($file_data);
$this->csv_m->insert_csv($insert_data);
}
$this->session->set_flashdata('success', 'Csv Data Imported Succesfully');
redirect(base_url().'csv');
//echo "<pre>"; print_r($insert_data);
} else
$data['error'] = "Error occured";
$this->load->view('csv', $data);
}
}
The Model :
<?php
class Csv_m extends CI_Model {
function __construct() {
parent::__construct();
}
function get_users() {
$query = $this->db->get('users');
if ($query->num_rows() > 0)
{
return $query->result_array();
}
else
{
echo"Nothing To Show";
return FALSE;
}
}
function insert_csv($data) {
$this->db->insert('users', $data);
}
}
The View :
<?php if (isset($error)): ?>
<div class="alert alert-error"><?php echo $error; ?></div>
<?php endif; ?>
<h2>CI Addressbook Import</h2>
<form method="post" action="<?php echo base_url() ?>csv/importcsv" enctype="multipart/form-data">
<input type="file" name="userfile" ><br><br>
<input type="submit" name="submit" value="UPLOAD" class="btn btn-primary">
</form>
<br><br>
<table class="table table-striped table-hover table-bordered">
<caption>Address Book List</caption>
<thead>
<tr>
<th>Id</th>
<th>SIP Id</th>
<th>SIP Password</th>
<th>Key</th>
<th>Name</th>
<th>Status</th>
<th>Email</th>
<th>Password</th>
<th>Balance</th>
<th>Created</th>
<th>Modified</th>
<th>Date_Inactive</th>
<th>Date Reset</th>
</tr>
</thead>
<tbody>
<?php if ($users == FALSE): ?>
<tr><td colspan="4">There are currently Users</td></tr>
<?php else: ?>
<?php foreach ($users as $row): ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['sip_id']; ?></td>
<td><?php echo $row['sip_pass']; ?></td>
<td><?php echo $row['key']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['status']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['password']; ?></td>
<td><?php echo $row['phone']; ?></td>
<td><?php echo $row['balance']; ?></td>
<td><?php echo $row['created']; ?></td>
<td><?php echo $row['modified']; ?></td>
<td><?php echo $row['date_inactive']; ?></td>
<td><?php echo $row['reset_date']; ?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
Please Review my code and help me solve this issue. When i upload the csv i get an error :
Message: Undefined index: id
Message: Undefined index: sip_id
Message: Undefined index: sip_pass
and all other fields. But some how id and date get inserted in table and other fields dont!
The Error I face :
If your model return false the user $users array is empty. So in view you have you check empty
<?php if (!empty($users)): ?>// check empty array
<tr><td colspan="4">There are currently Users</td></tr>
<?php else: ?>
<?php foreach ($users as $row): ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['sip_id']; ?></td>
<td><?php echo $row['sip_pass']; ?></td>
<td><?php echo $row['key']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['status']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['password']; ?></td>
<td><?php echo $row['phone']; ?></td>
<td><?php echo $row['balance']; ?></td>
<td><?php echo $row['created']; ?></td>
<td><?php echo $row['modified']; ?></td>
<td><?php echo $row['date_inactive']; ?></td>
<td><?php echo $row['reset_date']; ?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
Message: Undefined index: id Message: Undefined index: sip_id Message: Undefined index: sip_pass
this error occurs when variable is not define or no data related with it
Make sure your $users array names are with this names
$row['id'];
$row['sip_id'];
$row['sip_pass'];
$row['key'];
$row['name'];
$row['status'];
$row['email'];
$row['password'];
$row['phone'];
$row['balance'];
$row['created'];
$row['modified'];
$row['date_inactive'];
$row['reset_date'];
and in view table should be
<table class="table table-striped table-hover table-bordered">
<caption>Address Book List</caption>
<thead>
<tr>
<th>Id</th>
<th>SIP Id</th>
<th>SIP Password</th>
<th>Key</th>
<th>Name</th>
<th>Status</th>
<th>Email</th>
<th>Password</th>
<th>Balance</th>
<th>Created</th>
<th>Modified</th>
<th>Date_Inactive</th>
<th>Date Reset</th>
</tr>
</thead>
<tbody>
<?php if (empty($users)) //use empty()
{
?>
<tr><td colspan = "4" > There are currently Users </td ></tr>
<?php
}
else
{
foreach ($users as $row)
{
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['sip_id']; ?></td>
<td><?php echo $row['sip_pass']; ?></td>
<td><?php echo $row['key']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['status']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['password']; ?></td>
<td><?php echo $row['phone']; ?></td>
<td><?php echo $row['balance']; ?></td>
<td><?php echo $row['created']; ?></td>
<td><?php echo $row['modified']; ?></td>
<td><?php echo $row['date_inactive']; ?></td>
<td><?php echo $row['reset_date']; ?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
and in model
function get_users() {
$query = $this->db->get('users');
$result = $query->result_array();
$count = count($result);
if (empty($count))
{
return $result;
}
else
{
}
}

How to create incrementing button in Codeigniter that updates database onclick?

I am new to codeigniter. I have a project where Admin allots balance to its reseller. Now I want to create a button in html which provides interface to admin to increment the balance on button click. and when he increments it the value should be also updated in database. Maybe this is very simple but i couldn't do this! I think I should use ajax or Jquery both for this maybe
Below is my code for view :(want to add increment and decrement button besides the balance input )
<?php echo validation_errors(); ?>
<?php echo form_open(); ?>
<table class="table">
<tr>
<td>SIP Username</td>
<td><?php echo form_input('sip_username', set_value('sip_username', $user->sip_username)); ?></td>
</tr>
<tr>
<td>SIP Password</td>
<td><?php echo form_input('sip_password', set_value('sip_password', $user->sip_password)); ?></td>
</tr>
<tr>
<td>Key</td>
<td><?php echo form_input('key', set_value('key', $user->key), 'readonly'); ?></td>
</tr>
<tr>
<td>Allocation Block</td>
<td><?php echo form_input('allocation_block', set_value('allocation_block', $user->allocation_block)); ?></td>
</tr>
<tr>
<td>Name</td>
<td><?php echo form_input('name', set_value('name', $user->name)); ?></td>
</tr>
<tr>
<td>Reseller Email</td>
<td><?php echo form_input('email', set_value('email', $user->email)); ?></td>
</tr>
<tr>
<td>Password</td>
<td><?php echo form_password('password'); ?></td>
</tr>
<tr>
<td>Confirm password</td>
<td><?php echo form_password('password_confirm'); ?></td>
</tr>
<tr>
<td>User_Required</td>
<td><?php echo form_input('user_num', set_value('user_num', $user->user_num)); ?></td>
</tr>
<tr>
<td>Balance</td>
<td><?php echo form_input('balance', set_value('balance', $user->balance)); ?></td>
</tr>
<tr>
<td>Phone</td>
<td><?php echo form_input('phone', set_value('phone', $user->phone)); ?></td>
</tr>
<tr>
<td>Address</td>
<td><?php echo form_input('address', set_value('address', $user->address)); ?></td>
</tr>
<tr>
<td>Status</td>
<td><?php echo form_dropdown('status', array('Active' => 'Active', 'Inactive' => 'inactive', 'Delete' => 'delete'), $this->input->post('status') ? $this->input->post('status') : $user->status ); ?></td>
</tr>
<tr>
<td>Country</td>
<td><?php echo form_input('country', set_value('country', $user->country)); ?></td>
</tr>
<tr>
<td>Country Code</td>
<td><?php echo form_input('country_code', set_value('country_code', $user->country_code)); ?></td>
</tr>
<tr>
<td></td>
<td><?php echo form_submit('submit', 'Save', 'class="btn btn-primary"'); ?></td>
</tr>
</table>
<?php echo form_close();?>
The Controller :
public function edit ($id = NULL)
{
// Fetch a user or set a new one
if ($id) {
$this->data['user'] = $this->reseller_m->get($id);
count($this->data['user']) || $this->data['errors'][] = 'User could not be found';
}
else {
$this->data['user'] = $this->reseller_m->get_new();
}
// Set up the form
$rules = $this->reseller_m->rules_admin;
$id || $rules['password']['rules'] .= '|required';
$this->form_validation->set_rules($rules);
// Process the form
if ($this->form_validation->run() == TRUE) {
$data = $this->reseller_m->array_from_post(array('sip_username','sip_password','key','allocation_block','name','email','password','phone','balance','user_num','address','country','country_code','created','modified','status'));
$data['password'] = $this->reseller_m->hash($data['password']);
$key=$this->reseller_m->save($data, $id);
//here we get the last inserted record id in $last_id
$last_id = $this->db->insert_id();
//The logic to create blank rows in user table mapped to reseller_id
$values=array($this->input->post('name'),$this->input->post('country_code'),$this->input->post('allocation_block'),$this->input->post('user_num'));
$key=implode('-',$values);
$this->db->where('id',$last_id);
$this->db->update('reseller',array('key'=>$key));
for($i=1; $i<=$data['user_num'];$i++)
{
$userdata=array('key'=>$key);
// here users is taken name of user table with retailer_id is field
$this->db->insert('users',$userdata);
}
redirect('admin/reseller');
}
// Load the view
$this->data['subview'] = 'admin/reseller/edit';
$this->load->view('admin/_layout_main', $this->data);
}
If you are familiar with ajax, you can use ajax for this.
On click of the button you can call ajax and write code in the controller function to update the value in the database.

Update DB row in codeigniter, by id

I'm trying to update a row in DB using ID. I realized you could do it through the URI SEGMENT But there is another way as the variable $ GET. For some reason it fails to update.
Another question, why when I do the URI SEGMENT IF it does not work? Sample code: if ($ this-> uri-> segment (3)) === false) ...
For some reason, the condition does not work. Thank assistants.
the model:
public function updata_user($data) {
$this->db->where('id', $this->uri->segment(3));
$this->db->update('users', $data);
}
}
the controller:
public function save() {
$data = array(
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'password' => $this->hash($this->input->post('password'),TRUE)
);
if ($this->uri->segment(3) === false) {
$this->users_model->insert_user($data);
redirect('users/index');
}
else {
$result = $this->users_model->updata_user($data);
if ($result) {
redirect('users/index');
}
}
}
the view:
<?php echo validation_errors(); ?>
<?php echo form_open('users/save'); ?>
<table class="table">
<tr>
<td>Name</td>
<td><?php echo form_input('name', set_value('name', $user->name)); ?></td>
</tr>
<tr>
<td>Email</td>
<td><?php echo form_input('email', set_value('email', $user->email)); ?></td>
</tr>
<tr>
<td>Email</td>
<td><?php echo form_input('id', set_value('id', $user->id)); ?></td>
</tr>
<tr>
<td>Password</td>
<td><?php echo form_password('password'); ?></td>
</tr>
<tr>
<td>Confirm password</td>
<td><?php echo form_password('password_confirm'); ?></td>
</tr>
<tr>
<td></td>
<td><?php echo form_submit('submit', 'Save', 'class="btn btn-primary"'); ?></td>
</tr>
</table>
<?php echo form_close();?>
You can put id in hidden input and using $this->input->post('id') you can using that in model
View
<tr>
<td>id</td>
<td><?php echo form_hidden('id',$user->id); ?></td>
</tr>
Model
public function updata_user($data)
{
$this->db->where('id', $this->input->post('id'));
$this->db->update('users', $data);
}

cant fetch row data from database after search in search form using codeigniter

hey guys i am new in codeigniter and i am working on an application in which i have database and many records in database..i make a search form in which i make a text field in which user can fill any field value and search records.but here is a small problem..when i fill a name in textbox and click on search button no record display on view . . pls help me . . thanks ..
my controller is:
function search()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('inputsearch', 'Search Your Text Here','required');
if ($this->form_validation->run() == TRUE)
{
$this->load->helper('form');
$this->load->helper('html');
$search_this=$this->input->post('inputsearch');
$this->load->model('mod_user');
$searchmember=$this->mod_user->search_member($search_this);
$data['searchmember'] = $searchmember;
var_dump($searchmember);
$this->load->view('view_home',$data);
$this->load->view('view_homemember',$data);
}
else
{
redirect('ctl_home');
}
}
view is:
<form class="userinfo" action="" method="post">
<table border="1" cellpadding="2" cellspacing="0">
<tr>
<td width="5%;"> </td>
<td width="5%;"> </td>
<td width="15%;">Name</td>
<td width="15%;">Moderator Name</td>
<td width="20%;">KCC Branch</td>
<td width="15%;">Father/Husband Name</td>
<td width="15%;">Address</td>
<td width="10%;">Date</td>
</tr>
<?php foreach ($rows as $row):?>
<tr>
<td><?php echo anchor("Ctl_Home/edit_member/" . $row->member_id, 'Edit'); ?></td>
<td>Delete</td>
<td><?php echo $row->member_name; ?></td>
<td><?php echo $row->moderator_name; ?></td>
<td><?php echo $row->branch_name; ?></td>
<td><?php echo $row->father_name; ?></td>
<td><?php echo $row->address; ?></td>
<td><?php echo $row->date; ?></td>
</tr>
<?php endforeach; ?>
</table>
</form>
my model is:
function search_member($search_this)
{
$query=$this->db->query("SELECT tbl_members.* , tbl_moderators.member_id AS moderator_id, tbl_moderators.member_name AS moderator_name, tbl_moderators.member_no AS moderator_no, tbl_branches.branch_name FROM tbl_members, tbl_members AS tbl_moderators, tbl_branches WHERE tbl_members.moderator_id = tbl_moderators.member_id AND tbl_branches.branch_id = tbl_members.kcc_branch
AND CONCAT(tbl_members.member_name, ' ', tbl_moderators.member_name, ' ',tbl_members.moderator_id, ' ', tbl_moderators.member_id, ' ',tbl_branches.branch_name, ' ',tbl_members.father_name, ' ',tbl_members.address, ' ',tbl_moderators.address, ' ',tbl_members.date, ' ',tbl_moderators.date) like '%".$search_this."%'");
if ($query->num_rows >= 0)
{
foreach($query->result_array() as $row)
{
$data[]=$row;
}
return $data;
}
}
Your loop should have been like this
<?php foreach ($searchmember as $row):?>
<tr>
<td><?php echo anchor("Ctl_Home/edit_member/" . $row->member_id, 'Edit'); ?></td>
<td>Delete</td>
<td><?php echo $row->member_name; ?></td>
<td><?php echo $row->moderator_name; ?></td>
<td><?php echo $row->branch_name; ?></td>
<td><?php echo $row->father_name; ?></td>
<td><?php echo $row->address; ?></td>
<td><?php echo $row->date; ?></td>
</tr>
<?php endforeach; ?>
It is $searchmember as $row instead of $rows as $row in foreach
If $data['searchmember'] contains value in controller,
you can access the varibale in view as $searchmember

Categories