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

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.

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 ?>

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

Submitting Form using Dynamic Text Link

I'm at lost. What I wanted to do is, I want my website to allow the users to select a certain row(records) in my database and then redirect them to another webpage that will show them the full information about that certain record.. I don't know how could I connect those two web pages together using dynamic table/text..
Below is a portion of my code: ( This is the first webpage: )
mysql_select_db($database_rfq_portal, $rfq_portal);
$query_rfqrecord = "SELECT tblrfq.`RFQ_ID`, tblrfq.`Company_Name`, tblrfq.Service,
tblrfq.`Kind_of_Request`, tblrfq.Status, tblrfq.`Date` FROM tblrfq";
$rfqrecord = mysql_query($query_rfqrecord, $rfq_portal) or die(mysql_error());
$row_rfqrecord = mysql_fetch_assoc($rfqrecord);
$totalRows_rfqrecord = mysql_num_rows($rfqrecord);
<form id="viewform" name="viewform" method="get" action="ViewSpecificRFQ.php">
<table width="716" border="1" align="center" cellpadding="5">
<tr>
<td>RFQ ID</td>
<td>Company Name</td>
<td>Service</td>
<td>Kind of Request</td>
<td>Status</td>
<td>Date</td>
</tr>
<?php do { ?>
<tr>
<td><?php echo $row_rfqrecord['RFQ_ID']; ?></td>
<td><?php echo $row_rfqrecord['Company_Name']; ?></td>
<td><?php echo $row_rfqrecord['Service']; ?></td>
<td><?php echo $row_rfqrecord['Kind_of_Request']; ?></td>
<td><?php echo $row_rfqrecord['Status']; ?></td>
<td><?php echo $row_rfqrecord['Date']; ?></td>
</tr>
<?php } while ($row_rfqrecord = mysql_fetch_assoc($rfqrecord)); ?>
</table>
}
</form>
This is the webpage that will get the form..(portion of my code)
$RFQID = $_GET['RFQ_ID'];
mysql_select_db($database_rfq_portal, $rfq_portal);
$query_rfqrecord = "SELECT * FROM tblrfq WHERE $RFQID";
$rfqrecord = mysql_query($query_rfqrecord, $rfq_portal) or die(mysql_error());
$row_rfqrecord = mysql_fetch_assoc($rfqrecord);
$totalRows_rfqrecord = mysql_num_rows($rfqrecord);
mysql_select_db($database_rfq_portal, $rfq_portal);
$query_user = "SELECT tbluser.Username, tbluser.Password FROM tbluser";
$user = mysql_query($query_user, $rfq_portal) or die(mysql_error());
$row_user = mysql_fetch_assoc($user);
$totalRows_user = mysql_num_rows($user);
<table width="716" border="0" align="center">
<tr>
<th colspan="2" scope="row">RFQ ID:</th>
<td><?php echo $row_rfqrecord['RFQ_ID']; ?></td>
</tr>
<tr>
<th colspan="2" scope="row">Company Name:</th>
<td width="511"><?php echo $row_rfqrecord['Company_Name']; ?></td>
</tr>
<tr>
<th width="101" rowspan="2" scope="row">Address:</th>
<th width="90" scope="row">Site A:</th>
<td><?php echo $row_rfqrecord['Address_A']; ?></td>
</tr>
<tr>
<th scope="row">Site B:</th>
<td><?php echo $row_rfqrecord['Address_B']; ?></td>
</tr>
<tr>
<th colspan="2" scope="row">Contact Number:</th>
<td><?php echo $row_rfqrecord['Contact_Number']; ?></td>
</tr>
<tr>
<th colspan="2" scope="row">Contact Person:</th>
<td><?php echo $row_rfqrecord['Contact_Person']; ?></td>
</tr>
<tr>
<th colspan="2" scope="row">Service:</th>
<td><?php echo $row_rfqrecord['Service']; ?></td>
</tr>
<tr>
<th colspan="2" scope="row">Bandwidth:</th>
<td><?php echo $row_rfqrecord['Bandwidth']; ?></td>
</tr>
<tr>
<th colspan="2" scope="row">Telco:</th>
<td><?php echo $row_rfqrecord['Telco']; ?></td>
</tr>
<tr>
<th colspan="2" scope="row">Account Manager:</th>
<td><?php echo $row_rfqrecord['Account_Manager']; ?></td>
</tr>
<tr>
<th colspan="2" scope="row">Status:</th>
<td><?php echo $row_rfqrecord['Status']; ?></td>
</tr>
<tr>
<th colspan="2" scope="row">Kind of Request:</th>
<td><?php echo $row_rfqrecord['Kind_of_Request']; ?></td>
</tr>
<tr>
<th colspan="2" scope="row">Date:</th>
<td><?php echo $row_rfqrecord['Date']; ?></td>
</tr>
<tr>
<th colspan="2" scope="row">Remarks:</th>
<td><?php echo $row_rfqrecord['Remarks']; ?></td>
</tr>
</table>
</form>
this redirects the user to the next page but my problem is it keeps on showing the same record, which is the first record in my database.
1- Consider page1.php is the first page that user selects a record and then in page2.php you show full detail.
for page1.php
you need to send some parameters like record id or any other key in order to be able to identify the record and select the detail in database.
for example:
Record 1 once user clicked on this link you have to do something like this in page2.php, remember our parameter is record_id
<?php
$id = $_GET['record_id'];
//we have to check it for validity
//if id is an integer (numbers) then simply we can check it
//if(!is_numeric($id)) { die("invalid id") }
// now the id is there
//lets build query
$query = "SELECT * from tablename where id= '$id' ";
?>
for your code it must be like this:
//adding where clause
$query_rfqrecord = "SELECT * FROM tblrfq where RFQ_ID = '$id' ";
$rfqrecord = mysql_query($query_rfqrecord, $rfq_portal) or die(mysql_error());
$row_rfqrecord = mysql_fetch_assoc($rfqrecord);
$totalRows_rfqrecord = mysql_num_rows($rfqrecord);
If you want your users to get to load a specific record when they click a specific row, you have to be sure you're redirecting them to a specific url. In your sample you have <a href="ViewSpecificRFQ.php"> in your do-while, so every row in your table will have the same link and then the same destination page.
You probably want something like <a href="ViewSpecificRFQ.php?recordId=<?php echo $row_rfqrecord['RFQ_ID'] ?>"> and then in your landing page the query will be performed against the key you will have in $_GET['recordId'].
EDIT: The do-while problem
You use a do-while, and so in your first iteration you have no record loaded, because the fetch instruction is located at the bottom of the code block, so you should change it to:
<?php
while ($row_rfqrecord = mysql_fetch_assoc($rfqrecord)) {
?>
<tr>
<td><?php echo $row_rfqrecord['RFQ_ID']; ?></td>
<td><?php echo $row_rfqrecord['Company_Name']; ?></td>
<td><?php echo $row_rfqrecord['Service']; ?></td>
<td><?php echo $row_rfqrecord['Kind_of_Request']; ?></td>
<td><?php echo $row_rfqrecord['Status']; ?></td>
<td><?php echo $row_rfqrecord['Date']; ?></td>
</tr>
<?php
}
?>

Categories