简体   繁体   English

如何从PHP中的phpmyadmin一起回显所有相同id的行

[英]How to echo all the rows of same id together from phpmyadmin in PHP

My code: 我的代码:

  $stmt = $conn->prepare("SELECT tmdb_movies.movie_title, images.image_url
    FROM tmdb_movies
    JOIN images ON images.images_tmdb_id=tmdb_movies.tmdb_id
    GROUP BY tmdb_movies.movie_title,images.image_url");

 // Then fire it up
 $stmt->execute();
 // Pick up the result as an array
 $result = $stmt->fetchAll();

// Now you run through this array in many ways, for example
 for($x=0, $n=count($result); $x < $n; $x++){

     echo'
  '.$result[$x]["movie_title"].' - <img src="'.$result[$x]["image_url"].'"/>
  ';

}

Example: How this code ECHO data 示例:此代码如何使用ECHO数据

// SOME Stuff Here
The Dark Knight: - <img src="sdfsdfds.jpg"/>
// MORE STUFF HERE
// SOME Stuff Here
The Dark Knight: - <img src="zxfdy.jpg"/>
// MORE STUFF HERE

// SOME Stuff Here
Logan - <img src="rhfrg.jpg"/>
// MORE STUFF HERE
// SOME Stuff Here
Logan - <img src="sdfsqw.jpg"/>
// MORE STUFF HERE

How I Want It to echo data 我希望它如何回应数据

// Some Stuff Here
The Dark Knight -   <img src="sdfsdfds.jpg"/> <img src="zxfdy.jpg"/>
// More Stuff Here

 // SOME Stuff Here
Logan - <img src="rhfrg.jpg"/> <img src="sdfsqw.jpg"/>
// MORE STUFF HERE

I hope you understand. 我希望你明白。

Why it ECHO Data like this? 为什么ECHO这样的数据?

Because i am using one to many relationship table. 因为我正在使用一对多关系表。 My images table is like this: 我的图片表是这样的:

tmdb_id        images
1             sfdsfds.jpg
1             zcxz.jpg
2             fsfsdfs.jpg
3             dsfsdfs.jpg
3             dsfsfs.jpg
3            dfdsfdsw.jpg

Both tmdb_movies and images table are connected via tmdb_id tmdb_movies和images表都通过tmdb_id连接

Alternate way to display movies with one or more images. 使用一个或多个图像显示电影的替代方式。 This groups by title and uses GROUP_CONCAT to create a list of images for each title. 这按标题分组,并使用GROUP_CONCAT为每个标题创建图像列表。

$qstr = "";
$qstr .= "SELECT tmdb_movies.movie_title, GROUP_CONCAT(images.image_url) as `images`\n";
$qstr .= "FROM tmdb_movies\n";
$qstr .= "  JOIN images ON images.images_tmdb_id=tmdb_movies.tmdb_id\n";
$qstr .= "GROUP BY tmdb_movies.movie_title\n";
$qstr .= "LIMIT 10;";
$stmt = $conn->prepare($qstr);

// Then fire it up
$stmt->execute();
// Pick up the result as an array
$result = $stmt->fetchAll();

// Now you run through the results, one row per movie title.
foreach($result as $x => $row) {
    echo "\n".$row["movie_title"] . " - ";
    //  Images will be comma separated if multiple images.
    $img_list = explode(',',$row['images'];
    foreach($img_list as $c => $img) {
        echo " <img src='".$img."'/>";
    }
}

You can do a break sort, looking for duplicate movie titles, emitting the title only when it changes. 您可以进行中断排序,查找重复的电影标题,仅在更改时发出标题。

$qstr = "";
$qstr .= "SELECT tmdb_movies.movie_title, images.image_url\n";
$qstr .= "  FROM tmdb_movies\n";
$qstr .= "  JOIN images ON images.images_tmdb_id=tmdb_movies.tmdb_id\n";
$qstr .= "  GROUP BY tmdb_movies.movie_title,images.image_url";
$stmt = $conn->prepare($qstr);

// Then fire it up
$stmt->execute();
// Pick up the result as an array
$result = $stmt->fetchAll();

// Now you run through this array in many ways, for example
$lastTitle = "";
foreach($result as $x => $row) {
    if($lastTitle != $row["movie_title"]) {
        echo "\n".$row["movie_title"] . " - ";
        $lastTitle = $row["movie_title"];
    } else {
    }
    echo " <img src='".$row["image_url"]."'/>";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM