I am new to codeigniter. I am stuck at update my form data. i am using one source code from internet but its not working for me. I am done with fetching data from multiple tables on one form.Now i want to update that form.I am sharing my code step by step so you will understand it.
Controller for fetching data in form:
public function __construct(){
parent::__construct();
$this->load->helper('url');
$this->load->model('user_model');
$this->load->library('session');
}
function login_user(){
$user_login=array(
'USER_NAME'=>$this->input->post('user_email'),
'PASSWORD'=>($this->input->post('user_password')),
'PIN'=>($this->input->post('user_pin'))
);
$data=$this->user_model->login_user($user_login['USER_NAME'],$user_login['PASSWORD'],$user_login['PIN']);
if($data)
{
$this->session->set_userdata('user_id',$data['USER_ID']);
$this->session->set_userdata('user_name',$data['USER_NAME']);
$this->session->set_userdata('employee_id',$data['EMPLOYEE_ID']);
$this->session->set_userdata('nationality',$data['NATIONALITY']);
$this->session->set_userdata('EMPLOYEE_NAME',$data['EMPLOYEE_NAME']);
$this->session->set_userdata('EMPLOYEE_NUMBER',$data['EMPLOYEE_NUMBER']);
$this->session->set_userdata('DATE_OF_BIRTH',$data['DATE_OF_BIRTH']);
$this->session->set_userdata('JOINING_DATE',$data['JOINING_DATE']);
$this->session->set_userdata('CURRENT_LOCATION',$data['CURRENT_LOCATION']);
$this->session->set_userdata('SEX',$data['SEX']);
$this->session->set_userdata('ADDRESS',$data['ADDRESS']);
$this->session->set_userdata('COMPANY_EMPLOYEE_NUMBER',$data['COMPANY_EMPLOYEE_NUMBER']);
$this->session->set_userdata('PHONE',$data['PHONE']);
$this->session->set_userdata('MOBILE',$data['MOBILE']);
$this->session->set_userdata('EMAIL_ADDRESS',$data['EMAIL_ADDRESS']);
$this->session->set_userdata('ALTERNATE_EMAIL',$data['ALTERNATE_EMAIL']);
$this->session->set_userdata('WORKING_HOURS',$data['WORKING_HOURS']);
$this->session->set_userdata('NOK',$data['NOK']);
$this->session->set_userdata('NOK_CONTACT_NUMBER',$data['NOK_CONTACT_NUMBER']);
$this->session->set_userdata('EMPLOYMENT_STATUS_ID',$data['EMPLOYMENT_STATUS_ID']);
$this->session->set_userdata('DESIGNATION_NAME',$data['DESIGNATION_NAME']);
$this->session->set_userdata('CURRENT_ORG_ID',$data['CURRENT_ORG_ID']);
$this->session->set_userdata('COS_NUMBER',$data['COS_NUMBER']);
$this->session->set_userdata('VISA_START_DATE',$data['VISA_START_DATE']);
$this->session->set_userdata('VISA_END_DATE',$data['VISA_END_DATE']);
$this->session->set_userdata('DESCRIPTION',$data['DESCRIPTION']);
$this->session->set_userdata('SKILLS',$data['SKILLS']);
$this->session->set_userdata('YR_OF_EXPERIENCE',$data['YR_OF_EXPERIENCE']);
$this->load->view('layouts/index.php');
}
else{
$this->session->set_flashdata('error_msg', 'Error occured,Try again.');
$this->load->view('layouts/login.php');
}
}
model for fetching data:
class User_model extends CI_model{
public function login_user($email,$pass,$pin){
$this->db->select('*');
$this->db->from('app_users');
$this->db->where('USER_NAME',$email);
$this->db->where('PASSWORD',$pass);
$this->db->where('PIN',$pin);
$this->db->join('employee_details', 'app_users.EMPLOYEE_ID = employee_details.EMPLOYEE_ID', 'LEFT');
$this->db->join('employee_master', 'app_users.EMPLOYEE_ID = employee_master.EMPLOYEE_ID', 'LEFT');
$this->db->join('designation_master', 'employee_details.DESIGNATION_ID = designation_master.DESIGNATION_ID', 'LEFT');
$this->db->join('employee_visa_details', 'employee_details.EMPLOYEE_ID = employee_visa_details.EMPLOYEE_ID', 'LEFT');
$this->db->join('visa_status_master', 'employee_visa_details.VISA_STATUS_ID = visa_status_master.VISA_STATUS_ID', 'LEFT');
$this->db->join('employment_status_master', 'employee_details.EMPLOYMENT_STATUS_ID = employment_status_master.EMPLOYMENT_STATUS_ID', 'LEFT');
$this->db->join('employee_skills', 'employee_details.EMPLOYEE_ID = employee_skills.EMPLOYEE_SKILLS_ID', 'LEFT');
if($query=$this->db->get())
{
return $query->row_array();
}
else{
return false;
}
}
}
Above files are working fine. i can see data of particular user in form view.
Now i want to update that data so i write below code:
Controller:
class Personal_details extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->helper('url');
$this->load->model('user_model');
$this->load->model('personal_details_model');
$this->load->library('session');
}
public function update_personal_details(){
$products = new personal_details_model;
$products->update_personal_details($id);
redirect('/responsibilities/personal_details/');
}
}
model for the same:
class Personal_details_model extends CI_model{
public function update_personal_details($id)
{
$data=array(
'EMPLOYEE_NAME' => $this->input->post('emp_name'),
'DATE_OF_BIRTH'=> $this->input->post('dob'),
'JOINING_DATE' => $this->input->post('joining_date'),
);
if($id==0){
return $this->db->insert('employee_master',$data);
}else{
$this->db->where('EMPLOYEE_ID',$id);
return $this->db->update('employee_master',$data);
}
}
}
I dont understand how to write code to update above all tables.
My form view is like this:
<form action="<?php echo site_url('personal_details/update_personal_details');?>" method="post" class="form-horizontal" >
<button type="submit" class="btn btn-info">Edit</button>
<button type="submit" class="btn btn-danger">Cancel</button>
I am getting this error on click edit button:
error on update
First you have to get form data in Personal_details controller then you will pass that data to the Personal_details_model. Second no need to create new object for your model in update_personal_details() function you already loaded personal_details_model in your constructor.
Related
I'm having issues passing data from my controller to my view. I have a contenteditable div that I am saving the contents of to my database; I want to then display that in the view.
My controller
{
function __construct(){
parent::__construct();
$this->load->database();
$this->load->model('view_data');
$data = array();
}
public function index(){
$data['contactMe'] = $this->input->post('data');
$this->db->update('content', $data);
$results = $this->view_data->loadData();
$data['contactMeData'] = $results;
$this->load->view('pages/home', $data);
}
}
My model
<?php
class View_data extends CI_Model{
function __construct(){
parent::__construct();
}
public function loadData(){
$this->db->where('contactMe');
$query = $this->db->get('content');
return $query->result_array();
}
}
My view
<div class="w-100" ondblclick="makeEditable(this)" onblur="makeReadOnly(this)" contenteditable="true" id="contactMe">
<?php
foreach($contactMeData as $contact){
echo $contact;
}
?>
</div>
<button id="editBtn" type="submit" value="">Save changes
</button>
Every time I reload the page it replaces the data already in the database and I just get a blank div. I'm unable to view the data that was in the database to begin with.
try to add parameter on where
$this->db->where('contactMe',$parameter);
try to add field name on your view
echo $contact['field_name'];
This might be because every time you open the URL it updates the table, so you need a condition to check if POST request was made or not and then update the column
public function index(){
if($this->input->post()){ // check for post request
$data['contactMe'] = $this->input->post('data');
$this->db->update('content', $data); // try writing db queries in model
}
$results = $this->view_data->loadData();
$data['contactMeData'] = $results;
$this->load->view('pages/home', $data);
}
See, if it helps you.
I'm trying to fetch de data from my database and show it in my view.
But I don't seem to get it right. I worked a bit with codeIgniter and I know you can display your data in the view in a way that goes something like this:
{data}{dataname}{/data}
And this would show you every record of the dataname.
This is my model:
public function __construct()
{
$this->load->database();
}
public function Get_Pedras()
{
$query=$this->db->query("SELECT * FROM pedras;");
$row = $query->row_array();
return $row;
}
And this is my controller
public function __construct()
{
parent::__construct();
$this->load->model('Pedras_Model');
$this->load->helper('url_helper');
}
public function index (){
$this->load->view('Main/Formulario_Email');
}
public function GetStone(){
$this->load->library('parser');
$name = $this->Pedras_Model->Get_Pedras();
$data = array(
'title' => 'IdPedra',
'heading' => 'NomePedra'
);
$this->load->view('Main/Formulario_Email', $data);
}
And at least my view:
<div class="col-sm-6 col-sm-offset-1">
{data}
{title}
{/data}
</div>
I think I might be missing something realy important, but I only worked a little bit with CI, and some time ago, could you be helping me out pls? Thanks in advance!
In your Model don't use row_array because you are querying all the available data from your table. use result_array instead.
Like this...
public function Get_Pedras(){
$query=$this->db->query("SELECT * FROM pedras;");
return $query->result_array();
}
Try this one in your Controller...
public function GetStone(){
$this->load->library('parser');
$data['pedras'] = $this->Pedras_Model->Get_Pedras();
$this->load->view('Main/Formulario_Email', $data); //pass data here!
}
then in your view, in order to display the data do this...
foreach($pedras as $p){
echo $p['data']; //rename data with you query_name.
}
When on of the admin clicks an approve button its supposed to add the user_id of the person approving to a field in approval table
This is the button
<?php echo anchor('admin/messages/approve/'.$message->id.'', 'Approve', 'class="btn btn-success"'); ?>
This is the approve function in Controller
public function approve($id) {
$data = array(
'first_approval' => $this->session->userdata('user_id')
);
$this->Message_model->approve($id, $data);
//isset Message
$this->session->set_flashdata('success', 'Your approval was send');
//Redirect
redirect('admin/messages');
}
This is the approve method inside the Model
public function approve($data) {
$this->db->select('*');
$this->db->from('approval');
$this->db->join('messages', 'messages.id = approval.sms_id');
$this->db->where('id', 'sms_id');
$this->db->set('first_approval', $data);
$this->db->update('approval');
}
I have sms_id, first_approval, second_approval and third_approval columns in my approval table but for now I'm just testing if I can put session->user_id in the column 'first_approval'
Change your approve method as following in MODEL
public function approve($id, $data) {
$this->db->where('sms_id', $id);
$this->db->update('approval',$data);
}
Please check below view model and controller
View
<?php echo anchor('admin/messages/approve/'.$message->id.'', 'Approve', 'class="btn btn-success"'); ?>
Controller
class Messages extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('message_model');
}
function approve(){
$id = $this->uri->segment(3); //Get message id
$data = array(
'first_approval' => $this->session->userdata('user_id')
);
$res = $this->Message_model->approve($id, $data);
if($res >0){
//Redirect
redirect('admin/messages');
}else{
//some error accured
}
}
}
Model
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Message_model extends CI_Model {
public function __construct() {
$this->load->database(); //If database not set in autoload
}
public function approve($id, $data) {
$this->db->where('sms_id', $id);
return $this->db->update('approval',$data); //If table update return no of rows update
}
}
model
This is my model.using last query I get value in the model,but I don't get the value in the controller
class Login_model extends CI_Model {
function __construct()
{
parent::__construct();
}
public function email()
{
$this->db->select('email');
$this->db->from('change_password');
$result=$this->db->get();
return $result;
}
}
controller
class Login extends CI_Controller {
function __construct()
{
parent::__construct();
}
function checking()
{
$email=$this->input->post('email');
$this->load->model('login_model');
$dbemail=$this->login_model->email();
echo $dbemail;
}
}
CI is a MVC Framework. So you need to Command from Controller and get data from the models and finely you need to pass them to view. This is a best Practice
Controller
function checking()
{
$email=$this->input->post('email');
$this->load->model('login_model');
$data['dbemail']=$this->login_model->email();// assign your value to CI variable
$this->load->view('home', $data); //passing your value to view
}
Model
public function email()
{
$query = $this->db->query("SELECT email FROM change_password");
$result = $query->result_array();
return $result;
}
View
Extra Knowledge
View will be create under View folder/ Ex im using view name
as home.php
you can use your own stylings(its also created as normal html page.)
foreach ( $dbemail as $new_dbemail )
{
echo $new_dbemail['database_field'];//in here you can get your table header.
//Ex if your table has name field and you need to sho it you can use $new_dbemail['name']
}
Basically all I want to do is from my view page which is displaying a specific article then having a delete link to a page with a field and a button for deleting that article with the id displayed in that field.
I have had a go at this for the last couple of days trying to put the id in the URL link i.e "delete?id=20" and trying to access it with a $_GET and then I tried "delete/20" and URI segments.
Then I attempted at using sessions etc but i am not sure which is best as I haven't got any of them working.
I have decided to show my untouched code and start from scratch here is my code:
view.php
<?php
echo '<h2>'.$news_item['title'].'</h2>';
echo '<p>'.$news_item['text'].'</p>';
?><br><br>
<a href="http://website.com/CodeIgniter/index.php/news">
Go to latest news</a>
Delete<br>
delete.php
<h2>Delete a news item</h2>
<?php echo validation_errors(); ?>
<?php echo form_open('news/delete') ?>
<form>
<label for="delete">Article Number</label><br>
<input name="id" class="resizedTitlebox" value="id" /><br>
<br>
<input type="submit" name="submit" value="Delete news item" /></form>
news.php (controller)
<?php
class News extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
}
public function view($slug)
{
$data['news_item'] = $this->news_model->get_news($slug);
if (empty($data['news_item']))
{
show_404();
}
$data['title'] = $data['news_item']['title'];
$this->load->view('templates/header', $data);
$this->load->view('news/view', $data);
$this->load->view('templates/footer');
}
public function delete() {
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Delete news item';
$this->form_validation->set_rules('id', 'required');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('news/delete');
$this->load->view('templates/footer');
}
else
{
$data['id'] = $this->news_model->delete('id');
$this->load->view('news/success');
}
}
}
news_model.php (model)
<?php
class News_model extends CI_Model {
public function __construct() {
$this->load->database();
}
public function get_news($slug = FALSE){
$this->load->helper('text');
if ($slug === FALSE){
$this->db->order_by('id', 'desc');
$query = $this->db->get('news');
return $query->result_array();
}
$query = $this->db->get_where('news', array('slug' => $slug));
return $query->row_array();
}
public function set_news(){
$this->load->helper('url');
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$data = array(
'id' => $this->input->post('id'),
'title' => $this->input->post('title'),
'slug' => $slug,
'text' => $this->input->post('text'));
return $this->db->insert('news', $data);
}
public function delete ($id) {
$this->db->where('id',$this->input->post('id'));
$this->db->delete('news');
}
}
routes.php (config)
$route['news/(:any)'] = 'news/view/$1';
$route['news/delete'] = 'news/delete';
$route['news'] = 'news';
$route['default_controller'] = 'news';
$route['404_override'] = '';
Thanks in advance for any help given!
#jeroen answer
"You are not passing any value in the delete link; you should add an ID either to the path or the query string" - I assume you mean like
Delete
So using article_id. Then I can define article_id in the delete controller? I am not sure how this can be done.
answer:
$this->input->get(article_id)
Your delete procedure seems strange and has some errors:
You are no specifying a method in your form, so that will default to GET but in your delete function you use $this->input->post('id'). You should change your form to <form action="" method="POST">;
You are sending a variable to your delete function that you don't use: $id. This is a string in your controller although the name in the model seems to suggest an ID. But you are not using it anyway...
You are expecting a return value from the delete function when you call it in your controller but you don't return anything from your function.
You are not passing any value in the delete link; you should add an ID either to the path or the query string, but be careful not to use the same name $id if you use the same controller as the form will validate without the confirmation;
You are not getting any value for the ID in the delete controller and passing it to the view to fill the value of your ID field.
By the way, there is no input type id but that should not cause any problems as it defaults to text.
I think their is no need of delete view page.You can directly delete news item from your news list page
Delete<br>
instead of this line use this
<a href = "<?php echo site_url("news/delete/".$news_item['id']); ?>Delete</a>
And in your controller
public function delete($id){
$this->news_model->delete($id);
$this->load->view('news/success');
}
And in your model use this
function Delete($id){
$this->db->where('id', $id);
if($this->db->delete("news"))
return true;
else
return false;
}