I am new to codeigniter. I am stuck somewhere in displaying the value retrieved from database!
How can I display the value extracted from Database into the Textbox using Codeigniter ?
My view is : PutArtistProfile_v
<?php
foreach ($return_Name as $row )
{
echo '<input type="text" name="Name" id="Name" />';
}
?>
My Controller is:
public function index($return_Name)
{
$this->load->view('PutArtistProfile_v', $return_Name );
}
$return_Name -- have data fetched from database.
<?php
foreach($return_Name as $key)
{
$val= $key->text_name;
echo "<input type='text' value='$val' />";
}
?>
In View -
<?php
foreach ($return_Name as $row )
{
echo '<input type="text" name="Name" id="Name" value="$row->columnname" />';
}
?>
You have to send your result to view:
In controller:
public function index($return_Name)
{
$data['return_Name'] = $return_Name;
$this->load->view('PutArtistProfile_v', $data );
}
In view you can get data like $return_Name
You can pass the data to view using controller method
public function index()
{
$data['return_data'] = $this->model_name->function_name();
$this->load->view('view_filename', $data );
}
And in view you can access the value using loop
foreach($return_data as $row)
{
echo '<input type="text" name="name" value="$row['column_name']" /';
}
You can access your data from session variables from controller
$data['id'] = $this->session->userdata('user_id');
And in view you can echo it out in input fields
<input type="text" name="id_admin" value="<?php echo $id; ?>" maxlength="50" class="form-control" />
Related
I'm new in codeigniter and I've already seen some similar types of questions here. But none worked in my case.
I've a form in my view and I've passed the input value from this form to a method named "Insert" inside the controller. Now,I'm trying to move this value from "insert" method "to another method named "post_action". But couldn't make it possible.
This is the Controller:
public function insert() {
$data['values'] = $this->Final_model->insert();
$this->load->view('info/insert',$data);
$val = $this->input->post('coursecode');
echo $val;
$this->post_action($val); //passing data into another function
}
public function post_action($val='')
{
$temp1 = $this->input->post('textbox');
if($temp1== "")
{
$message = "You can't send empty text";
}
else
{
print_r($val);
if($val == NULL) echo 'Value is null bro...';
$grades = $_POST['grade'];
$msg = $_POST['my'];
$message = $_POST['textbox'];
$this->Final_model->build_post($msg,$val,$message,$grades);
echo "Value added successfully";
}
}
This is my 'index' view :
<?php echo form_open('Home_Controller/insert'); ?>
<div class="form-group">
<label>Course Code</label>
<input type="text" class="form-control" name = "coursecode"
placeholder="Add Course Code">
<br> <br>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
This is my 'insert' view :
<?php echo form_open('Home_controller/post_action'); ?>
<td> <input type="text" name="textbox" id = "textbox"> </td>
<td><input type="text" name="grade" id = "grade1"></td>
<input type="hidden" name = "my" value = "<?php echo $value['id']; ?>"
id
= "my"/>
<td><input type="submit" value="Submit"></td>
</form>
Now, it is echoing the correct value in the insert method passed by the view. But in the post_action method, it is echoing "value is null bro" and a 0 is passing to the database table(this one is an insert operation done in the model).
My question is,why can't I get the input value passed from the view, in this post_action method.
P.S: I've already spent about 2 days in this problem but haven't got anything. So, please forgive me if the question is too naive. Thanks in advance.
Please try with this Controller:
<?php
public function insert()
{
$data['values'] = $this->Final_model->insert();
$this->load->view('info/insert',$data);
$val['coursecode'] = $this->input->post('coursecode');
echo $val['coursecode']; //echoing value from post
if(!empty($val)){
$this->post_action($val); //passing val into another function
} else {
echo "Didn't get value from post";
}
}
public function post_action($val = '')
{
if(is_array($val) && count($val) > 0)
{
$grades = $_POST['grade'];
$msg = $_POST['my'];
$message = $_POST['textbox'];
$this->Final_model->build_post($msg,$val,$message,$grades);
echo "Value added successfully";
}
else
{
echo "No result found";
}
}
?>
I'm having some challenge updating table a form joined with session userdata from another table. Each time I run, it displays undefined function. Please let me know where I'm getting it wrong so could fix it.Please find time to review it
I'm a newbie.
Here's the controller
public function index()
{
if ( $this->session->userdata('logged_in') )
{
$id = $this->session->userdata('id');
$this->load->model('Userinfo_model');
$data['result'] = $this->Userinfo_model->get_users($id);
$this->load->view("usermain_info", $data);
}
}
public function update_user (){
$data = $this->input->post('userId');
$id = array(
'balance' => $this->input->post('balance'),
'id' => $this->input->post('id')
);
$this->Userinfo_model->update_user($id, $data);
$this->index();
}
Model
function get_users($userid)
{
if (isset($this->session->userdata['logged_in'])) {
$userid = ($this->session->userdata['logged_in']['id']);
} else {
header("location: nwpgroup/nwp2/index.php");
}
/* all the queries relating to the data we want to retrieve will go in here. */
$query = $this->db->query("SELECT * FROM walletbalance WHERE userId='$userid'");
return $query;
}
function update_user($id,$data){
if (isset($this->session->userdata['logged_in'])) {
$data = ($this->session->userdata['logged_in']['id']);
} else {
header("location: nwpgroup/nwp2/index.php");
}
$this->db->where('userId', $data);
$this->db->update('walletbalance', $id);
}
View
<form method="post" action="<?php echo base_url() . "index.php/userinfo/update_user"?>">
<?php if($result->num_rows() == 0){
echo 'No user found';
}
else {
foreach ( $result->result_array() as $new_user ){ ?>
<h4>Your name:<input value=" <?php echo $new_user['balance'] ?>" type="text" /> </h4><br />
<h4>Your name:<input value=" <?php echo $new_user['id'] ?>" type="text"/> </h4><br/>
<h4>Your name: <input value="<?php echo $new_user['userId'] ?>" type="hidden"/> </h4>
<input type="submit" id="submit" name="dsubmit" value="Update">
<?php }
}
?>
Error message
Message: Undefined property: Userinfo::$Userinfo_model
and
Message: Call to a member function update_user() on null
Thanks, I'm grateful
change your view as follows :
<form method="post" action="<?php echo base_url() . "index.php/userinfo/update_user"?>">
<?php if($result->num_rows() == 0){
echo 'No user found';
}
else {
foreach ( $result->result_array() as $new_user ){ ?>
<h4>Your name:<input value=" <?php echo $new_user['balance'] ?>" type="text" name="balance" /> </h4><br />
<h4>Your name:<input value=" <?php echo $new_user['id'] ?>" type="text" name="id" /> </h4><br/>
<h4>Your name: <input value="<?php echo $new_user['userId'] ?>" type="hidden" name="userId"/> </h4>
<input type="submit" id="submit" name="dsubmit" value="Update">
<?php }
}
?>
</form>
form will send data to server only if the element has a name
and you cannot submit a form multiple times. The above code will create update button for each row. So if you want to update all the records in a single updation, use update_batch() in codeigniter. and change view as follows :
foreach ( $result->result_array() as $new_user ){ ?>
<h4>Your name:<input value=" <?php echo $new_user['balance'] ?>" type="text" name="balance" /> </h4><br />
<h4>Your name:<input value=" <?php echo $new_user['id'] ?>" type="text" name="id" /> </h4><br/>
<h4>Your name: <input value="<?php echo $new_user['userId'] ?>" type="hidden" name="userId"/> </h4>
<?php } ?>
<input type="submit" id="submit" name="dsubmit" value="Update">
for reference : https://www.codeigniter.com/userguide3/database/query_builder.html#updating-data
In your update_user function you didn't include the usermain_info model. That is why the error is coming.
Please create constructor function and include the model in it. I have updated your code. Try with this.
<?php
function __construct() {
parent::__construct();
$this->load->library('session');
$this->load->model('Userinfo_model');
}
public function index(){
if ( $this->session->userdata('logged_in') ){
$id = $this->session->userdata('id');
$data['result'] = $this->Userinfo_model->get_users($id);
$this->load->view("usermain_info", $data);
}
}
public function update_user (){
$data = $this->input->post('userId');
$id = array(
'balance' => $this->input->post('balance'),
'id' => $this->input->post('id')
);
$this->Userinfo_model->update_user($id, $data);
$this->index();
}
?>
I can't update data in a record in CodeIgniter . Instead of updating it's adding new record in database table.'hotelres_id' is the primary key of my table.
I have posted the code below:-
Controller code for update:-
function edit($id){
$this->load->model('hotel_reservation','mb');
$data['message'] = '';
$data['object'] = $this->mb->find_by_id($id);
if($data['object']){
$this->form_validation->set_rules('roomno', 'Room Number', 'required|is_unique[hotel_reservation.roomno]');
$this->form_validation->set_rules('checkin', 'Check In', 'required|is_unique[hotel_reservation.checkin]');
$this->form_validation->set_rules('checkout', 'Check Out', 'required|is_unique[hotel_reservation.checkout]');
if ($this->form_validation->run() == TRUE){
$this->mb->eventreg_id = $_POST['hotelres_id'];
$this->mb->eventreg_id = $_POST['eventreg_id'];
$this->mb->eventhotel_id = $_POST['eventhotel_id'];
$this->mb->roomno = $_POST['roomno'];
$this->mb->checkin = $_POST['checkin'];
$this->mb->checkout = $_POST['checkout'];
$this->mb->comment = $_POST['comment'];
$this->mb->update();
$data['message'] = 'Details updated successfully';
$data['object'] = $this->mb;
}
$this->load->view('core/hotel_reservation/edit',$data);
}
else{
$data['message'] = 'No details available!! Fill It!!';
$this->load->view('core/hotel_reservation/save',$data);
}
}
View Code :-
<html>
<head>
<title>Hotel Reservation</title>
</head>
<body>
<h2>Hotel Reservation</h2>
<?php if(isset($message)&&$message!='') echo "<span class=\"message\">{$message}</span>"; ?>
<form action="<?php echo site_url('core/hotel_re/edit/'.#$object->hotelres_id); ?>" method="POST" >
<table class="formtable">
<tr><td>hotelres_id</td><td><input type="text" name="hotelres_id" id="hotelres_id" class="textbox" value="<?php echo #$object->hotelres_id; ?>" readonly></td></tr>
<tr><td>eventreg_id</td><td><input type="text" name="eventreg_id" class="textbox" value="<?php echo #$object->eventreg_id; ?>" readonly></td></tr>
<tr><td>eventhotel_id</td><td><input type="text" name="eventhotel_id" class="textbox" value="<?php echo #$object->eventhotel_id; ?>" readonly></td></tr>
<tr><td>Room Number</td><td><input type="text" name="roomno" value="<?php echo #$object->roomno; ?>" class="textbox" ></td></tr>
<tr><td>Check In</td><td><input type="text" name="checkin" value="<?php echo #$object->checkin; ?>" class="textbox"></td></tr>
<tr><td>Check Out</td><td><input type="text" name="checkout" value="<?php echo #$object->checkout; ?>" class="textbox"></td></tr>
<tr><td>Comment</td><td><textarea type="text" name="comment" value="<?php echo #$object->comment; ?>" class="textarea" ></textarea></td></tr>
<tr><td> </td><td><input type="submit" name="submit" value="Update" class="submitbutton"></td></tr>
</table>
</form>
<span class="validation-errors"><?php echo validation_errors(); ?></span>
</body>
</html>
Model code:-
<?php
class hotel_reservation extends CI_Model{
var $hotelres_id;
var $eventreg_id;
var $eventhotel_id;
var $roomno;
var $checkin;
var $checkout;
var $comment;
static $tablename = 'hotel_reservation';
static $tableid = 'hotelres_id';
function find_by_id($id)
{
$tableid = self::$tableid;
$resultset = $this->db->get_where(self::$tablename,array($tableid=>$id),1);
if($resultset->num_rows()==1)
return array_shift($resultset->result(get_class($this)));
return false;
}
function find_all()
{
$resultset = $this->db->get(self::$tablename);
return $resultset->result(get_class($this));
}
function save()
{
$tableid = self::$tableid;
if(isset($this->$tableid)&&$this->$tableid!=''&&$this->$tableid!=0)
$this->update();
else
$this->insert();
}
private function insert()
{
$this->db->insert(self::$tablename,$this);
}
function update()
{
$tableid = self::$tableid;
$this->db->where($tableid,$this->$tableid);
$this->db->update(self::$tablename,$this);
}
function delete()
{
$tableid = self::$tableid;
$this->db->where($tableid,$this->$tableid);
$this->db->delete(self::$tablename);
}
}
The mistake is here!!
In your controller, you have done this...
$this->mb->eventreg_id = $_POST['hotelres_id'];
$this->mb->eventreg_id = $_POST['eventreg_id'];
This should be this
$this->mb->hotelres_id = $_POST['hotelres_id'];
$this->mb->eventreg_id = $_POST['eventreg_id'];
And like i Said, the update term should be written like this...
$this->db->where($tableid,$this->hotelres_id);
Solved?
From what i can see, i think your issue is with this line:
$this->db->update(self::$tablename,$this);
As you should pass the update function an array with matching keys to your TB columns and values to be updated, with you send the whole object ($this), doesn't also send tableid and tablename?
According to your code ($this->mb->update();), it is calling mb model. But you have provided code for hotel_reservation model. May be some mismatch there.
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)?>" />
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);
}