Update Codeigniter form - refresh - php

I'm having a form which retrieves the values from the database and updates it by submitting the form.
First time when I'm accessing the page, it works, it retrieves the values, but if I'm updating the values through the form it retrieves the new values only after refreshing manually the page.
How can I refresh it automatically?
Also, if I'm accessing the page, not doing an update, and refreshing it, everything it's deleted, including the values form the database.
This is the controller:
public function edit_profile()
{
$id=$_SESSION['user_id'];
$this->load->model('retrieve');
$upd = array
(
'Name' => $this->input->post('name'),
'surname' => $this->input->post('surname'),
'Id_loc' => $this->input->post('id_loc')
);
$data['info']=$this->retrive->select_Info($id);
$this->retrieve->update_info($id,$upd);
$this->load->view('edit_profile', $data);
}
And this is the model:
function update_info($id,$data){
$this->db->where('id', $id);
$this->db->update('users', $data);
}

your code will look like this: with two functions
public function edit_profile()
{
$id=$_SESSION['user_id'];
$data['info']=$this->retrive->select_Info($id);
$this->load->view('edit_profile', $data);
}
public function saveUpdate()
{
$id=$_SESSION['user_id'];
$this->load->model('retrieve');
$upd = array
(
'Name' => $this->input->post('name'),
'surname' => $this->input->post('surname'),
'Id_loc' => $this->input->post('id_loc')
);
$update = $this->retrieve->update_info($id,$upd);
if($update)
{
redirect('YURCTRLER/edit_profile');
}
}

add the following code to your controller after an update is done:
redirect($this->uri->uri_string());
or
redirect('YOURCONTROLLER/edit_profile', 'refresh');
For the case of deleting, the code you have provided I think is not enough to deduce the problem :-(

Related

Update/Replace data without form in codeigniter

Please help how to update data in a database table without input form. I want to make the value on table become "0". I have created a controller:
function reset(){
$data = array(
'nomor_antrian' => '0',
'jumlah_pengantri' => '0'
);
$this->db->replace('antrian', $data);
}
The model :
function reset($id) {
$data = array(
'nomor_antrian'=>'0',
'jumlah_pengantri'=>'0'
);
$this->db->set($data);
$this->db->where('nomor_loket', $id);
$this->db->update('antrian');
}
and this is the link button in the view section :
Reset
Thanks before.
You have to send $id to update record
Model code
function reset($id){
$data = array(
'nomor_antrian' => '0',
'jumlah_pengantri' => '0'
);
$this->db->set($data);
$this->db->where('nomor_loket', $id);
$this->db->update('antrian');
}
Reset link will be as follow
Reset //$id is a parameter
Pass parameter to reset() function

Insert one row using different models/pages - CodeIgniter

I'm new to CodeIgniter and I'm building a simple I/O website. I have only one database, say test and only one table results, that looks like this:
screenshot of the table "results"
I have two views personal.php and songs.php. In the first one I collect the data values to be inserted into the fields 2,3, and 4, and the rest of the values are collected in the second view. They are then inserted into the table via their relevant models, personal_model and songs_model.
Now obviously, they will be inserted into 2 different rows which is not what I want. What is the trick here? How should I manage it? So far I have thought of getting the last ID but I have no idea how to do it. Thanks in advance!
personal.php (first view)
<?php echo validation_errors(); ?>
<?php echo form_open('data/personal'); ?> //passes the data to the controller that loads the personal_model.php
"some input fields"
<button type="submit" name="submit">Submit Data</button>
songs.php (second view)
<?php echo validation_errors(); ?>
<?php echo form_open('data/songs'); ?> //passes the data to the controller that loads the songs_model.php
"some input fields"
<button type="submit" name="submit">Submit Rating</button>
personal_model.php (first model)
<?php
class Personal_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function insert_personal()
{
$this->load->helper('url');
$data = array(
'age' => $this->input->post('user_age'),
'education' => $this->input->post('user_edu'),
'twitter' => $this->input->post('user_twitter'),
'facebook' => $this->input->post('user_facebook')
);
return $this->db->insert('results', $data);
}
}
songs_model.php (second model)
<?php
class Ratings_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function insert_ratings()
{
$this->load->helper('url');
#$this->load->database();
$data = array(
'score' => $this->input->post('rating'),
'song1' => $this->input->post('rnd1'),
'song2' => $this->input->post('rnd2')
);
return $this->db->insert('results', $data);
}
}
Your Controller Function should be like this.
public function personal()
{
$lastInsertedID = $this->Personal_model->insert_personal();
$this->session->set_userdata("personalID",$lastInsertedID);
}
Set the last inserted id into session in your above controller function which should be return from your Personal_model. Here is code.
public function insert_personal()
{
$this->load->helper('url');
$data = array(
'age' => $this->input->post('user_age'),
'education' => $this->input->post('user_edu'),
'twitter' => $this->input->post('user_twitter'),
'facebook' => $this->input->post('user_facebook')
);
$this->db->insert('results', $data);
return $this->db->insert_id();
}
Then update your existing row in your insert_ratings function instead of insert record. Here is code.
public function insert_ratings()
{
$data = array(
'score' => $this->input->post('rating'),
'song1' => $this->input->post('rnd1'),
'song2' => $this->input->post('rnd2')
);
$personalID = $this->session->userdata("personalID");
$this->db->where("id",$personalID);
$this->db->update('results', $data);
return $this->db->affected_rows();
}
Then no new record will insert into table while submit your song.php form it will update the existing one.

Passing data from controller function to controller function before redirect

