I have created movie database. I have list of **Genres,** <ul> <li><a href="movielist.php?id=<?php echo"$id";?>"><?php echo"$genre";?></a></li> </ul> In **table movies**, movie have multiple geners. How to fetch same movie if multiple geners available (e.g. The Expendables 2 appear in **Action | Adventure | Thriller** these three geners). **How to display movie…
OK. This works for me with the mysql tables you posted:
SELECT
m.title,
GROUP_CONCAT(DISTINCT a.actor_name) AS allactors,
GROUP_CONCAT(DISTINCT d.director_name) AS alldirectors,
GROUP_CONCAT(DISTINCT g.genre) AS allgenres,
GROUP_CONCAT(DISTINCT w.writer_name) AS allwriters
FROM movies AS m
LEFT JOIN movie_actor AS ma ON m.movies_id = ma.movie_id
LEFT JOIN actors AS a ON ma.actor_id = a.id
LEFT JOIN movie_directors AS md ON m.movies_id = md.movie_id
LEFT JOIN directors AS d ON md.director_id = d.id
LEFT JOIN movie_genre AS mg ON m.movies_id = mg.movie_id
LEFT JOIN genres AS g ON mg.genre_id = g.g_id
LEFT JOIN movie_writers AS mw ON m.movies_id = mw.movie_id
LEFT JOIN writers AS w ON mw.writer_id = w.id
GROUP BY m.movies_id
You then have this content for the allactors field (Expendables 2):
'Sylvester Stallone,Jason Statham,Jean-Claude Van Damme,Jet Li,Dolph Lundgren,Chuck Norris,Bruce Willis,Arnold Schwarzenegger,Terry Crews,Nan Yu'
So in your php, you can do this:
$allactors = array();
$alldirectors = array();
$allgenres = array();
$allwriters = array();
$data = mysql_fetch_array($r);
if($data['allactors']) $allactors = explode(',' $data['allactors']);
if($data['alldirectors']) $directors = explode(',' $data['alldirectors']);
if($data['allgenres']) $genres = explode(',' $data['allgenres']);
if($data['allwriters']) $writers = explode(',' $data['allwriters']);
... other fields...
}
That'll give you arrays - so easier to format the output, although if you just need a comma delimited list, then you don't need to bother to explode.
diafol
Keep Smiling
10,627 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,506
Skill Endorsements: 57
I came across this: http://imdbapi.org/
An API for imdb. It's not an official imdb api and only for personal use, but you may find it useful for extracting data and inserting into your own db. However, I'm not sure about the legalities of such a move. It's really fast too - have a look at some of the examples.
I know that loads of site scrape data from IMDB. THey must get a bit miffed. :(
ALso (I think) official API: http://stackoverflow.com/questions/1966503/does-imdb-provide-an-api
diafol
Keep Smiling
10,627 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,506
Skill Endorsements: 57
Run my query in a phpmyadmin SQL window - see what it gives you.
I imported the DB from your text file, created my query and it returned what I expected. I don't know what more I can tell you.
diafol
Keep Smiling
10,627 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,506
Skill Endorsements: 57
I can't imagine any of thiose will display as you need
echo $row['title'];
etc, unless you've used extract() ?
I don't udnerstand: echo"$actor_id<br />";
actor_id isn't in the SQL so how can you retrieve it?
diafol
Keep Smiling
10,627 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,506
Skill Endorsements: 57
GROUP_CONCAT(DISTINCT CONCAT(a.actor_name, '|', ma.character_name)),
Will give this output:
Sylvester Stallone|Barney Ross,Jason Statham|Lee Christmas,Jean-Claude Van Damme|Vilain,Jet Li|Yin Yang,Dolph Lundgren|Gunnar Jensen,Chuck Norris|Booker,Bruce Willis|Church,Arnold Schwarzenegger|Trench,Terry Crews|Hale Caesar,Nan Yu|Maggie
So you can explode on ',' to get the actor-character pairs (into an array) and then you can explode on '|' in order to format the data to your liking.
diafol
Keep Smiling
10,627 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,506
Skill Endorsements: 57
... GROUP_CONCAT(DISTINCT CONCAT(a.actor_name, '|', ma.character_name)) AS actchar...
Example (assuming mysql) and single film
$data = mysql_fetch_assoc($result);
$act = array();
$actchars = explode(",", $data['actchar']);
foreach($actchars as $ac) $act[] = explode("|", $ac);
...when you need it... OR you can do it with the above
$table = '<table><thead><tr><th>Actor</th><th>Character</th></tr></thead><tbody>';
foreach($act as $a) $table .= '<tr><td>' . $a[0] . '</td><td>' . $a[1] . '</td></tr>';
$table .= '</tbody></table>';
..then echo out in the right place:
echo $table;
NOT TESTED and assuming output from DB like the following:
Sylvester Stallone|Barney Ross,Jason Statham|Lee Christmas,Jean-Claude Van Damme|Vilain,Jet Li|Yin Yang,Dolph Lundgren|Gunnar Jensen,Chuck Norris|Booker,Bruce Willis|Church,Arnold Schwarzenegger|Trench,Terry Crews|Hale Caesar,Nan Yu|Maggie
diafol
Keep Smiling
10,627 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,506
Skill Endorsements: 57
If genre is set / not set, then do this:
if($gen_id){
$where_clause = " WHERE genre_id = $gen_id ";
}else{
$where = '';
}
$query="SELECT m.*,
GROUP_CONCAT(DISTINCT CONCAT(a.actor_name, ' | ', ma.character_name)) AS allactors,
GROUP_CONCAT(DISTINCT d.director_name) AS alldirectors,
GROUP_CONCAT(DISTINCT g.genre) AS allgenres,
GROUP_CONCAT(DISTINCT w.writer_name) AS allwriters
FROM movies AS m
LEFT JOIN movie_actor AS ma ON m.movies_id = ma.movie_id
LEFT JOIN actors AS a ON ma.actor_id = a.id
LEFT JOIN movie_directors AS md ON m.movies_id = md.movie_id
LEFT JOIN directors AS d ON md.director_id = d.id
LEFT JOIN movie_genre AS mg ON m.movies_id = mg.movie_id
LEFT JOIN genres AS g ON mg.genre_id = g.g_id
LEFT JOIN movie_writers AS mw ON m.movies_id = mw.movie_id
LEFT JOIN writers AS w ON mw.writer_id = w.id
$where
GROUP BY m.movies_id";
You can use this idea on all bits of the query:
"SELECT m.*,
$allactors
$alldirectors
$allgenres
$allwriters
FROM movies AS m
$actorjoin
$directorjoin
$genrejoin
$writerjoin
$where
GROUP BY m.movies_id";
So you don't have to build a unique query for every search combination. You'd put this into a function with various parameters:
function getData($criteria){
...
}
diafol
Keep Smiling
10,627 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,506
Skill Endorsements: 57
use foreach:
$allactors = explode(',', $data['allactors']);
foreach($allactors as $actor){
echo "Actors:- $actor, ";
}
Why the comma?
diafol
Keep Smiling
10,627 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,506
Skill Endorsements: 57