CodeIgniter Forums
Building a Food Recipe website. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: Best Practices (https://forum.codeigniter.com/forum-12.html)
+--- Thread: Building a Food Recipe website. (/thread-1687.html)



Building a Food Recipe website. - bellsdesign - 04-01-2015

I an very new to codeigniter. And i successfully build a small CMS witch I am using for the base foundation for the website. You can find the working site here:
http://clanguild.bellswebdesign.com/  

and the admin is here:
http://clanguild.bellswebdesign.com/admin/user/login  

with a user name: [email protected] and password: pass

Now for my problem. I need help with the add recipe php page. That one page puts info into 4 tables:

recipes database
[Image: recipes.png]

ingredients database
[Image: ingredients.png]

directions database
[Image: directions.png]

measurements database
[Image: measurements.png]

and my database model
[Image: database_model.png]

Now I have put thinking about this long time and I can not get it. The problem I will run into later is. Every user will have its own database with only there recipes. I was thinking of putting the user_id on every recipe. Is this way I should do it or is there a better why. Please i may have more problems later to. And this website is my first project and I will have more coming cuz I am trying to build a web company so may have room for more staff but much later. Any help is good. I attach some of my work. [attachment=152]


RE: Building a Food Recipe website. - silentium - 04-01-2015

You need the users ID added to each entry in the recipes table, so that you know what recipes belong to what user.
But you do not need to have the users ID in the other tables though, they only need the "parents" id. That would mean recipes ID for ingredients & direction tables. And measurements only need the ingredients ID.

In a try to describe the relations.
  1. A users have a recipe.
  2. A recipe have some ingredients and some directions.
  3. A ingredient have a measurement.

I would do the setup this way
Users
id
name
email
password

Recipes
id
users_id
name
image
rating

Ingredients
id
recipes_id
name
sive

Directions
id
recipes_id
direction
prep_time
cook_time

Measurements
id
ingredients_id
name

I would also recommend setting up relations so that if a recipe is deleted from the database, all entries in ingredients, directions and measurements that belong to that recipe get deleted from the database automatically. Here is some information on that.


RE: Building a Food Recipe website. - bellsdesign - 04-01-2015

Any thoughts on my problem with the recipe page. I need help with the model. Should I have a model for every table or can I do it all one. On one page I'm trying to add to four different tables. The recipe table, ingredients table, descriptions table and the measurement table.


RE: Building a Food Recipe website. - RobertSF - 04-02-2015

(04-01-2015, 06:56 PM)bellsdesign Wrote: Any thoughts on my problem with the recipe page. I need help with the model. Should I have a model for every table or can I do it all one. On one page I'm trying to add to four different tables. The recipe table, ingredients table, descriptions table and the measurement table.

If every user will have their own recipies, then sure, each recipe should have the id of the user that owns it.

As far as a model for every table, no, you can certainly include more than one model per table. In the case of users, you would have one model and one table, users. That's because when you add users or modify their information, you only need to access the users table. But if you're adding or editing recipies, or anything that involves related tables, then you include all those tables in one model.

I don't mean to make your design more complicated, but if you have ingredients linked to a recipe, then you're going to have to duplicate rows in your ingredients table for common ingredients like salt, sugar, onions, tomatoes, etc. You need a third table between recipes and ingredients, like this (skeletal).

RECIPES
id
user_id
name

QUANTITIES
id
recipe_id -- links to recipes table
ingredient_id -- links to ingredients table
quantity

INGREDIENTS
id
name

If someone removes a recipe, you delete the recipe row and the quantity row, but you leave the ingredients table alone because other recipes use it too.

If you're not familiar with the subject, google for "database normal form." There should be some good tutorials out there.