在 PHP 中拆分并运行代码
我目前正在开展一个项目,人们撰写文章,并将其存储在数据库中。文章的信用信息存储为[USER ID]:Role
,然后通过使用用户 ID 从另一个表加载用户信息来加载到页面中,然后将角色实现到信用信息中。
例如:1:Writing
将生成 Brad -writing
但由于每页有多个学分,它们将存储在同一列中,并以逗号分隔。例如: 1:Writing,4:Images,8:Music
然后将显示为
Brad - Writing
Ben - Images
Jay - Music
为此,我使用分隔符 ,
分解列的内容,然后在数组上运行 foreach();
。
在此 foreach();
中,我需要查找用户的 ID,然后从表中加载信息,然后将角色也包含在信用中。
我想不出最好的方法来做到这一点。我是否需要运行 preg_replace
来运行单独的函数来获取信息,或者是否有其他方法可以做到这一点?或者是否有更好的方法将信息存储在数据库中?
预先感谢,布拉德。
I am currently working on a project where people write articles, which are stored in a database. The credit for the article is stored as [USER ID]:Role
and then loaded into the page by loading the user information from another table using the USER ID then implementing the role into the credit.
For example: 1:Writing
would produce Brad - Writing
But as there are multiple credits per page they will be stored in the same column separated by a comma. For example: 1:Writing,4:Images,8:Music
which would then appear as
Brad - Writing
Ben - Images
Jay - Music
To do this I am exploding the contents of the column with the separator ,
and then running a foreach();
on the array.
In this foreach();
I need to look up the ID's of the users then load the information from the table and then include the Role as well in the credit.
I can't think of the best way to do this. Would I need to run a preg_replace
to run a separate function to get the information or would there be another way to do it? Or would there even be a better way to store the information in the database?
Thanks in advance, Brad.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将它们存储在 MySQL 中的单独表中
,然后发出这样的查询
,或者您可以使用
preg_match
从当前字段中提取它来捕获所有user_id
并提供给它们到You can store them in MySQL in a separate table
and then issue a query like this
Or you can extract it from your current field using
preg_match
to catch alluser_id
s and feed them to完成您正在做的所有事情,但不要运行查询来从 foreach 循环中的表加载信息,而是可以使用:
然后执行查询
Do all the things you are doing but don't run query to load information from table in foreach loop instead you can use:
then execute query