Associate images to category

Asked

Viewed 55 times

0

I am developing a slideshow based on categories. That is, the categories are pulled from the database and the images are associated with them. However, I am having 2 problems. The first is the miniatures of the categories. One or more thumbnails of categories appear repeated (2 or 3 times), while other categories are not displayed.

The code is:

echo '<h2 class="margin-bototm10" style="text-align:center;">Outras Categorias</h2>';
                $result = $mysql->sql_query("select * from TB_CATEGORIA inner join TB_GALERIA on TB_CATEGORIA.PK_CATEGORIA = TB_GALERIA.CATEGORIA where TB_CATEGORIA.DELETED IS NULL AND TB_CATEGORIA.CODUSUARIO=1 AND TIPO='1'");

                while ($row = mysql_fetch_assoc($result)) {
                    echo "<li class='isotope-item col-sm-6 col-md-3' style='list-style-type:none;'>";
                    echo "<div class='item-box'>";
                    echo "<figure>";
                    echo "<a title='".$row['DESCRICAO']."' class='item-hover' href='galerias.php?categoria=".$row['PK_CATEGORIA']."'>";
                    echo " <span class='overlay color2'></span>";
                    echo " <span class='inner'>";
                    echo " <span class='block fa fsize30 fa-picture-o'></span>";
                    echo "<strong>Ver</strong> Galeria                        </span>";
                    echo "</a>";
                    echo "<img class='img-responsive' src='../images/".$row['IMAGEM']."' width='260' height='260' style='max-height: 170px;' />";
                    echo "</figure>";
                    echo "<div class='item-box-desc'>";
                    echo " <small class='item-box-date'> </small>";
                    echo "<small style='display: inline;'>".$row['NOME']."</small>";
                    echo " <div class='row' style='max-width: 250px;'>";
                    echo " <div class='col-md-12'>";
                    echo "   <p class='similar-h4' style='display: inline;'>".$row['NOME']."</p>";
                    echo " </div>";
                    echo "</div>";
                    echo " </div>";
                    echo "</div>";
                    echo "</li>";

The other is the following, in the code is to display only images/categories where the table value DELETED is NULL, I changed this to test, but the original idea is that it is displayed when the table is empty and not null. Because for security reasons, the deleted one only gets the * tag in the DELETED table and only after a few days is it really deleted. I tried with DELETED=', but it didn’t work at all.

What am I doing wrong?

1 answer

1

First thing gives an order by in your query:

select * from TB_CATEGORIA 
inner join TB_GALERIA on TB_CATEGORIA.PK_CATEGORIA = TB_GALERIA.CATEGORIA 
where 
   (TB_CATEGORIA.DELETED IS NULL OR TRIM(TB_CATEGORIA.DELETED) <> '')  
   AND TB_CATEGORIA.CODUSUARIO=1 
   AND TIPO='1'
Order by
    TB_CATEGORIA.PK_CATEGORIA

What is happening is that in your query the results will come like this:

  • Category Figure

    1        x
    1        y
    3        qq
    4        xx
    

You will need to make a control when printing the category:

if($row['PK_CATEGORIA'] != $categoria){
  //imprime thumb da categria
  $categoria = $row['PK_CATEGORIA'];
}
  • I don’t fully understand the print control part. How exactly should this work? I should put that if after the while or it would be the part?

  • and what to put in Energy, for example?

  • I’m confused because on some pages the categories Texts appear repeated like that (taking only the first category and duplicating) but on other pages no category appears.

  • You put that if in the part where you want to print Thumb. You don’t need Echo, because it prints or doesn’t print Thumbs

  • Now the categories are being displayed, but there’s a problem, they’re still repeating themselves. For example, if there are 6 images in the category, it is repeated 6 times, if there are 2 images, it repeats 2 times and so on.

  • And are only being displayed the categories where the DELETED table is as Null, not empty

  • placed (TB_CATEGORIA.DELETED IS NULL OR Trim(TB_CATEGORIA.DELETED) <> '')

  • Now it worked! Now all that is left is the problem of repeating the category. Instead of just displaying a Thumb, it displays the repeated category with each of the images contained in it. For example, the first with image 1, the second with image 2 and the third with image 3, of the same category. Instead of just taking one of the images and using as Thumb and displaying the category once.

  • The idea of the solution lies in the answer I gave you...

  • I tried here, but it didn’t roll.

  • Face to logic is a way. Now it’s a matter of understanding, and making the necessary adaptations

Show 6 more comments

Browser other questions tagged

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