I have this function (get_profile_info)
function get_profile_info()
{
$this->db->where('username',$this->session->userdata('username'));
$get_profile_info = $this->db->get('memberships');
if($get_profile_info->num_rows() == 1){
foreach ($get_profile_info->result() as $row)
{
echo $row->firstname;
echo $row->lastname;
}
}
}
The question is how do i put this results: firstname and lastname into input form default set_value: echo "Firstname".form_input('firstname2');
Try this code:
foreach($get_profile_info->result() as $row)
{
$data = array(
'name' => 'firstname',
'id' => 'firstname',
'value' => $row->firstname,
);
echo form_input($data);
// do the same for lastname
}
Set input value of your view file like this.
<input type="text" value="<?php echo $data['firstname']; ?>" />
you have to pass data array from controller.
Try this
function get_profile_info()
{
$this->db->where('username',$this->session->userdata('username'));
$get_profile_info = $this->db->get('memberships');
if($get_profile_info->num_rows() == 1){
return $get_profile_info->row();
}
function your_controller
{
$data['row'] = get_profile_infor()
$this->load->view("yourview",$data)
}
IN View
<input type="text" name="firstname" value="<?php echo set_value('firstname',$row->firstname)?>" />
<input type="text" name="lastname" value="<?php echo set_value('lastname',$row->lastname)?>" />
Related
This is my view Page
This is Dynamically fields Test Name and units are from Table I need to insert Test name and Result and Normal value also Unit.
My Controller
$patient_id = $this->input->post('patient_id');
$doctor_id = $this->input->post('doctor_id');
$prescription_id = $this->input->post('prescription_id');
$lab_result =$this->input->post('lab_result');
$lab_test =$this->input->post('lab_test');
$units =$this->input->post('units');
$normal_value =$this->input->post('normal_value');
$cat_id = $this->input->post('cat_id');
for($i=0; $i<count($prescription_id); $i++)
{
$labreport[] = array(
'patient_id' => $patient_id[$i],
'doctor_id' => $doctor_id[$i],
'prescription_id' =>$prescription_id[$i],
'lab_result' => $lab_result[$i],
'lab_test' => $lab_test[$i],
'units' => $units[$i],
'normal_value' => $normal_value[$i],
'cat_id' => $cat_id, );
//echo '<pre>'; print_r($labreport); '</pre>'; exit;
}
$stringlabreport= json_encode($labreport);
$this->db->insert('patient_lab_report',$stringlabreport);
if($this->db->affected_rows()){
return true;
} else {
return false;
}
$this->session->set_flashdata('message','Lab Report Added Successfully');
redirect('laboratory_report/all');
=========================
And This is My View Code
<tr>
<td><input type="hidden" value="<?php echo $data->name; ?>"name="lab_test[]"><?php echo $data->name; ?></td>
<td><input type="text" value="" name="lab_result[]" class="form-control"></td>
<td><input type="hidden" value="<?php echo $data->units; ?>" name="units[] "> <?php echo $data->units; ?></td>
<td><input type="hidden" value="<?php echo $data->n_value; ?>" name="normal_value[] "> <?php echo $data->n_value; ?></td>
</tr>
Use array names for input. I.E.:
<form action="" method="post" name="someform">
<input name="row[0][test_name]"/>
<input name="row[0][result]"/>
<input name="row[1][test_name]"/>
<input name="row[1][result]"/>
<input type="submit" name="submitted" value="send">
</form>
So upon submission, parsing in php, use filter_input with FILTER_REQUIRE_ARRAY as option:
<?php
$submitted = filter_input(INPUT_POST, 'submitted');
if(!empty($submitted)){
$rows = filter_input(INPUT_POST,'row', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
$parsed_rows = [];
foreach($rows as $row){
if(!empty($row['test_name']) && !empty($row['result'])){
$parsed_rows[] = $row;
}
}
}
?>
I want to update data for multiple users using codeginiter, I used the bellow script but it only updates for one user.
<?php echo form_open(base_url() . 'get/save_payment', array('role' => 'form')); ?>
<?php foreach ($mature as $key => $value) { ?>
<input type="text" class="form-control" value="<?php echo $value->id; ?>" readonly name="id[]">
<input type="text" class="form-control" value="<?php echo $value->name; ?>" readonly name="name[]">
<input type="text" class="form-control" value="0" readonly name="amount[]">
<?php } ?>
<button type="submit" class="btn btn-success">Confirm Request</button>
<?php echo form_close(); ?>
CONTROLLER
public function save_payment() {
$this->load->library('form_validation');
$this->form_validation->set_rules('id[]', 'id', 'required|trim');
$this->form_validation->set_rules('name[]', 'name', 'trim');
$this->form_validation->set_rules('amount[]', 'Amount', 'required|max_length[4]|trim|strip_tags');
if ($this->form_validation->run() == FALSE) {
$this->session->set_flashdata('id', set_value('id'));
$this->session->set_flashdata('name', set_value('name'));
$this->session->set_flashdata('amount', set_value('amount'));
$this->payment();
} else {
$id = set_value('id[]');
$data['amount'] = set_value('amount[]');
$data['name'] = set_value('id[]');
$this->dashboard->update_ph($data,$id);
}
}
Here is the MODEL :
public function update_ph($data,$id)
{
$this->db->where('id', $id);
$this->db->update('gw_ph', $data);
}
Kindly explain on how I can go through this code and get it working without error.
Thanks
Codeigniter use 'name' attributte for get data from 'input' and you use same 'name' attributte for all of your readonly input
I'm having trouble to edit and update an item in my first CodeIgniter project. When I click on the edit button, first of all, it loads the view with the inputs filled with the informations of that item on the database, but it doesn't load the bootstrap that I'm using.
Then, when I click the "Edit" button, returns lots of errors saying that:
my function on the controllers is missing an argument and the variable is undefined, and
my view has an undefined offset and is trying to get property of non-object.
All the other commands for the CRUD are working, except this.
The version of CodeIgniter I'm using is 2.2.6
Codes:
Model:
function edit($id)
{
$this->db->where('id', $id);
$query = $this->db->get('products');
return $query->result();
}
function update($data)
{
$this->db->where('id', $data['id']);
$this->db->set($data);
return $this->db->update('products');
}
Controller:
function edit($id)
{
$data['title'] = "Edit Product";
$data['product_data'] = $this->model->edit($id);
$this->load->view('productEdit', $data);
}
function update()
{
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<span>', '</span>');
$validations = array(
array(
'field' => 'name',
'label' => 'Name',
'rules' => 'trim|required|max_length[255]'
),
array(
'field' => 'price',
'label' => 'Price',
'rules' => 'trim|required|max_length[255]'
),
array(
'field' => 'stock_quantity',
'label' => 'In Stock',
'rules' => 'trim|required|max_length[255]'
)
);
$this->form_validation->set_rules($validations);
if ($this->form_validation->run() === FALSE) {
$this->edit($this->input->post('id'));
} else {
$data['id'] = $this->input->post('id');
$data['name'] = $this->input->post('name');
$data['price'] = $this->input->post('price');
$data['stock_quantity'] = $this->input->post('stock_quantity');
if ($this->model->update($data)) {
redirect('products');
} else {
log_message('error', 'Error');
}
}
}
View:
<?php echo form_open('products/edit/', 'id="form-products"'); ?>
<input type="hidden" name="id" value="<?php echo $product_data[0]->id; ?>"/>
<label for="nome">Product Name:</label><br/>
<input type="text" name="name" value="<?php echo $product_data[0]->name; ?>"/>
<div class="error"><?php echo form_error('name'); ?></div>
<label for="email">Price:</label><br/>
<input type="text" name="price" value="<?php echo $product_data[0]->price; ?>"/>
<div class="error"><?php echo form_error('price'); ?></div>
<label for="email">In Stock:</label><br/>
<input type="text" name="stock_quantity" value="<?php echo $product_data[0]->stock_quantity; ?>"/>
<div class="error"><?php echo form_error('stock_quantity'); ?></div>
<input type="submit" name="update" value="Update" />
<?php echo form_close(); ?>
Thank you for your time.
Try this one
Update your model
function edit($id)
{
$this->db->where('id', $id);
$query = $this->db->get('products');
return $query->row(0);
}
and then in your view.
<input type="hidden" name="id" value="<?php echo $product_data->id; ?>"/>
also change your form action
products/update/
I want to update the data using array and checkbox. If the checkbox checked, status become "1". Else, leave it "0".
I have try something like this
<?php
foreach($report as $r){;
?>
<input type="checkbox" name="status[]" value="1" value="<?php echo $r->status;?>">
<input type="hidden" name="id_name[]" value="<?php echo $r->id_name;?>">
<input type="hidden" name="name[]" value="<?php echo $r->name;?>">
<?php } ?>
and this
<input type="checkbox" name="status[]" value="1">
<input type="hidden" name="id_name[]" value="<?php echo $r->id_name;?>">
<input type="hidden" name="name[]" value="<?php echo $r->name;?>">
But both of them update the first row even I check the third or the fourth row.
my controller is something like this
function update_approval() {
$status = $this->input->post('status');
$id_name = $this->input->post('id_name');
$name = $this->input->post('name');
for($a=0; $a< sizeof ($id_name); $a++) {
$data[$a] = array(
'status' => $status[$a],
'id_name' => $id_name[$a],
'name' => $name[$a]
);
}
$this->db->update_batch('tbl_m_name', $data, 'id_name');
}
Thanks in advance
I've solved it. Thanks,
I change the position, here is my views
<input type="checkbox" name="id_name[]" value="<?php echo $r->id_name;?>">
<input type="hidden" name="name[]" value="<?php echo $r->name;?>">
and here is my controller
function update_approval() {
$id_name = $this->input->post('id_name');
$name = $this->input->post('name');
$data[$a] = array();
for($a=0; $a< sizeof ($id_name); $a++) {
$data[] = array(
'status' => 1,
'id_name' => $id_name[$a],
'name' => $name[$a]
);
}
$this->db->update_batch('tbl_m_name', $data, 'id_name');
}
so if I check the box, it will post the id_name that I choose
thanks, but I had tried yours, I get undefinied variable $a. So, I changed the position and removed $data[$a]= array(); . Below is my code (after resolve my problem) based on yours:
function update_approval() {
$id_name = $this->input->post('id_name');
$name = $this->input->post('name');
for($a=0; $a< sizeof ($id_name); $a++) {
$data[$a] = array(
'status' => 1,
'id_name' => $id_name[$a],
'name' => $name[$a]
);
}
$this->db->update_batch('tbl_m_name', $data, 'id_name');
}
i have 2 textbox in loop
and i want to update their value in database when i press button.
the issue is that when i press button it updates only last textbox value .
following is my model.
function approvedHrs($taskid,$data)
{
//$this->db->where('id', $id);
$this->db->where('taskid', $taskid);
$this->db->update(MILESTONE, $data);
}
Following is my View (For lop is here)
<?php
foreach ($result as $milestone_row) {
?>
<tr id="<?php echo $milestone_row->id; ?>">
<?php
if ($is_master_admin) {
if ($i > 1) {
if ($milestone_row->userid == $userid) {
} else {
$userid = $milestone_row->userid;
echo $milestone_row->usertitle;
}
} else {
$userid = $milestone_row->userid;
echo $milestone_row->usertitle;
}
}
?>
<li class="in">
Add Bug
<div class="message">
<span class="arrow"></span>
<span class="body">
<?php
echo '<b> <U> Task Title </U>:- </b>  ';
echo $milestone_row->tasktitle;
echo '<br/>';
echo '<b> <U> Workspace Title </U>:- </b>  ';
echo $milestone_row->workspacetitle;
echo '<br/>';
echo '<b> <U> Description </U>:- </b>  ';
echo $milestone_row->description;
echo '<br/>';
echo '<b> <U> Hours </U>:- </b>  ';
echo $milestone_row->esthours;
echo 'hrs';
echo '<br/>';
echo '<b> <U> Minutes </U>:- </b>  ';
echo $milestone_row->estmin;
echo'mins';
echo '<br/>';
?>
<b><u>Approved Hours:-</u></b>
<input style="height:14px;font-size:10pt; width: 33px" type="text" id="hours" name="approvedhrs" data-required="1" value="<?php echo $milestone_row->esthours; ?>" placeholder="Hours" onkeypress="return isNumberKey(event)" />
<input style="height:14px;font-size:10pt; width: 33px" type="text" id="minutes" name="approvedmins" value="<?php echo $milestone_row->estmin; ?>" data-required="1" placeholder="Minutes" onkeypress="return isNumberKey(event)" />
Edit
<?php echo " | ";?>
Delete
<!--<span class="datetime">at <?php //echo $milestone_row->createddate; ?></span> -->
</span>
</div>
</li>
Followinf is myController
function approvedHrs($taskid)
{
if ($this->session->userdata('logged_in')){
$session_data = $this->session->userdata('logged_in');
$id = $session_data['id'];
$username = $session_data['username'];
$is_master_admin = $session_data['master'];
$imagethumb = $session_data['imagethumb'];
$pendingbug = $this->bugmodel->getBug($id, $is_master_admin);
$pendingtask = $this->taskmodel->getTask($id, $is_master_admin);
$data = '';
$data = array(
'approvedhrs' =>$this->input->post('approvedhrs'),
'approvedmins'=>$this->input->post('approvedmins')
);
// print_r($data);
// exit;
$result = $this->milestonemodel->approvedHrs($taskid,$data);
// $this->session->set_userdata('msg', $result);
redirect('task', 'refresh');
}
else
{
redirect('login', 'refresh');
}
}
When i use array like this it gives me following error
PHP Error Occured :Message: Array to string conversion
Database Error : Unknown column 'Array' in 'field list'
UPDATE milestone SET approvedhrs = Array, approvedmins = Array WHERE taskid = '17'
You should simplify your code like this
$post = $this->input->post();
$data['approvedhrs'] = $post['approvedhrs'];
$data['approvedmins'] = $post['approvedmins'];
print_r($data);
$result = $this->milestonemodel->approvedHrs($taskid,$data);
You should make sure $post['approvedhrs'] and $post['approvedmins'] do not have arrays. They could contain array when defined in the form like this
<input type='text' name="approvedhrs[]" />
<input type='text' name="approvedmins[]" />
EDIT
As you are making inputs inside loops there fore both approvedhrs and approvedmins are posted as array. Which is causing problem in updating.
EDIT
You should make the inputs in your loop like this
<input type='text' name="approvedhrs[]" />
<input type='text' name="approvedmins[]" />
Make hidden inputs for row id and task id
<input type='hidden' name="rowid[]" />
<input type='hidden' name="taskid[]" />
<input type='text' name="approvedhrs[]" />
<input type='text' name="approvedmins[]" />
And now php
$post = $this->input->post();
$data['approvedhrs'] = $post['approvedhrs'];
$data['approvedmins'] = $post['approvedmins'];
for($i=0;$i<count($data['approvedhrs']);$i++){
$update_data['approvedhrs'] = $data['approvedhrs'][$i];
$update_data['approvedmins'] = $data['approvedmins'][$i];
$taskid = $data['taskid'][$i];
print_r($update_data);
$result = $this->milestonemodel->approvedHrs($taskid,$update_data);
unset($update_data);
unset($taskid);
}
As you are passing array of data to update, you can use update_batch mentod.
In your controller, prepare the data as below.
$k= 0 ;
$data = array();
$aphrs = $this->input->post('approvedhrs'); // returns array
$apmis = $this->input->post('approvedmins'); //
foreach($aphrs as $hrs)
{
$data[] = array(
'approvedhrs' =>$hrs,
'approvedmins'=>$apmis[$k]
);
$k++;
}
// call your model function
$result = $this->milestonemodel->approvedHrs($taskid,$data);
In your model function: add update_batch method. So no need to call update method in forloop.
for update_batch, check this tutorial.
http://ellislab.com/codeigniter/user-guide/database/active_record.html#update
View
<input type='hidden' name="milestoneid[]" value ="<?php echo $milestone_row->id ?>" />
<input type="text" id="hours" name="approvedhrs[<?php echo $milestone_row->id ?>][]" value="<?php echo $milestone_row->esthours; ?>"/>
<input type="text" name="approvedmins[<?php echo $milestone_row->id ?>][]" value="<?php echo $milestone_row->estmin; ?>"/>
Controller
function approvedHrs($taskid) {
if ($this->session->userdata('logged_in')) {
$session_data = $this->session->userdata('logged_in');
$id = $session_data['id'];
$username = $session_data['username'];
$is_master_admin = $session_data['master'];
$imagethumb = $session_data['imagethumb'];
$pendingbug = $this->bugmodel->getBug($id, $is_master_admin);
$pendingtask = $this->taskmodel->getTask($id, $is_master_admin);
$data = array();
$milestoneIDs = $this->input->post('milestoneid');
$approvedhrs = $this->input->post('approvedhrs');
$approvedmins = $this->input->post('approvedmins');
foreach ($milestoneIDs as $milestoneID) {
$data = array(
'approvedhrs' => isset($approvedhrs[$milestoneID]) ? $approvedhrs[$milestoneID] : '',
'approvedmins' => isset($approvedmins[$milestoneID]) ? $approvedmins[$milestoneID] : '',
);
$this->milestone_model->approvedHrs($milestoneID,$data);//change this model name as per you use
unset($data);
}
redirect('task', 'refresh');
} else {
redirect('login', 'refresh');
}
}
Model
function approvedHrs($milestoteid, $data) {
//$this->db->where('id', $editid);
$this->db->where('milestoteid', $milestoteid);
$this->db->update(MILESTONE, $data);
}