PHP/MySQL - 将默认文本存储在数据库或文件中?
好的,我正在创建一个系统,每当商店注册时,他们都会获得 10 张默认幻灯片,然后可以自定义这些幻灯片。默认幻灯片数据只是他们的起点。但我必须假设很多商店不会登录并更新信息,他们只是想尽快更新。
因此,对于仪表板幻灯片来说,每个商店都以相同的标题和文本开始,直到他们自行更新。我是否将其插入数据库中,如下所示:
id | slide_id | user_id | headline | text
1 | 1 | 1000 | ... | ....
2 | 1 | 1001 | ... | ....
因此数据库将包含该标题和文本的许多重复项,因为它是默认的。这是个好主意吗?
或者我做这样的事情:
define("DEFAULT_TEXT", "The text goes here and just used once in a file.");
$text = ($stmt->num_rows < 1 ? DEFAULT_TEXT : $text);
所以我有一个查询,搜索我的 slide_data 表,看看该幻灯片 ID 是否存在任何内容,如果存在,则只需使用该标题和文本。但如果没有行,这意味着它们尚未添加任何内容,则只需使用常量 DEFAULT_TEXT
。这样,我可以拥有不同的语言配置文件并在其中进行翻译,因此如果语言是法语,那么它将使用 DEFAULT_TEXT_FR 常量来进行手动翻译。
您推荐哪一款?我不确定我的想法是否正确,所以寻找其他一些意见和建议。
谢谢!
OK so I am creating a system where whenever a shop signs up, they get 10 default slides that they can then customize later. The default slide data is just a starting point for them. But I have to assume a lot of shops will not log in and update the information, they just want something up ASAP.
So say for the Dashboard slide, every shop starts out with the same heading and text until they go in and update it themselves. Do I insert it in the database, like so:
id | slide_id | user_id | headline | text
1 | 1 | 1000 | ... | ....
2 | 1 | 1001 | ... | ....
So the database will contain many duplicates of that headline and text since it is the default. Is that a good idea?
Or do I do something like this:
define("DEFAULT_TEXT", "The text goes here and just used once in a file.");
$text = ($stmt->num_rows < 1 ? DEFAULT_TEXT : $text);
so I have a query that searches through my slide_data table to see if anything exists for that slide id, and if it does, then just use that headline and text.. but if there are no rows, which means they have not added anything yet, then just use the constant DEFAULT_TEXT
. And that way, I can have different language config files and have translations in there, so if the language is French then it will use the DEFAULT_TEXT_FR
constant which will have the manual translation.
Which one do you recommend? I'm not sure if I have the right idea here, so looking for some other opinions and suggestions.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您执行此操作的方式不一定不正确,但正如您所暗示的那样,您最终可能会得到大量重复数据。
您可能需要考虑将它们包装到一个对象中,例如 ShopSlides,它保存默认值和 Shop 配置的值。
非常快速的形式可能是这样的:
The way you are doing it is not necessarily incorrect, but as you hint, you may end up with a large amount of duplicate data.
You may want to consider wrapping these into an object such as ShopSlides which holds default values and configured values by Shop.
This could in a very quick form be something like this:
我更喜欢常量解决方案,因为您可以避免数据冗余,并且可以轻松自定义默认消息,而无需更新大量记录......
I'd prefer the constants solution because you can avoid data redundance and you can easily customize default messages without the need to update a lot of records...
简单的。您只需解析管理员成员 shopSlides 即可。这样您就可以添加和管理所有默认的 ShopSlide !只需在您的表格中添加一列“isDefaultShopSlide”即可!
Easy. You just have to parse the admin member shopSlides. That way you will be able to add and manage all your default ShopSlide ! Just add a column "isDefaultShopSlide" to your Table!
我会在数据库外部定义默认文本,将其存储在数据库中会创建不必要的重复条目,这最终会随着时间的推移影响性能(数据库表越大,查询该表所需的时间就越长)。定义后,只需按用户 ID 运行查询,看看他们是否为该幻灯片创建了文本,否则默认为您的预定义文本
编辑:
I would define the default text outside of the database, storing it in the db creates unecessary duplicate entries which will ultimately effect performance over time (the larger a db table gets the longer it takes to query that table). Once you have it defined just run a query by user id to see if they created text for that slide, otherwise default to your predefined text
Edit: