PHP:如何制作电影和流派列表(示例)- MySQL 加入多对多(想要?)和 PHP?

发布于 2024-09-18 06:07:04 字数 1166 浏览 8 评论 0 原文

如何制作电影和类型列表 - MySQL 加入多对多(我应该吗?)和 PHP? 或者采取其他方式?

示例:
在 MySQL 中:

Table 1:
movie_id | movie_title
1______  | title_movie_1
2______  | title_movie_2
3______  | title_movie_3

Table 2:
genre_id | genre_title
1______  | title_genre_1
2______  | title_genre_2
3______  | title_genre_3

Table 3:
id | movie_id | genre_1
1_ | __1____ | _1
2_ | __1____ | _2
3_ | __2____ | _1
4_ | __2____ | _3
5_ | __3____ | _1
6_ | __3____ | _3
7_ | __3____ | _2

结果 PHP:

Movie: <a href="movie/1 (id_movie)">title_movie_1 _/a>
Genre: 
<a href="genre/1 (id_genre)">title_genre_1 _/a>, 
<a href="genre/2">title_genre_2 _/a>

Movie: <a href="movie/2 (id_movie)">title_movie_2 _/a>
Genre: 
<a href="genre/1">title_genre_1 _/a>,
<a href="genre/3">title_genre_3 _/a>

Movie: <a href="movie/3 (id_movie)">title_movie_3 _/a>
Genre: 
<a href="genre/1">title_genre_1 _/a>
<a href="genre/2">title_genre_2 _/a>
<a href="genre/3">title_genre_3 _/a>

我找不到做... 请帮忙!我在等!

此致, 阿雷库

How to make a list of film and genre - MySQL Join Many-to-Many (should I?) And the PHP?
Or do the other way?

Examples:
In MySQL:

Table 1:
movie_id | movie_title
1______  | title_movie_1
2______  | title_movie_2
3______  | title_movie_3

Table 2:
genre_id | genre_title
1______  | title_genre_1
2______  | title_genre_2
3______  | title_genre_3

Table 3:
id | movie_id | genre_1
1_ | __1____ | _1
2_ | __1____ | _2
3_ | __2____ | _1
4_ | __2____ | _3
5_ | __3____ | _1
6_ | __3____ | _3
7_ | __3____ | _2

Results PHP:

Movie: <a href="movie/1 (id_movie)">title_movie_1 _/a>
Genre: 
<a href="genre/1 (id_genre)">title_genre_1 _/a>, 
<a href="genre/2">title_genre_2 _/a>

Movie: <a href="movie/2 (id_movie)">title_movie_2 _/a>
Genre: 
<a href="genre/1">title_genre_1 _/a>,
<a href="genre/3">title_genre_3 _/a>

Movie: <a href="movie/3 (id_movie)">title_movie_3 _/a>
Genre: 
<a href="genre/1">title_genre_1 _/a>
<a href="genre/2">title_genre_2 _/a>
<a href="genre/3">title_genre_3 _/a>

I can not find do ...
Help, please! I'm waiting!

Sincerely, Areku

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

安穩 2024-09-25 06:07:04
SELECT
    T1.movie_id,
    T1.movie_title,
    T2.genre_id,
    T2.genre_title
FROM
    Table3 AS T3
    INNER JOIN Table1 AS T1 ON T3.movie_id = T1.movie_id
    INNER JOIN Table2 AS T2 ON T3.genre_1 = T2.genre_id
ORDER BY
    T1.movie_id

这将返回类似这样的结果:

movie_id | movie_title   | genre_id | genre_title
1        | title_movie_1 | 1        | title_genre_1
1        | title_movie_1 | 2        | title_genre_2
2        | title_movie_2 | 1        | title_genre_1
2        | title_movie_2 | 3        | title_genre_3
3        | title_movie_3 | 1        | title_genre_1
3        | title_movie_3 | 3        | title_genre_3
3        | title_movie_3 | 2        | title_genre_2

之后,您可以循环遍历所有记录;如果 movie_id 更改,则打印 movie_title,然后打印类型标题。

$currentMovie = 0;
while ($row = mysql_fetch_row($result))
{
    if($row['movie_id'] != $current_movie)
    {
        $currentMovie = $row['movie_id'];
        echo "Movie: <a href=\"movie/" + $row['movie_id'] + "\">" + $row['movie_title'] + "</a><br />Genre:<br />"
    }
    echo "<a href=\"genre/" + $row['genre_id'] + "\">" + $row['genre_id'] + "</a><br />"
}
SELECT
    T1.movie_id,
    T1.movie_title,
    T2.genre_id,
    T2.genre_title
FROM
    Table3 AS T3
    INNER JOIN Table1 AS T1 ON T3.movie_id = T1.movie_id
    INNER JOIN Table2 AS T2 ON T3.genre_1 = T2.genre_id
ORDER BY
    T1.movie_id

That would return something like this:

movie_id | movie_title   | genre_id | genre_title
1        | title_movie_1 | 1        | title_genre_1
1        | title_movie_1 | 2        | title_genre_2
2        | title_movie_2 | 1        | title_genre_1
2        | title_movie_2 | 3        | title_genre_3
3        | title_movie_3 | 1        | title_genre_1
3        | title_movie_3 | 3        | title_genre_3
3        | title_movie_3 | 2        | title_genre_2

Afterwards, you can just loop through all the records; print movie_title if movie_id changed, then print genre_title.

$currentMovie = 0;
while ($row = mysql_fetch_row($result))
{
    if($row['movie_id'] != $current_movie)
    {
        $currentMovie = $row['movie_id'];
        echo "Movie: <a href=\"movie/" + $row['movie_id'] + "\">" + $row['movie_title'] + "</a><br />Genre:<br />"
    }
    echo "<a href=\"genre/" + $row['genre_id'] + "\">" + $row['genre_id'] + "</a><br />"
}
千秋岁 2024-09-25 06:07:04

执行多对多 sql 查询:

SELECT m.movie_id, m.movie_title, g.genre_id, g.genre_title
FROM movies m 
INNER JOIN movie_genres mg ON m.movie_id = mg.movie_id
INNER JOIN genres g ON g.genre_id = mg.genre_id

然后循环遍历结果并按电影 id 进行分组。

Do a many-to-many sql query:

SELECT m.movie_id, m.movie_title, g.genre_id, g.genre_title
FROM movies m 
INNER JOIN movie_genres mg ON m.movie_id = mg.movie_id
INNER JOIN genres g ON g.genre_id = mg.genre_id

Then loop through the result and group by movie id.

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