Question

I'm working on creating a web application using php codeigniter. Essentially this application will have an unlimited amount of users who can add their own products to their own account to track an inventory of products for events.

I have a database with a table for users, events, and products.

My question is, how should I set up a table/ or tables for an unlimited amount of products, unlimited amount of users, and unlimited amount of events. And make it so there is a rolling tally for the inventory.

I'm looking at creating some way of entering how much of the products they bring in to an event and how much they leave with.

Any Ideas?

Was it helpful?

Solution

What you seem to need is a many-to-many relationship between all three of your tables. I presume you're using mysql for you database, so you will want to make cross-reference tables 'in between' your current tables.

A simple diagram:

user -> user_product <- product
user -> user_event <- event
product -> product <- event

The outside tables have a one-to-many relationship with the middle tables, which you will reference with a foreign-key. This way you can simply count the rows in the crossref tables to get your totals. I hope this makes sense. Personally, I use Propel ORM (object relational mapping) with my CI installations for all my database needs. There are other ORMs that may handle this type of relationship better, but Porpel is the easiest to get working, in my opinion.

Feel free to ask for more clarification.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top