I am making a site which allows users to sign up and record their daily activities in a table. Their activities will be stored in a MySQL database, and will be displayed using jQuery/PHP.
Here is an example picture of the type of data being stored
The problem is, I have no idea how to properly store this type of information.
I was originally going to create a table for each user called $userid . "_activies" or something along those lines, with the columns id, user_id, type, location, duration, date. Then to read the data, I would simply check for what date they want and load all of the activities that match that date. This works fine, but I can’t figure out how to update existing entries properly.
For example: In the picture above, if you wanted to change the duration of the “Kitchen” row, how would you go about detecting if it already exists in the database? You can’t simply check for a row containing ‘cleaning’ and ‘kitchen’ on 11/18/2012 then overwrite it, because Kitchen could appear multiple times in the cleaning section (say they cleaned the Kitchen a second time after going to the Bathroom).
I originally thought about making each <tr> have an id equal to the id field for the data that it loaded, then when it comes time to save the data, check if the row id already exists and overwrite it. This seems like a poor workaround for my issue.
I’d like to be able to just completely clear the original data for the entire day and rewrite it with whatever the table contains when they hit save. Would it be reasonable to just delete every row in the table that matches the date of the table, then write new information? Or is there a better way to store this information. For example, should I create a new table for each day for each user? That seems like a lot of tables…
I’m stumped, can anyone share any ideas on ways to set up the database?
I am a big believer in keeping your data in logical chunks. Never be afraid of using more tables if it means keeping everything logical. Based just on what you said I would have at least the following tables:
User ( user_id, user_name, password, email, …)
Location( location_id, location_Name, …)
Durations ( duration_id, duration_length(increment in 5, 10 or 15 minutes chunks from what would be the smallest duration to what would be the largest))
Activity( activity_id, activity_name, activity_description, activity_user(user_id), activity_date, activity_location(location_id), activity_duration(duration_id)…)
Again, that is based on the elements described in your question. This way, everything would be stored in Activity, and you would obviously want to index activity_user. You would simply add activities into Activity as they are entered, and display them based on activity_user and activity_date. Hope this helps.
ETA: I realized that I didn’t answer your final questions. As I said, you would enter fresh activities into Activity, and, if you wanted a situation where you only maintain the most current records for a given user you simply delete all activities for user_id where activity_date is not the most recent date. It’s tough to give sample code because I’m not sure what you mean exactly. I hope that I at least conveyed the basics properly.