在 PHP 中拆分并运行代码

发布于 2024-10-10 08:44:04 字数 615 浏览 0 评论 0原文

我目前正在开展一个项目,人们撰写文章,并将其存储在数据库中。文章的信用信息存储为[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

友欢 2024-10-17 08:44:04

您可以将它们存储在 MySQL 中的单独表中

article_id   user_id   role
---------------------------
Brad       | 1       | Writing

,然后发出这样的查询

SELECT u.name, ac.role FROM article_credits ac 
    LEFT JOIN users u ON ac.user_id = u.id
    WHERE ac.article_id = ?

,或者您可以使用 preg_match 从当前字段中提取它来捕获所有 user_id 并提供给它们到

SELECT * FROM users WHERE users.id IN (' . implode(',', $user_ids).')'

You can store them in MySQL in a separate table

article_id   user_id   role
---------------------------
Brad       | 1       | Writing

and then issue a query like this

SELECT u.name, ac.role FROM article_credits ac 
    LEFT JOIN users u ON ac.user_id = u.id
    WHERE ac.article_id = ?

Or you can extract it from your current field using preg_match to catch all user_ids and feed them to

SELECT * FROM users WHERE users.id IN (' . implode(',', $user_ids).')'
不念旧人 2024-10-17 08:44:04

完成您正在做的所有事情,但不要运行查询来从 foreach 循环中的表加载信息,而是可以使用:

 $query = "Select * from table where id in ('" . implode("','", $columns) . "')";

然后执行查询

Do all the things you are doing but don't run query to load information from table in foreach loop instead you can use:

 $query = "Select * from table where id in ('" . implode("','", $columns) . "')";

then execute query

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文