Print all images from another table without repeating the title

Asked

Viewed 60 times

0

I have the two tables below

table IMAGES

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

Table POSTS

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
}
?>

By going to the "Post title 2" page:

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 already understood that you can have an if check or make a group by in sql statement, but it is not entering in my head how to make the check if the title already exists before printing... or if to use group by how to foreach the images inside while

I appreciate help

1 answer

0


A simple way to solve this is:

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

    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, doing an if inside 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.

  • thanks resolves to print the title only once, but in the other contents (advertisements and html tags) inside while continue to be duplicated according to the amount of images, it is impossible to create several ifs

  • The solution you wanted is solved, I’m not understanding what you want now, advertisements? Explain better what you want.

  • I marked it as settled, because it really settles the question thank you very much. As I commented there are other html contents inside while and in some parts there are php tags in the case of the title and images, with its solution the title prints a single time but the rest of the html content is replicated according to the amount of images

  • What else? Post what is coming out and what is waiting to come out to try to find a solution.

  • In the question I entered some information inside the while for you to have an idea

  • I edited the answer, see if it works

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.