Print all images from another table without repeating the title

0

I have both tables below

IMAGES table

id_imagens  | diretorio_imagem | post_id
       1        imagem1.jpg          1
       2        imagem2.jpg          1
       3        imagem3.jpg          1
       4        imagem4.jpg          1
       5        imagem5.jpg          2
       6        imagem6.jpg          2
       7        imagem7.jpg          2
       8        imagem8.jpg          2

POSTS Table

id_post   |          slug             |   titulo     
 1              titulo_do_post           Titulo do Post 
 2              titulo_do_post_2         Titulo do Post 2

PHP:

$slug = $_GET['slug'];
$stmt = $db->prepare("SELECT * FROM posts, imagens
WHERE posts.id_post = imagens.post_id
AND slug=:slug");
$stmt->execute(array(":slug"=>$_GET['slug']));
     while($row=$stmt->fetch(PDO::FETCH_BOTH))
{
?> 

<h3>Você esta aqui:<?php print utf8_encode($row['titulo']);?></h3>

<!-- Start Anuncio-->
<div> 
   <img src="http://entreconnection.com/wp-content/uploads/2012/09/Wide-Skyscrapper3.png"style="margin-top:50px;">
</div>
 <!--End Anuncio-->

<h1><?php print utf8_encode($row['titulo']);?></h1>

<img src="<?php print ($row['diretorio_imagem']); ?>">

 <div> 
        <!-- Start Anuncio-->
        <div style="text-align:center">
        <img src="https://www.google.com/help/hc/images/adsense/mobile-leaderboard.png"style="margin:20px;">
        </div>
        <!-- END Anuncio -->

        <div class="fb-comments" data-href="<?php echo BASE_URL ?>/<?php print ($row['slug']); ?>" data-width="600" data-numposts="5"></div>

 </div>


<?php
}
?>

Accessing the "Title 2 post" page prints:

Titulo do post 2
imagem5.jpg

Titulo do post 2
imagem6.jpg

Titulo do post 2
imagem7.jpg

Titulo do post 2
imagem8.jpg

How to have the result:

Titulo do post 2
imagem5.jpg
imagem6.jpg
imagem7.jpg
imagem8.jpg

I have already understood that I can have an if check or make a group by in the sql statement, but it's not coming into my head how to check if the title already exists before printing ... or if you use group by as make the foreach of images within the while

Thank you for your help

    
asked by anonymous 23.03.2016 / 00:48

1 answer

0

One simple way to solve this is:

  • First let's organize the data by the title of the post followed by the id of the image:

    SELECT * FROM posts, imagens WHERE posts.id_post = imagens.post_id AND slug=:slug ORDER BY posts.titulo, imagens.id_imagens

  • Then follow the example below, making an if within the while to see if the title has changed, and print if so:

    $slug = $_GET['slug'];
    $stmt = $db->prepare("SELECT * FROM posts, imagens WHERE posts.id_post = imagens.post_id AND slug=:slug ORDER BY posts.titulo, imagens.id_imagens");
    $stmt->execute(array(":slug"=>$_GET['slug']));
    
    $titulo = '';
    $conteudo = '';
    while($row=$stmt->fetch(PDO::FETCH_BOTH)) {
    
        if($row['titulo'] != $titulo) {
    
            $conteudo .= ($conteudo != '') ?
            '<div>
                <!-- Start Anuncio-->
                <div style="text-align:center">
                    <img src="https://www.google.com/help/hc/images/adsense/mobile-leaderboard.png"style="margin:20px;">
                </div>
                <!-- END Anuncio -->
                <div class="fb-comments" data-href="'.BASE_URL.'/'.$row['slug'].'" data-width="600" data-numposts="5"></div>
            </div>' : '';
    
            echo $conteudo;
    
            $conteudo = '<h3>Você esta aqui: '.utf8_encode($row['titulo']).'</h3>
            <!-- Start Anuncio-->
            <div> 
              <img src="http://entreconnection.com/wp-content/uploads/2012/09/Wide-Skyscrapper3.png"style="margin-top:50px;">
            </div>
            <!--End Anuncio-->
            <h1>'.utf8_encode($row['titulo'])'</h1>';
    
            $titulo = $row['titulo'];
        }
    
        $conteudo .= '<img src="'.$row['diretorio_imagem'].'"> ';
    }
    

I hope it helps.

    
23.03.2016 / 04:01