Update DB row in codeigniter, by id - php

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);
}

Related

Diffrent query in one function codeigniter

I use Codeigneter and I make a search function and in my function I want run two diffrent query and I want show them in diffrent table, How I can do that Sorry I can't show the data
This is My code:
My controller
function showDetail(){
// Retrieve the posted search term.
$detailTiket = $this->input->get('ticket_id');
// Use a model to retrieve the results.
$data["result"]= $this->tracking_model->showDetail($detailTiket);
$data1["result"]= $this->tracking_model->showDetail2($detailTiket);
// Pass the results to the view.
$this->load->view('tracking/tiket_detail',$data,$data1);
}
My Model
function showDetail($detailTiket)
{
if($detailTiket==""){
$detailTiket = "";
}
$showDetailTiket=$this->db->query("My Query1");
return $detailTiketDown->result();
}
function showDetail2($detailTiket)
{
if($detailTiket==""){
$detailTiket = "";
}
$detailTiketDown=$this->db->query("My query2");
return $detailTiketDown->result();
}
My view
<table Width='800'>
<?php
foreach($data as $row){?>
<tbody>
<tr>
<td><?php echo $row->ticket_id; ?></td>
</tr>
<tr>
<td><?php echo $row->created_time; ?></td>
</tr>
<tr>
<td><?php echo $row->start_IT; ?></td>
</tr>
<tr>
<td><?php echo $row->estimasi_selesai; ?></td>
</tr>
<tr>
<td><?php echo $row->name; ?></td>
</tr>
<tr>
<td><?php echo $row->description; ?></td>
</tr>
<tr style="background-color: cyan">
<td><b><?php echo $row->Status; ?></b></td>
</tr>
</tbody>
<?php } ?>
</table>
</center>
</div>
<div>
<table Width='1000'>
<?php foreach($data1 as $rows){ ?>
<tbody>
<tr>
<td><?php echo $rows->Tgl_Waktu; ?></td>
<td><?php echo $rows->PIC; ?></td>
<td><?php echo $rows->Tracking_Ticket; ?></td>
<td><?php echo $rows->Keterangan_Ticket; ?></td>
<td><?php echo $rows->File_Pendukung; ?></td>
</tr>
</tbody>
<?php }?>
</table>
You can pass data like Associative array to view
change your controller like this
Controller:
$data["result"]= $this->tracking_model->showDetail($detailTiket);
$data["result1"]= $this->tracking_model->showDetail2($detailTiket);
// Pass the results to the view.
$this->load->view('tracking/tiket_detail',$data);
view:
Table1 :
foreach($result as $row)
{
//for first result
}
Table2:
foreach($result1 as $row1)
{
//for second result
}

Codeigniter-returning value to output in table with foreach

I am using Codeigniter, and I want to return some information for an advisor. For some reason, the model isn't returning anything from the database (I'm assuming since I placed text throughout the table in view and nothing came up). The query calls information from two different tables, and I used the query directly in the database to make sure it works.
At this point, I'm sure that it is something small that I'm missing. Any insight is appreciated.
Model
function profileInfo($user_id, $user_type)
{
if($user_type == 'advisor')
{
$this->db->select('user_fullname, CWID, user_name, user_email, user_phone, major, office_loc');
$this->db->from('users, advisor');
$this->db->where('users.user_id', $user_id);
$this->db->where('users.user_id = advisor.user_id');
$query = $this->db->get();
return $query->result();
}
elseif($user_type == 'advisee')
{
$this->db->select('user_fullname, CWID, user_name, user_email, user_phone, major, classification');
$this->db->from('users, advisee');
$this->db->where('users.user_id', $user_id);
$this->db->where('users.user_id = advisee.user_id');
$query = $this->db->get();
return $query->result();
}
else
{
$this->db->select('user_fullname, CWID, user_name, user_email, user_phone');
$this->db->from('users');
$this->db->where('user_id', $user_id);
$query = $this->db->get();
return $query->result();
}
}
Controller
function profilePage()
{
$CWID = $this->session->userdata('id');
$userType = $this->session->userdata('user_type');
$data = array('view' => 'viewProfile', 'userid' => $CWID, 'usertype' => $userType, 'user_info' => $this->Users_model->profileInfo($CWID, $userType));
$this->load->view('admin', $data);
}
View
<?php if($usertype =='advisor'): ?>
<table>
<?php foreach ($user_info as $row): ?>
<tr>
<th>Name: </th>
<td><?php echo $row->user_fullname?></td>
</tr>
<tr>
<th>CWID: </th>
<td><?php echo $row->CWID ?></td>
</tr>
<tr>
<th>User Name: </th>
<td><?php echo $row->user_name ?></td>
</tr>
<tr>
<th>Email: </th>
<td><?php echo $row->user_email ?></td>
</tr>
<tr>
<th>Phone: </th>
<td><?php echo $row->user_phone ?></td>
</tr>
<tr>
<th>Major: </th>
<td><?php echo $row->major ?></td>
</tr>
<tr>
<th>Office Location: </th>
<td><?php echo $row->office_loc ?></td>
</tr>
<?php endforeach ?>
</table>
<!--Provides view for a user that is an advisee-->
<?php elseif($usertype =='advisee'): ?>
<table>
<?php foreach ($user_info as $row): ?>
<tr>
<th>Name: </th>
<td><?php echo $row->user_fullname ?></td>
</tr>
<tr>
<th>CWID: </th>
<td><?php echo $row->CWID ?></td>
</tr>
<tr>
<th>User Name: </th>
<td><?php echo $row->user_name ?></td>
</tr>
<tr>
<th>Email: </th>
<td><?php echo $row->user_email ?></td>
</tr>
<tr>
<th>Phone: </th>
<td><?php echo $row->user_phone ?></td>
</tr>
<tr>
<th>Major: </th>
<td><?php echo $row->major ?></td>
</tr>
<tr>
<th>Classification: </th>
<td><?php echo $row->classification ?></td>
</tr>
<?php endforeach ?>
</table>
<!--Provides view for all other user types-->
<?php else: ?>
<table>
<?php foreach ($user_info as $row): ?>
<tr>
<th>Name: </th>
<td><?php echo $row->user_fullname ?></td>
</tr>
<tr>
<th>CWID: </th>
<td><?php echo $row->CWID ?></td>
</tr>
<tr>
<th>User Name: </th>
<td><?php echo $row->user_name ?></td>
</tr>
<tr>
<th>Email: </th>
<td><?php echo $row->user_email ?></td>
</tr>
<tr>
<th>Phone: </th>
<td><?php echo $row->user_phone ?></td>
</tr>
<?php endforeach ?>
</table>
<?php endif ?>

How to enable disable dropdown based on user logged in?

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>

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.

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