PHP - If there is no content on the line do not display

0

I have a problem that I can not solve (I do not understand much of php) that I think is simple.

I am pulling a list of products from a database, however some products have no image and I would like it at the time of the Loop it does not load those that have some empty line.

For example: If you do not have anything on the line "imgmarca" it does not take this product on the screen.

Or rather, if you have nothing, it does not display the product.

Follow the 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));
 }
 ?>
    
asked by anonymous 19.10.2015 / 14:47

2 answers

3

You can do this right on query

SELECT * FROM techloja WHERE img IS NOT NULL ORDER  BY RAND(), id DESC LIMIT 12

If you save the field in blank instead of NULL

SELECT * FROM techloja WHERE img != "" ORDER  BY RAND(), id DESC LIMIT 12

Or by PHP using the function empty()

    
19.10.2015 / 14:56
1

Use this code if you just do not want to display the image when the field is empty.

<?php
$query = "SELECT * FROM techloja ORDER  BY RAND(), id DESC LIMIT 12";
$dados = mysql_query($query, $con) or die(mysql_error());
$total = mysql_num_rows($dados);
while($linha = mysql_fetch_assoc($dados) ) {?>          

<div class="produto_interna"  title="<?php echo $linha['nome']?>" >
        <a href="<?php echo $linha['link']?>">
        <div class="nome"><p><?php echo $linha['nomepeq']?> </p></div>
<?php
if (trim($linha['imgmarca']) != "") { ?>
        <div class="foto">
        <div  class="foto_img"><p><img src="<?php echo $linha['img']?>"  title="<?php echo $linha['nome']?>" alt="<?php echo $linha['nome']?>" /></p></div>

        <div class="loja"><p><img src="<?php echo $linha['imgmarca']?>" height="30px" /> <span style="float:right; font-size:1.5em; font-weight:bold; color:#BC0003; margin-top:5px;">R$ <?php echo $linha['valor']?></span></p></div>
        <div class="comprar"><img src="images/comprar_bt.jpg"/></div>
        </div>
<?php } ?>
        </a>
  </div>
 <?php    } // fim while 
 ?>

To avoid displaying the whole product, use the @Jeferson tip

    
19.10.2015 / 15:13