Editing more than one field in Codeigniter - php

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);
}

Related

Update Codeigniter form - 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 :-(

cakephp how to send data from controller to view with json

Hi I'm trying to make an API to send data encapsulating with json.
as cakephp manual said, I added extensions in routes.php
$routes->extensions(['json]);
and I've made an index function in controller.
public function index(){
$item = $this->Items->find('all');
$this->set(['items' => $items, '_serialize' => ['items']]);
}
here are the problem.
what should i do after this to make api encapsulating with json??
Please help.
thank you
According to Cake 2.x Book (http://book.cakephp.org/2.0/en/development/rest.html)
You have to add this to your routes.php file:
Router::mapResources('items');
Router::parseExtensions();
Then, in your items controller, add the RequestHandler to your components array:
public $components = array('RequestHandler');
Then, in your items controller, add your methods, in your example:
public function index() {
$recipes = $this->Items->find('all');
$this->set(array(
'items' => $items,
'_serialize' => array('items')
));
}
Note: according to model names convention you should call $this->Item instead of $this->Items unless you previously defined the model name as "Item" (singular) in your item model file.
Finally, the API is done, you can access to yourprojecturl/items.json and see the json result.
I had trouble with RicardoCamacho's code until I used the $recipes, which is $this->Items->find('all'); in the $this->set(array...
public function index() {
$recipes = $this->Items->find('all');
$this->set(array(
'items' => $recipes,
'_serialize' => array('items')
));
}

Edit and delete issues in codeigniter

I tried some of the steps here, but until now, my edit and delete functions do not work correctly. Can you please help me with it?
First, I have these lines in my client controller:
public function edit_job
{
$this->validateRole('client');
$this->load->model('job_model');
$id = $this->uri->segment(3);
$data['my_preference'] = $this->array_to_select( $this->job_model->get_all_categories(), 'id','name');
$data['job'] = $this->job_model->get_job($id);
$this->load->view('client/edit_job', $data);
}
public function delete_job()
{
$this->validateRole('client');
$this->load->model('job_model');
$id = $this->uri->segment(3);
$this->job_model->delete_job($id);
//echo '<script language="javascript">alert("Post successfully deleted.");</script>';
redirect('client/manage_jobs?message=Job post successfully deleted');
}
}
Then, I have these lines in my job_model
function edit_job()
{
$data = array(
'title' => $this->input->post('title'),
'category_id' => $this->input->post('category_id'),
'start_date' => date("m-d-Y", strtotime($this->input->post('start_date'))),
'description' => $this->input->post('description'),
);
$this->db->where('id', $this->input->post('id'));
$this->db->update('job', $data);
}
function delete_job($id)
{
$this->db->where('id', $id);
$this->db->delete('job', $data);
}
Currently, add job works quite well (although I sometimes have problem with the start date), but edit and delete do not work well. Whenever I press edit in my view page, it will show a page exactly similar to a new add_job page (so whenever I type and fill in the form, it will be added as a new job, not as a revision of a particular job) (I've been trying to echo the original values first before editing can be done, but it never worked. In terms of delete job, it does show the redirect message, but whenever I check the jobs available, the supposedly deleted job is still available.
I have been stuck here for quite some time now. Please help me..
I think asp_tags is disable in your php settings. Change your code as following:
<?php echo $this->uri->segment(3, 0); ?>
instead of
<?= $this->uri->segment(3, 0); ?>
and about delete, you don't need to pass second parameter in delete function. So do following:
$this->db->delete('job');
instead of
$this->db->delete('job', $data);

Custom delete method doesn't call in custom datasource

I'm using a custom datasource to consume webservice.
Create, Read and Update work well but Delete doesn't works.
Here is my code calling the delete method in my controller.
public function delete($id){
$this->autoRender = false;
debug($this->Article->delete($id));
}
And here the code in my datasource
public function delete(Model $Model, $id = null) {
echo "Display a message if this method is called";
$json = $this->Http->post(CakeSession::read('Site.url') . '/webservice/delete/', array(
'id' => $id,
'apiKey' => $this->config['apiKey'],
'model' => $Model->name
));
$res = json_decode($json, true);
if (is_null($res)) {
$error = json_last_error();
throw new CakeException($error);
}
return true;
}
But when I want to delete an item, the debug(); display false.
I have no other displays.
I don't understand why my delete method isn't called correctly.
Is there something wrong in my code ?
Thanks
Let's check: you're only passing a parameter to your method:
$this->Article->delete($id)
According to the method that you created, the first parameter, which is required, is the Model. The second is $id:
public function delete(Model $Model, $id = null)
During the method you want to use both parameters. Here:
'id' => $id
And here:
'model' => $Model->name
Based on this, you need to review how this method will be called. BTW, if you want override delete() method, according the book, you need something like this: delete(int $id = null, boolean $cascade = true).

How to Retrieve Image Files from Database Using Yii Framework?

Our Yii Framework application has the following defined as part of the UserProfileImages model:
public function getProfileImages($param, $user_id) {
if(isset($param['select']) && $param['select']=='all'){
$profile_images = UserProfileImages::model()->findAllByAttributes( array( 'user_id'=>$user_id) );
} else {
$profile_images = UserProfileImages::model()->findByAttributes( array( 'user_id'=>$user_id) );
}
return $profile_images;
}
How would I wire up the above snippet to a widget in my view to return all the images for a given user?
Bonus Question: Which image rotator do you suggest to render the above?
In your view file, add something like this, assuming that your controller specified $user_id:
$this->widget('UserProfileImagesWidget', array(
"userProfileImages" => UserProfileImages::getProfileImages(
array("select" => "all"),
$user_id
),
"user_id" => $user_id
));
Depending on your MVC philosophy, you could also retrieve the userProfileImages data in the controller and pass that data to your view.
Define a widget like this:
class UserProfileImagesWidget extends CWidget {
public $user_id;
public $userProfileImages = array();
public function run() {
$this->render("userProfileImage");
}
}
Finally, in the userProfileImages.php view file, you can do something like this:
if(!empty($this->userProfileImages)) {
// Your display magic
// You can access $this->user_id
}
As a side note: You might want to change the order of your parameters in getProfileImages. If $user_id is the first parameter, you can leave out $params completely in case you don't want to specify any.

Categories