Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:如何制作电影和流派列表(示例)-MySQL加入多对多(想要?)和PHP?_Php_Mysql_Many To Many_Movie - Fatal编程技术网

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

PHP:如何制作电影和流派列表(示例)-MySQL加入多对多(想要?)和PHP?,php,mysql,many-to-many,movie,Php,Mysql,Many To Many,Movie,如何制作电影和流派列表-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___

如何制作电影和流派列表-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:title\u Movie\u 1\u/a>
体裁:
标题(类型)(1)(a),
标题\u体裁\u 2\u/a>
电影名称:电影名称\u电影\u 2\u/a>
体裁:
标题(类型)(1)(a),
标题\u类型\u 3\u/a>
电影名称:电影名称\u电影\u 3\u/a>
体裁:
标题\u体裁\u 1\u/a>
标题\u体裁\u 2\u/a>
标题\u类型\u 3\u/a>
我找不到你要做的事。。。 救命啊!我在等你


诚恳地说,Areku执行多对多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分组

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
之后,您可以循环浏览所有记录;如果电影id已更改,则打印电影标题,然后打印流派标题

$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 />"
}
$currentMovie=0;
while($row=mysql\u fetch\u row($result))
{
如果($row['movie\u id'!=$current\u movie)
{
$currentMovie=$row['movie_id'];
echo“电影:
类型:
” } 回声“
” }
$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 />"
}