问题
Ok so this question is not so much about the coding procedure, but more like a good and clean way to code this activitylist, i plan to build..
This activitylist will contain all activities on the page.
So basically on all the actions made on the page, it will call something like a function insertActivity($stuff)
to insert the activity about that e.g you just did something.
Now I am wondering the coding way, how should I do it?
Should I have the message "you just did something" in the row in db or should i have numbers where in my php file i define that number 1 stands for "you just did something"?
With message not stored in db will help me editing the message for all the activitymessages later, e.g if the message should be changed to "you just ate something".
Should i do it like i said previously about calling a function before/after the action saves, to log this action into the activitylist, or any other prefferable way to do it?
What i had in mind was just that insertActivity() should just insert the activity to the database, by the inputs ($stuff) it got.
Really appreciate a simple and clean coding way to do this.
回答1:
That is... an impossible question. It isn't impossible in the fact that it is difficult to answer, it isn't impossible in that it is impossible to give a good answer, it is impossible because there are many, many different good answers and no "right" answers.
There is a lot to be said about having two tables, one is just for descriptions, the other is has a foreign key which will reference a description ID. Retrieving the value is as simple as:
SELECT -- your other columns can go here.
(SELECT DESCRIPTION FROM DESCRIPTIONS DE
WHERE DE.ID = DID.DESCRIPTION_ID LIMIT 1) as ACTIVITY
-- I always add a LIMIT in a sub-query...
-- your other columns can go here.
FROM DID_THINGS DID WHERE USER_ID = 1;
MySQL makes this type of structure a little bit easier if there are a reasonably finite number of activities (and those are known beforehand) -- you should look into the ENUM type. With ENUM's, however, that becomes the most annoying way of modifying the site. Personally, I like the second table option and I use it very frequently. If I want to have it so that an admin can add something like "You watched something," I can simply expose that via a web form and keep the precious keys to the database hidden away where no-one knows them, including me.
Another frequent way of doing things is to simply have an associative array (or a hash, or whatever). The benefit of this is that it may not involve a database connection, it is very simple and it is very easy and obvious to test and debug. Because of these (among others), it is very common to find this used in the internationalization scripts. The problem is that in order to add something to one of those arrays, you will need to actually go, modify the file, and then re-deploy the file to all versions of the site. Now, often this is something trivially simple, but it is an extra step. It is very difficult (though not strictly impossible) to allow a user to add to the list, and it is even more annoying to edit the data once entered.
回答2:
Storing just id of an action is good idea. You can also create "actions" table where you will have stored all possible actions.
This way you will safe some disk space and make the solution more flexible.
You should log the action after the action was made. This way you can validate if the action was successful or not.
The question is very general, so it's hard to provide more concrete answer. Provide more information if you need to help with something else.
回答3:
It depends
- will be there millions of rows?
- do you care about disk space?
- will have user chance to see that log?
You should look at those dependencies and think about it.
来源:https://stackoverflow.com/questions/6892613/way-coding-method-to-do-activitylist-log