I am almost through building a simple gallery, but need help on the logic.
The gallery is using code from the CI file upload series.
I basically have a form that displays title, description, and image field to upload an image. I have already set up where the image uploads to a directory and makes a thumb on the server. That was not to difficult.
Now I am trying to upload a title and description with an image id stored in the db.
The image id will have to be joined with the title and description id.
I will also add validation to prevent sql injection.
So, what are the steps(logic) to assign id to an image. Store the images. Relate the title and description id to the image id. So when the gallery is viewed, the title and description will be next to the correct corresponding image.
I created some time ago a simple upload form with:
- Name field for image.
- Description field image.
- Field for the file.
So when it was submited:
- It uploaded the image, checked if the image was uploaded correctly, then creates thumbnail(optional).
- If the first step worked, then takes the image name, the description, and the random name generated if it was set to insert those data in the DB.
I don’t have the code here rigth now but that is the main idea, if you need more help ask
Here is my model. I am able to easily store the title and description, but I cannot get the path and thumb path to be stored in the db. The problem is under function store_images_db.
If you wan i can post my code but it doesnt store the paths, only the id, name, description and the random name generated at the upload.
If you can show me you code that would be great. Come to think of it, there is no point storing the path, because if the path changes, then the paths in the database will be wrong.
Hi again, I’ve some of my code here i’ll put some parts maybe it can help you.
You are right. If the path is changed it would be a mess to change all the records of the database, so I prefer create a variable for the galleries and define their path, so all of them can be saved in their own directory (If you use some kind clasification like carsgallery, moviesgallery….)
Now the upload code. In MY_Model(or MY_Controller, i think is better in model) cause was used by many of them.
// Upload Function // Returns $image_data['file_name'] if the upload of the image and its thumb was correct or it will return errors to the controller. public function subir_archivo($upload_path, $upload_path_thumbs){
//empieza la subida del archivo $config = array( 'upload_path' => $upload_path, 'allowed_types' => 'gif|jpg|png|jpeg', 'max_size' => 1024*2, 'max_width' => 1000, // for example 'max_height' => 1000, // for example 'remove_spaces' => true, 'encrypt_name' => true );
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload()) { $data = $this->upload->display_errors();
This should be enough for your needs.
This should work (in fact it works actually in a web) but its for single upload, If you need multiple upload at the same time the code have to be revised.
For multiple there are some alternatives, I use a code that uses twitter bootstrat and CI.
I read your codes.
And I noticed you broke the MVC rule.
Some of your methods like do_upload() should not be in the model.
It should be in the controller or library.
And catching the data from a form is supposed to be in the controller as well,
not in the model.
The codes that should be in the model/MY_Model is the CRUD.
C = Create record
R = Read record
U = Update record
D = Delete record
Also before diving into a project.
You should have a plan. Try to visualize everything first.
And use the Divide and conquer method.
You can use flowchart but if you are in a hurry you can use the comments method.
Here is a basic example of ‘Comment Method’,
controller:
- catch data
- validate data
- upload image
- resize image
model:
- store info like img_id, title, image_name, description
view:
- display upload form
- display error message
- display successful upload page
Here is another way to visualize your plan for your codes using the ‘Comment method’.
Step 1 - Display Upload Form (view)
Step 2 - validate data from controller (controller)
- If it has error display form again with error
Step 3 - Capture valid data (controller)
Step 4 - resize image for thumbnail (controller)
Step 5 - store images into image_path/image_folder (controller)
step 6 - When everything is right then store information into DB. (model)
- When record insert is successful return to controller
Step 7 - If insert record is a success display ‘Success Message’ (view)
etc, etc, etc…
The key to right coding is to plan it ahead.
(Visualize it, you can use flowchart or Comment method).
Do it step by step, you can modify the steps above if it doesn’t meet your needs.
@jojo777
Thank you so much for the example. I think I now see that I just need to set the image path as a variable and simply pass to the database. I just need to tweak a couple things.
I will post my finished code soon.
@solid9
You are God sent man. I should plan better next time. I only planned a few of the steps you mentioned. I hurried because of the lack of time I have to do what I love, programming. When you manage a well known restaurant, there is not much time for anything else.
I will take what you said to heart, plan efficiently, and re-arrange the code in its proper place (MVC) and post soon. Thank you for your time.