[eluser]darrentaytay[/eluser]
You will need to do add a snippet along the lines of this to your controller. Basically, this only adds your image data to your array that you pass to the model if a new image has been presented.
Code: if (!empty($_POST['userfile']['name']))
{
// call the upload function
// if the upload function succeeds
// pass the image information to the model
if ($this->upload->do_upload())
{
$content['imagename'] => $file_info['file_name'];
$content['thumbname'] => $file_info['raw_name'].'_thumb'.$file_info['file_ext'];
}
}
Now we can check if there is a new image in the model by simply testing if the imagename & thumbname keys are passed through.
MODEL:
Code: function updateSale($id, $content) {
$this ->db->where('id', $id);
$update = $this->db->get('sales');
$row = $update->row_array();
if($update->num_rows() > 0) {
if (isset($content['imagename']) && isset($content['thumbname']))
{
#lets delete the image
unlink("/includes/uploads/gallery/".$row['imagename']);
#lets delete the thumb.
unlink("/includes/uploads/gallery/thumbs/".$row['thumbname']);
}
$this->db->update('sales', $content);
} # End IF
} #
[eluser]jzmwebdevelopement[/eluser]
Code: if ($this->upload->do_upload())
{
$content['imagename'] => $file_info['file_name'];
$content['thumbname'] => $file_info['raw_name'].'_thumb'.$file_info['file_ext'];
}
}
After what section would this be placed?
[eluser]darrentaytay[/eluser]
You seem to have it at the correct place on your updated stack overflow code.
[eluser]jzmwebdevelopement[/eluser]
Ok thanks Darren, Will the model adjustment fix my error confusion?
[eluser]darrentaytay[/eluser]
Yeh it should do because now in your model you will first be checking to see if a new image & thumbnail exists before deleting the old ones.
[eluser]jzmwebdevelopement[/eluser]
Thanks mate, Will report back, Have shut everything down -> was up all night doing this.
[eluser]jzmwebdevelopement[/eluser]
Hey Darren,
I have tried what you have said and it seems that if I have more then one sale and update the image it seems to change all data in the other sales to the one that I have chosen to update and not upload the image.
Why?
View:
Code: <?php if($sales_pages): ?>
<?php foreach($sales_pages as $sale): ?>
<div id ="sales">
<span class"thumbnail"><a >id?>"><img class="thumbnailImg">thumbname?>" alt=""/></a>
<span class="location"><?= $sale->location?></span>
<span class="price"><?= $sale->price?></span>
</span>
</div>
<?php endforeach; ?>
<?php endif; ?>
Controller
Code: class Editsale extends CI_Controller {
/**
* This document sets the following for the editsale page
*PHP Validation
*Thumbnail and Image Sizes
*Image Locations
*DB Settings
* @author Jess McKenzie
*/
function __construct() {
parent::__construct();
}
function index() {
if(!$this->session->userdata('logged_in'))redirect('admin/home');
$id = $this->uri->segment(4);
# Set Main Page Data
$data['title'] = 'Edit Sale:';
$data['sales_pages'] = $this->sales_model->getSalesPages();
$data['cms_pages'] = $this->navigation_model->getCMSPages();
$data['sale']= $this->sales_model->getSalesContent($id);
#Set The Validation Rules
$this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean');
$this->form_validation->set_rules('location', 'Location', 'trim|required|xss_clean');
$this->form_validation->set_rules('bedrooms', 'Bedrooms', 'trim|numeric|required|xss_clean');
$this->form_validation->set_rules('bathrooms', 'Bathrooms', 'trim|numeric|required|xss_clean');
$this->form_validation->set_rules('condition', 'Condition', 'trim|required|xss_clean');
$this->form_validation->set_rules('description', 'Description', 'trim|required|xss_clean');
$this->form_validation->set_rules('price', 'Price', 'trim|required|xss_clean');
if($this->form_validation->run()) #If Valid Run
{
$content = array(
'name' => $this->input->post('name', TRUE),
'location' => $this->input->post('location', TRUE),
'bedrooms' => $this->input->post('bedrooms', TRUE),
'bathrooms' => $this->input->post('bathrooms', TRUE),
'condition' => $this->input->post('condition', TRUE),
'description' => $this->input->post('description', TRUE),
'price' => $this->input->post('price', TRUE)
);
if($_FILES['userfile']['error'] != 4)
{
//Set File Settings
//Set File Settings
$config['upload_path'] = 'includes/uploads/gallery/';
$config['allowed_types'] = 'jpg|png';
$config['remove_spaces'] = TRUE;
$config['overwrite'] = TRUE;
$config['max_size'] = '1024';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
//Continue and resize the image
$data = array('upload_data' => $this->upload->data());
$config['image_library'] = 'GD2';
$config['source_image'] = $this->upload->upload_path.$this->upload->file_name; #resize
$config['new_image'] = 'includes/uploads/gallery/thumbs/';
$config['create_thumb'] = 'TRUE';
$config['thumb_marker'] ='_thumb';
$config['maintain_ratio'] = 'FALSE';
$config['width'] = '175';
$config['height'] = '175';
$this->load->library('image_lib', $config);
$this->image_lib->resize();
if(!$this->upload->do_upload()) //Process the upload function and uploads image
{
$data['message'] = array('imageError' => $this->upload->display_errors()); // Capture any $config errors and output them
}
else
{
$file_info = $this->upload->data(); //Returns an data array
// Append image data to content array in preparation for db update
$content['imagename'] = $file_info['file_name'];
$content['thumbname'] = $file_info['raw_name'].'_thumb'.$file_info['file_ext'];
}
}
// After everything, update DB
if($this->sales_model->updateSale($id, $content))
{
$data['success'] = TRUE;
}
else
{
$data['success'] = FALSE;
}
} # End Form Validation
[eluser]darrentaytay[/eluser]
I gotta go to work soon so I'll have a propert look when I get there but for now, can you ensure all the correct information is getting passed to the updateSale method in your model? Do a print_r of the content array.
You're going to want to do this twice;
1st time, upload an image and make sure the image data is being passed
2nd time, don't upload an image and make sure there is no image data being passed
[eluser]jzmwebdevelopement[/eluser]
Will do this shortly,
Just making my final portfolio dvd
Thanks Darran
|