单个帖子的多个类别

时间:2015-05-25 08:49:11

标签: php mysql sql

我需要帮助,我有三张桌子

1. filmi | film_nr...
2. kategorite | kat_nr, emri_kat
3. film_kat | film_nr, kat_nr....

EX:

filmi 1
kategorite 3
           4
film_kat 1 3
         1 4

SELECT *, fk.kat_nr, f.film_nr AS fnr
FROM filmi f   
  INNER JOIN film_kat fk ON f.film_nr = fk.film_nr   
  INNER JOIN kategorite k ON fk.kat_nr = k.kat_nr   
ORDER by f.film_nr desc

我用它来获取行:

while($row = mysqli_fetch_assoc($rezultati)) {

当我从kat_nr获取结果以获取类别的名称时,对于每个查询,我得到一个新的div,当我使用GROUP By时,只显示一个类别名称,我想用逗号显示类别。

<?php 
    $sql = "
    SELECT *,fk.kat_nr,f.film_nr AS fnr FROM filmi f
    INNER JOIN film_kat fk ON f.film_nr = fk.film_nr
    INNER JOIN kategorite k ON fk.kat_nr = k.kat_nr
    ORDER by f.film_nr desc
    ";
    $rezultati = mysqli_query($conn, $sql);

    if (mysqli_num_rows($rezultati) > 0) {

    // output data of each row
    while($row = mysqli_fetch_assoc($rezultati)) {
   echo '<div class="filmi">';
    echo '<a href="film.php?nr='. $row["fnr"] .'" title="'. $row["emri_kat"] .'">'. $row["emri_film"] .'</a>';
    echo '<div class="fotografija">';
     echo '<img src="'. $row["imazhi_url"] .'" alt="">';
    echo '</div>';
    echo '<div class="pershkrimi">';
     echo '<p>Vlersimi: <strong>'. $row["imdb"] .'</strong>/10</p>';
    echo '</div>';
   echo '</div>';
    }
    }
   ?> 

类别显示在链接标题

1 个答案:

答案 0 :(得分:0)

在jarlh的帮助下,我做到了! 你唯一需要的是group concat

SELECT *,fk.kat_nr,f.film_nr AS fnr,group_concat(`emri_kat` separator ',') as `katname` FROM filmi f
INNER JOIN film_kat fk ON f.film_nr = fk.film_nr
INNER JOIN kategorite k ON fk.kat_nr = k.kat_nr
GROUP by f.film_nr