0
I’m having a problem that I can’t solve (I don’t know much about php) that I think is simple.
I am pulling a list of products from a database, but some products have no image and I would like that at the time of Loop it does not load those that have some empty line.
For example: If you don’t have anything in the line "imgmarca" it does not ecibe this product on the screen.
Or better if in have nothing does not display the product.
Follows code.
<?php
$query = sprintf("SELECT * FROM techloja ORDER BY RAND(), id DESC LIMIT 12");
$dados = mysql_query($query, $con) or die(mysql_error());
$linha = mysql_fetch_assoc($dados);
$total = mysql_num_rows($dados);
if($total > 0) {
do {
?>
<div class="produto_interna" title="<?=$linha['nome']?>" >
<a href="<?=$linha['link']?>">
<div class="nome"><p><?=$linha['nomepeq']?> </p></div>
<div class="foto">
<div class="foto_img"><p><img src="<?=$linha['img']?>" title="<?=$linha['nome']?>" alt="<?=$linha['nome']?>" /></p></div>
<div class="loja"><p><img src="<?=$linha['imgmarca']?>" height="30px" /> <span style="float:right; font-size:1.5em; font-weight:bold; color:#BC0003; margin-top:5px;">R$ <?=$linha['valor']?></span></p></div>
<div class="comprar"><img src="images/comprar_bt.jpg"/></div>
</div>
</a>
</div>
<?php
}while($linha = mysql_fetch_assoc($dados));
}
?>
Adds a
if (!empty($linha)) {
after yourdo {
and so it only comes in if the variable $line has content– MoshMage
It is not good practice to use
do { } while
the way you used it. Even if there is only 1 single result, that secondfetch
will be executed.– Marcos Regis
Swap the one from{}while for just the while. So you don’t have to do this out-of-loop check, like you’re doing on the third line, and improve the readability of the code.
– user28595
@Marcosregis was cool to teach how he should have done it too ;) -- along with what Marcos said, it was preferable for you to wear a
forEach($linhas as $linha)
but then you’ll have to change the structure of the code.– MoshMage
So this way I did it is the only way I "know" but adding if (!Empty($line)) it keeps displaying.
– Rodrigo B. Silva