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

Asked

Viewed 1,317 times

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));
 }
 ?>
  • 1

    Adds a if (!empty($linha)) { after your do { and so it only comes in if the variable $line has content

  • 1

    It is not good practice to use do { } while the way you used it. Even if there is only 1 single result, that second fetch will be executed.

  • 1

    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.

  • 2

    @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.

  • So this way I did it is the only way I "know" but adding if (!Empty($line)) it keeps displaying.

2 answers

3

You can do it right on query

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

If you save the blank field instead of NULL

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

Or by PHP using the function empty()

1

Use this code if you just don’t 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 entire product, use the @Jeferson tip

  • One thing I realized now is that your SQL has a ORDER BY RAND(), id DESC Choose only one of the two because one cancels the other.

  • I had forgotten to delete this from the code... .

  • I use a "NO PHOTO" image to display a product without the photo. SELECT * FROM techloja WHERE imgmarca IS NOT NULL AND imgmarca != "" ORDER BY id DESC LIMIT 12

Browser other questions tagged

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