php - SQL display and fetch GROUP BY categories -
i have list of authors , display book genre, this:
horror stephen king author 2 author 3 ... comedy author 4 author 5 ...
in database have author table:
t_author
aut_name | aut_genre stephen king | 1 ... ... author 4 | 2
and genre table :
t_genre
genre_id | genre_name 1 | horror 2 | comedy ... ...
i've tried use group can't find how display names of category , organize list:
select t_author.aut_name, t_author.aut_genre t_author left join t_genre on t_author.aut_genre = t_genre.genre_id group t_genre.aut_genre
so, using mysql , pdo, is there way select , fetch authors , display categories?
no need i'm not looking code clue because don't see how describe it. mean, group right statement use?
edit 1 :
apparently, looking :
select t_author.aut_name, t_author.aut_slug, t_genre.genre_name t_author left join t_genre on t_author.genre_id = t_genre.genre_id
but displays:
horror stephen king horror author 2 comedy author 4 horror author 3
i understand why can't find how display below:
horror stephen king author 2 author 3 ... comedy author 4 author 5 ...
answer
htmhell provided me way 2d array. 3d array, edited code this:
foreach($result $author=> $slug) { $authorsbygenre[$slug['genre_name']][] = array($slug['aut_name'] => $slug['aut_slug']); } return $authorsbygenre;
how this:
$authorsbygenre = []; $results = $db->query(" select t_author.aut_name, t_genre.genre_name t_genre join t_author on t_author.aut_genre = t_genre.genre_id ")->fetchall(); foreach ($results $data) { $authorsbygenre[$data['genre_name']][] = $data['aut_name']; } print_r($authorsbygenre);
Comments
Post a Comment