How to echo all strings of one id along with 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: like this code ECHO data p>
// 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 the 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 is this ECHO data like this?
Because I am using one or two relationship tables. My images table looks 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 are linked via tmdb_id
+3
source to share
2 answers
An alternative 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.
$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."'/>";
}
}
+1
source to share
You can do sorting, look for duplicate movie titles, emit a title only when they change.
$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"]."'/>";
}
0
source to share