duanniu4106 2011-07-21 16:06
浏览 22
已采纳

PHP复杂的Sprintf

I'm doing a system to log users' actions, such as the purchases it made, friends it added, among anothers. For example, when a user purchase some products, the following message should appear in your profile:

Foo bought the products A, B, C, D and F.

I could save the whole string in the database, but the site will have more than one language, which will make unviable keep a record for each language. My idea is to have a string with the template of the action taken and pass it to the sprintf function, however, the amount of products may change, so the number of arguments as well. In this situation, how should I proceed?

Edit #1: My question is not about the database schema, the schema I'm using is this: Databases: Making a Log of actions, how to handle various references?, The question is how to make a string to be used with sprintf that can receive as many arguments as needed. Something like this: "%s bought the products [magic here]"

  • 写回答

1条回答 默认 最新

  • doushan7997 2011-07-21 16:09
    关注

    Have a unique code for each action. Store the action in a number of fields in a table called log:

    user      action      products
    -----------------------------------
    Foo       BUY         A|B|C|D
    

    Map BUY to human-readable descriptions in your application as required.

    You could separate out the individual products if required, to achieve full normalisation... though if you don't need anything more with them and their representations are concise IDs (with no | in them!) then tokenisation will do fine.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?