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
Related
sensei. can you help me with my code? i wan to redirect page to the same page after edit it.
here is my controller
function edit_book(){
$editBook = array(
'id' => $this->input->post('id'),
'book_title' => $this->input->post('book_title'),
'category' => $this->input->post('category'),
'author' => $this->input->post('author')
):
$this->m_profile->edit_book($data1, $data, 'book_data');
redirect('app/book')
}
suppose im editing the data from page number 3. how can i redirect to page 3 (app/book/3) again after submit the edited data?
i have try to solve it by get URI value(suppose the page is app/book/3)<-- i need this '3'to put in redirect code. so i implement this code from many stackeroverflow answer, hope to get some array i can use
$url = "//{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
$escaped_url = htmlspecialchars( $url, ENT_QUOTES, 'UTF-8' );
$query_str = parse_url($escaped_url, PHP_URL_QUERY);
parse_str($query_str, $query_params);
print_r($query_params);
but it results array()or null. anyone can help me. thanks
function edit_book(){
$id = $this->input->post('id');
$editBook = array(
'id' => $this->input->post('id'),
'book_title' => $this->input->post('book_title'),
'category' => $this->input->post('category'),
'author' => $this->input->post('author')
):
$this->m_profile->edit_book($data1, $data, 'book_data');
$url = base_url('app/book/'.$id);
redirect( $url)
}
function book($id = NULL)
{
//get record using $id;
}
try below code me be solve u r issue. i have pass id in url.
In the controller need to pass the last value like "3" as argument so that any edited value can be pass to the controller. And then after editing the code you can redirect by the php header function with the passed value as argument to redirect on the same page like this example:
<?php
function edit_book($id){
Your all edited codes here.
// To redirect to the same page with avalue
redirect('same_controller/method/'.$id, 'refresh');
}
?>
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 :-(
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;
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);
}
I'm trying to save data after the same model read it. Here's my code:
public function partner($postid, $partner){
$this->Partner->id = $postid;
$this->Partner->id = $partner;
$this->Partner->ip = $_SERVER['REMOTE_ADDR'];
$result = $this->Partner->read();
if($result){
$time = $result['Partner']['time'];
pr($result);
}
$data = array('id' => $partner,
'post' => $postid,
'time' => time(),
'cash' => '0.001',
'ip' => $_SERVER['REMOTE_ADDR']);
$this->Partner->save($data);
}
It just updates the data, but doesn't save it as a new row
Before $this->Partner->save($data);, call $this->Parter->create();, that will set the Partner model to save a new row.
EDIT: Unless id is your model's primary key and you're setting it to a value that already exists in the database, in that case it will update regardless of whether you use $model->create() or not. (I think).