1
Analyzing the MODEL, would like some simple suggestion to classify these blocks by categories dynamically with the same existing effects.
I have a table that saves the "product" id and the respective category ids related to this product.
categories_products
catp_id int(11) No
catp_pro_id int(11) Yes NULL
catp_cat_id varchar(255) Yes NULLTABLE DATA:
PHP prototype (requires solution):
Remembering that category ids are processed into one array
and separated by comma in the database using the function explode
.
<div class="section-content portfolio-section grid">
<div class="container">
<ul class="filter">
<li><a href="#" class="active" data-filter="*">Todas categorias</a></li>
<?php $selCategoria = $conn->prepare("SELECT * FROM cat ORDER BY cat_nome ASC");
$selCategoria->execute();
if($selCategoria->rowCount() > 0):
while($rowCategoria = $selCategoria->fetch(PDO::FETCH_OBJ)): ?>
<li><a href="#" data-filter=".<?php echo $rowCategoria->cat_id; ?>"><?php echo $rowCategoria->cat_nome; ?></a></li>
<?php endwhile; endif; ?>
</ul>
<div class="portfolio-box masonry two-col">
<?php $selProdutos = $conn->prepare("SELECT p.pro_id, p.pro_nome, p.pro_status, p.pro_valor, p.pro_destaque, p.pro_brevedescricao, fp.posicao, fp.arquivo, cp.catp_cat_id
FROM produtos p
LEFT JOIN fotos_produtos fp ON p.pro_id = fp.id_produto AND fp.posicao = ?
RIGHT JOIN produtos ON p.pro_status = ?
LEFT JOIN categorias_produtos cp ON cp.catp_pro_id = p.pro_id
GROUP BY p.pro_id
ORDER BY rand() ");
$selProdutos->execute(array(0,1));
while($rowProduto = $selProdutos->fetch(PDO::FETCH_OBJ)): ?>
<div class="project-post <?php echo $rowProduto->catp_cat_id; ?>">
<div class="project-gal">
<img alt="" src="<?php echo URL; ?>administracao/imagens/produtos/tamanho1_<?php echo $rowProduto->arquivo; ?>">
</div>
<h2><?php echo $rowProduto->pro_nome; ?></h2>
<p><?php echo $rowProduto->pro_brevedescricao; ?></p>
</div>
<?php endwhile; ?>
</div>
</div>
</div>
HTML structure:
Ignore the origin of the classes in this structure. You can see that the category name is processed within the tag data-filter
, in PHP I built something similar with the comma separated category id in data-filter
.
<div class="section-content portfolio-section grid">
<div class="container">
<ul class="filter">
<li><a href="#" class="active" data-filter="*">Todas categorias</a></li>
<li><a href="#" data-filter=".design">Categoria 1</a></li>
<li><a href="#" data-filter=".animation">Categoria 2</a></li>
<li><a href="#" data-filter=".photography">Categoria 3</a></li>
</ul>
<div class="portfolio-box masonry two-col">
<div class="project-post design">
<div class="project-gal">
<img alt="" src="http://placehold.it/460x300">
</div>
<h2>Cras ornare tristique</h2>
<p>Vivamus vestibulum nulla nec ante.</p>
</div>
<div class="project-post animation photography">
<div class="project-gal">
<img alt="" src="http://placehold.it/460x300">
</div>
<h2>Cras ornare tristique</h2>
<p>Vivamus vestibulum nulla nec ante.</p>
</div>
<div class="project-post design">
<div class="project-gal">
<img alt="" src="http://placehold.it/460x300">
</div>
<h2>Cras ornare tristique</h2>
<p>Vivamus vestibulum nulla nec ante.</p>
</div>
<div class="project-post photography">
<div class="project-gal">
<img alt="" src="http://placehold.it/460x300">
</div>
<h2>Cras ornare tristique</h2>
<p>Vivamus vestibulum nulla nec ante.</p>
</div>
<div class="project-post design">
<div class="project-gal">
<img alt="" src="http://placehold.it/460x300">
</div>
<h2>Cras ornare tristique</h2>
<p>Vivamus vestibulum nulla nec ante.</p>
</div>
<div class="project-post animation">
<div class="project-gal">
<img alt="" src="http://placehold.it/460x300">
</div>
<h2>Cras ornare tristique</h2>
<p>Vivamus vestibulum nulla nec ante.</p>
</div>
</div>
</div>
</div>
with numbers and commas in the class will not work, create a keyword for each category and separate them with space and not comma... by the way this your table structure this strange way, to concatenate the category Ids thus, would not need to be in a separate table, could be a field in the same products table...
– Jader A. Wagner
At first it is so because its use was differentiated earlier. In the case some time ago I developed something similar however, unsuccessfully, where I used a function
in_array
, to check whether there is a certain number in thedata-filter
. I thought of something like this, developed the logic, but without success in practice. At first I think there must be some way using the ids, otherwise I will rediscover this part.– Tiago Boeing