I am using codeigniter I have an edit page which shows me all information of a vacancy.
The (Vacancy) controller method to load this view looks like this, it makes sure all data is preloaded.
public function editVacancy($vacancyid)
{
$this->is_logged_in();
$this->load->model('vacancy/vacancies_model');
// Passing Variables
$data['title'] = 'Titletest';
$data['class'] = 'vacancy';
$orgadminuserid = $this->vacancies_model->getOrgAdminUserId($vacancyid);
if ((!is_null($orgadminuserid)) && ($this->auth_user_id == $orgadminuserid[0]->user_id)) {
$data['vacancyid'] = $vacancyid;
$data['vacancy'] = $this->vacancies_model->get($vacancyid);
$data['test'] = $this->session->flashdata('feedbackdata');
$partials = array('head' => '_master/header/head', 'navigation' => '_master/header/navigation_dashboard', 'content' => 'dashboard/vacancy/edit_vacancy', 'footer' => '_master/footer/footer');
$this->template->load('_master/master', $partials, $data);
}
}
In this view i have different forms for updating different sections.
Every form submit goes to a different method in my 'Vacancy' controller.
public function saveGeneralInfo()
{
$this->is_logged_in();
$this->load->model('vacancy/vacancies_model');
$vacancyid = $this->input->post('vacancyid');
$vacancyUpdateData = $this->vacancies_model->get($vacancyid);
$result = $this->vacancies_model->update($vacancyid, $vacancyUpdateData);
if ($result) {
$feedbackdata = array(
'type' => 'alert-success',
'icon' => 'fa-check-circle',
'title' => 'Success!',
'text' => 'De algemene vacature gegevens zijn geupdate.'
);
$this->session->set_flashdata('feedbackdata', $feedbackdata);
redirect("dashboard/vacancy/editVacancy/" . $vacancyid);
}
}
}
Where I indicated in my code "//HERE ..." is where I would want the feedback message parameter to pass on to my 'main controller method' which loads the view with the prefilled data. (editVacancy).
Is there a clean way to do this?
EDIT:
I tried using flashdata, i updated the code to have the flashdata inserted.
However when i do a var_dump($test); in my view, it remains null.
EDIT 2:
I noticed when i put my $_SESSION in a variable in my editVacancy controller method (which is being redirected to) and var_dump it in my view that this does not contain the ci_vars with the flashdata in.
Instead of using set_header you can use simple redirect function for it.
redirect("dashboard/vacancy/editVacancy/".$vacancyid);

passing array value to a controller and dispalying in a view In Codeigniter

I am new to Code Igniter. I am trying to pass an array from one controller to another to another controller and then displaying those value in a view. but its not working.
Controller No. 1
if($query)
{
$data = array(
'user' => $this->input->post('email'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('site/member_area',$data);
}
else {
$this->index();
}
Controller No. 2 having member_area() function
function member_area()
{
$data['title'] = 'Dashboard';
$data['main_content'] = 'members_area';
$this->load->view('includes/main_template',$data);
}
And in view i am trying to display display the 'user' name received in controller one using this statement in view
echo $user;
But its not displaying user name i.e $data['user']. the view is having only values received from controller No. 2
You can define you session variables as
$this->session->set_userdata('mysession',$data);
and can access values from the session as
echo $this->session->userdata['mysession']['user'];// your email or username
You need some changes within your code. As you were calling session value as simple array. This'll work for you..
controller 1
$data = array(
'user' => $this->input->post('email'),
'is_logged_in' => true
);
$this->session->set_userdata('retrieveData',$data);
and within your view you can access that using
$retrieveData = $this->session->userdata('retrieveData'); // will return the array
$userid = $this->session->userdata['retrieveData']['user'];// retrieving single element from session
echo $userid;

Editing more than one field in Codeigniter

Currently, editing profile works for me. However, my problem is, only one element is successfully edited. I can echo previously saved data, and I can even type and edit each of the textboxes. Problem is, only the revision made on "profile" field is properly reflected. All the other fields remain the same.
here's what I have so far:
in controller page:
public function edit_profile()
{
//loads client profile and allows editing to be done
$this->validateRole('client');
$this->load->model('job_model');
$id = $this->auth_model->get_user_id();
$data['client'] = $this->auth_model->get_client($id);
$this->load->view('client/edit_profile', $data);
}
public function edit_profile_submit()
{
$this->validateRole('client');
$this->load->model('auth_model');
//$this->auth_model->edit_client_profile($this->auth_model->get_user_id(), $_POST['tagline']);
$this->auth_model->edit_client_profile($this->auth_model->get_user_id(), $_POST['profile']);
//$this->auth_model->edit_client_profile($this->auth_model->get_user_id(), $_POST['billing_mode']);
redirect('client/view_profile?message=Profile updated.');
}
in model page:
public function edit_client_profile($id, $profile)
{
// allows client to edit his or her profile
$data = array(
//'tagline' => $tagline,
'profile' => $profile
//'billing_mode' => $billing_mode
);
$this->db->where('id', $id);
$this->db->update('client', $data);
}
I tried editing my controller and model page only to get errors so I commented the added lines for now instead.
I am looking forward to any possible help.
Thanks!
The error is probably because you have two undefined variables in your $data array each time you call the model. Instead of creating the array in your model, create it in your controller:
$data = array(
'tagline' => $_POST['tagline'],
'profile' => $_POST['profile'],
'billing_mode' => $_POST['billing_mode']
);
Then call the model
$this->auth_model->edit_client_profile($this->auth_model->get_user_id(), $data);
Remove the array creation from your model, change the second parameter being passed to $data:
public function edit_client_profile($id, $data)
{
$this->db->where('id', $id);
$this->db->update('client', $data);
}

Categories