06-11-2019, 04:58 AM
(This post was last modified: 06-11-2019, 06:56 AM by ciadmin.)
Hello Everybody,
I have a controller called Home.php & I have method called addProperty() here process the image upload. But the problem is when i use the image upload code after $this->form_validation->run() , its not working.
But without $this->form_validation->run() , the image upload working. When i use the image upload code after the $this->form_validation->run() the image is not getting uploaded.
PHP Code: function addProperty() {
$this->load->library('form_validation');
//$this->form_validation->set_rules('prop_title','Villa Title','trim|required|max_length[128]'); // $this->form_validation->set_rules('prop_status','Status','trim|required'); //$this->form_validation->set_rules('prop_type','Type','trim|required'); //$this->form_validation->set_rules('prop_price','Price','trim|required|numeric'); //$this->form_validation->set_rules('prop_sqft','Sqft','trim|required|numeric'); $this->form_validation->set_rules('prop_rooms','Rooms','trim|required|numeric'); $this->form_validation->set_rules('prop_address','Address','trim|required|max_length[200]'); $this->form_validation->set_rules('prop_owner_phone','Mobile Number','required|min_length[10]'); $this->form_validation->set_rules('prop_owner_name','Owner Name','required|min_length[5]'); $this->form_validation->set_rules('prop_owner_email','Owner Email','required|valid_email'); //$this->form_validation->set_rules('prop_detailed_info','Detailed Info','trim|required|max_length[2000]'); //$this->form_validation->set_rules('prop_status','Current Status','trim|required');
if($this->form_validation->run() == FALSE) { $this->listProperty(); } else {
$prop_title = ucwords(strtolower($this->security->xss_clean($this->input->post('prop_title')))); $prop_status = ucwords(strtolower($this->security->xss_clean($this->input->post('prop_status')))); $prop_type = ucwords(strtolower($this->security->xss_clean($this->input->post('prop_type')))); $prop_price = $this->security->xss_clean($this->input->post('prop_price')); $prop_sqft = $this->security->xss_clean($this->input->post('prop_sqft')); $prop_rooms = $this->security->xss_clean($this->input->post('prop_rooms')); $prop_address = $this->security->xss_clean($this->input->post('prop_address')); $prop_owner_name = $this->security->xss_clean($this->input->post('prop_owner_name')); $prop_owner_email = $this->security->xss_clean($this->input->post('prop_owner_email')); $prop_owner_phone = $this->security->xss_clean($this->input->post('prop_owner_phone')); $prop_detailed_info = $this->security->xss_clean($this->input->post('prop_detailed_info')); $prop_current_status = $this->security->xss_clean($this->input->post('prop_current_status'));
//$userInfo = array('email'=>$email, 'password'=>getHashedPassword($password), 'roleId'=>$roleId, 'name'=> $name, //'mobile'=>$mobile, 'createdBy'=>$this->vendorId, 'createdDtm'=>date('Y-m-d H:i:s'));
$propertyInfo = array('title'=>$prop_title, 'status'=>$prop_status, 'type'=>$prop_type, 'price'=>$prop_price, 'sqft'=>$prop_sqft, 'rooms'=>$prop_rooms, 'address'=>$prop_address, 'name'=>$prop_owner_name, 'email'=>$prop_owner_email, 'phone'=>$prop_owner_phone, 'detailedInfo'=>$prop_detailed_info, 'currentStatus'=>$prop_current_status);
$this->load->model('property_model'); $result = $this->property_model->addNewProperty($propertyInfo); }
if($result > 0) { if (!empty($_FILES)) { $is_image = "Yes"; $count_img = count($_FILES['file']['name']);
foreach($_FILES['file']['tmp_name'] as $key => $value) {
$tempFile = $_FILES['file']['tmp_name'][$key]; $fileName = $_FILES['file']['name'][$key]; $targetPath = 'property-images/'; $targetFile = $targetPath . $fileName ; if(move_uploaded_file($tempFile, $targetFile)) {
$imgData = array('path'=>$targetPath,'propertyid'=>$result); $prop_push = $this->property_model->addPropertyImgs($imgData);
}
} }
//$propertyImgUpload = uploadImages($result); $this->session->set_flashdata('success', 'Thank you for submiting.'.$result.'. your property!, Our team will contact you shortly');
} else { $this->session->set_flashdata('error', 'Submission of property failed, Contact [email protected] }
redirect('/submit-property');
}
(06-11-2019, 09:23 AM)php_rocs Wrote: @sarath_unrelax,
What value is $this->form_validation->run() kicking out?. Seems like you need to do some troubleshooting. Check your validation statements. They may be kicking out an unexpected value.
Other thing i tried that worked out is
PHP Code: $imgData = array('path'=>$targetPath,'propertyid'=>$result);
Here i passed property_id = 10 ( manual value) instead of result value. Then it working !
-
demyr
Senior Member
-
Posts: 330
Threads: 17
Joined: Sep 2018
Reputation:
7
Instead of trying this one
PHP Code: if($this->form_validation->run() == FALSE) { $this->listProperty(); } else {
can you please try :
PHP Code: if($this->form_validation->run()) { //the working result comes here } else { $this->listProperty();}
06-11-2019, 12:22 PM
(This post was last modified: 06-11-2019, 12:57 PM by ciadmin.)
@ php_rocs
Buddy, I tried below code as per your advice. But still not working !!
PHP Code: <?php if(!defined('BASEPATH')) exit('No direct script access allowed');
/** * Class : Login (LoginController) * Login class to control to authenticate user credentials and starts user's session. * @author : Kishor Mali * @version : 1.1 * @since : 15 November 2016 */ class Home extends CI_Controller { /** * This is default constructor of the class */ public function __construct() { parent::__construct(); $this->load->model('property_model');
}
/** * Index Page for this controller. */ public function index() { $data = array('title'=> 'Vierra Property Broker', 'main_content'=>'home'); $this->load->view('template', $data);
}
/** * Loaading the submit property form. */
public function listProperty()
{
$data = array('title'=> 'Vierra Property Broker - Submit your Property', 'main_content'=>'submit_property'); $this->load->view('template', $data);
}
function addProperty() {
$this->load->library('form_validation');
//$this->form_validation->set_rules('prop_title','Villa Title','trim|required|max_length[128]'); // $this->form_validation->set_rules('prop_status','Status','trim|required'); //$this->form_validation->set_rules('prop_type','Type','trim|required'); //$this->form_validation->set_rules('prop_price','Price','trim|required|numeric'); //$this->form_validation->set_rules('prop_sqft','Sqft','trim|required|numeric'); $this->form_validation->set_rules('prop_rooms','Rooms','trim|required|numeric'); $this->form_validation->set_rules('prop_address','Address','trim|required|max_length[200]'); $this->form_validation->set_rules('prop_owner_phone','Mobile Number','required|min_length[10]'); $this->form_validation->set_rules('prop_owner_name','Owner Name','required|min_length[5]'); $this->form_validation->set_rules('prop_owner_email','Owner Email','required|valid_email'); //$this->form_validation->set_rules('prop_detailed_info','Detailed Info','trim|required|max_length[2000]'); //$this->form_validation->set_rules('prop_status','Current Status','trim|required');
if($this->form_validation->run()) {
$prop_title = ucwords(strtolower($this->security->xss_clean($this->input->post('prop_title')))); $prop_status = ucwords(strtolower($this->security->xss_clean($this->input->post('prop_status')))); $prop_type = ucwords(strtolower($this->security->xss_clean($this->input->post('prop_type')))); $prop_price = $this->security->xss_clean($this->input->post('prop_price')); $prop_sqft = $this->security->xss_clean($this->input->post('prop_sqft')); $prop_rooms = $this->security->xss_clean($this->input->post('prop_rooms')); $prop_address = $this->security->xss_clean($this->input->post('prop_address')); $prop_owner_name = $this->security->xss_clean($this->input->post('prop_owner_name')); $prop_owner_email = $this->security->xss_clean($this->input->post('prop_owner_email')); $prop_owner_phone = $this->security->xss_clean($this->input->post('prop_owner_phone')); $prop_detailed_info = $this->security->xss_clean($this->input->post('prop_detailed_info')); $prop_current_status = $this->security->xss_clean($this->input->post('prop_current_status'));
//$userInfo = array('email'=>$email, 'password'=>getHashedPassword($password), 'roleId'=>$roleId, 'name'=> $name, //'mobile'=>$mobile, 'createdBy'=>$this->vendorId, 'createdDtm'=>date('Y-m-d H:i:s'));
$propertyInfo = array('title'=>$prop_title, 'status'=>$prop_status, 'type'=>$prop_type, 'price'=>$prop_price, 'sqft'=>$prop_sqft, 'rooms'=>$prop_rooms, 'address'=>$prop_address, 'name'=>$prop_owner_name, 'email'=>$prop_owner_email, 'phone'=>$prop_owner_phone, 'detailedInfo'=>$prop_detailed_info, 'currentStatus'=>$prop_current_status);
$this->load->model('property_model'); $result = $this->property_model->addNewProperty($propertyInfo);
if($result > 0)
{
if (!empty($_FILES)) { $is_image = "Yes There is Image!"; $count_img = count($_FILES['file']['name']); foreach($_FILES['file']['tmp_name'] as $key => $value) {
$tempFile = $_FILES['file']['tmp_name'][$key]; $fileName = $_FILES['file']['name'][$key]; $targetPath = 'property-images/'; $targetFile = $targetPath . $fileName ; $imgData = array('path'=>$targetFile,'propertyid'=>$result); if(move_uploaded_file($tempFile, $targetFile)) {
$prop_push = $this->property_model->addPropertyImgs($imgData);
}
}
//$propertyImgUpload = uploadImages($result); $this->session->set_flashdata('success', 'Successfuly inserted record & .'.$is_image.'. ,Last Insert ID is.'.$result.'. your property!, Our team will contact you shortly');
} else { $this->session->set_flashdata('error', 'The image not posted');
} } else { $this->session->set_flashdata('error', 'Submission of property failed, Contact [email protected] '); }
}
else { $this->listProperty();
}
redirect('/submit-property');
}
}
?>
The output is :
The image not posted
(06-11-2019, 02:03 PM)demyr Wrote: That's probably they're 2 different posts and you are trying to combine the second one (uploading image) to the first one. But they're not in the same form->validation ?? Try combining them together or put them in seperate views.
I hope this page will help you about form validation for uploads. Buddy !
I got the point. But i need the last inserted id to insert image details to table. Then how i i can do that?
Upload image option i used for uploading property images along with property data.
If i use two post into differently how i can get the refeence of the last insert record. I mean the $result variable.
-
demyr
Senior Member
-
Posts: 330
Threads: 17
Joined: Sep 2018
Reputation:
7
When you get to the point of sending
Code: $data = array('your_column'=>$variable);
to the database, do not output the result. Create another $data, for example : $data2
Code: $data2 = array('your_column_about_image'=>$variable_for_image);
They both should have the same query name. For example $my_result :
Code: $data = array(bla bla);
$this->load->models('My_Backend_Model');
$my_result = $this->My_Backend_Model->lets_insert_form_post($data);
-- dont go if($my_result){} here--
start your secont $data2 :
Code: $data2 = array(bla bla);
$this->load->models('My_Backend_Model');
$my_result = $this->My_Backend_Model->lets_upload_photo($data2);
then run your checking code :
PHP Code: if($my_result){ $this->session->set_flashadata('done','bla bla'); redirect('somewhere'); }else{ bla bla }
If I use both in same query how I can get the last inserted id from
PHP Code: $my_result = $this->My_Backend_Model->lets_insert_form_post($data);
To insert image I need the last inserted id return from the above query. Then only I can do
PHP Code: $my_result = $this->My_Backend_Model->lets_upload_photo($data2);
Because
PHP Code: $data2 = array('path'=>$imagePath,'propertyid'=>$propertyid);
Here I want to pass the id as propertyid returned from the first query. How I can use then if we use the both operations in same variable ?
-
demyr
Senior Member
-
Posts: 330
Threads: 17
Joined: Sep 2018
Reputation:
7
After your first $data, get the last_id
Code: $this->load->model('My_Backend_model');
$result = $this->My_Backend_model->lets_insert_form_post($data);
$lastid = $this->db->insert_id();
then within your second $data which is named as $data2
Code: $data2=array(
'bla bla column from your db' => $array,
'propertyid' => $lastid
);
then send this $data2 to Model as well.
|