When you have no news message appears

1

Hello, I have a news system and I wanted a message to appear when I did not have any news in my database.

Code:

<?php
$news8 = mysql_query("SELECT * FROM 10cms_noticias WHERE status = 'Ativo' ORDER BY ID DESC LIMIT 8") or die(mysql_error());
?>

<?php $i = 0; while($noticias = mysql_fetch_assoc($news8)){ $i++; ?>
            <div class='noticia'>
                <div class='img' style='background-image: url(<?php echo $noticias['img']; ?>);'></div>
                <div class='titulo'><?php echo'<a href="./noticias/'.$noticias['id'].'">';?><b><?php echo $noticias['titulo']; ?></b></div>
                <div class='desc'><?php echo $noticias['resumo']; ?></a></div>
                <div class='info'>
                    <div class='comentarios' title="20 Comentários"></div>
                    <div class='data' title="<?php echo $noticias['data']; ?>"></div>
                    <div class='autor' title="<?php echo $noticias['autor']; ?>" style='background-image: url(https://www.habbo.com.br/habbo-imaging/avatarimage?img_format=gif&user=<?php echo $noticias['autor']; ?>&action=std&direction=2&head_direction=3&gesture=std&size=s);'></div>
                </div>
            </div>
        <?php } ?>
    
asked by anonymous 23.07.2015 / 23:54

1 answer

0

Do as follows:

<?php
$news8 = mysql_query("SELECT * FROM 10cms_noticias WHERE status = 'Ativo' ORDER BY ID DESC LIMIT 8") or die(mysql_error());
?>

<?php 
if(mysql_num_rows($news8)>0){ 
$i = 0; 
while($noticias = mysql_fetch_assoc($news8)){ $i++; ?>
<div class='noticia'>
    <div class='img' style='background-image: url(<?php echo $noticias['img']; ?>);'></div>
    <div class='titulo'><?php echo'<a href="./noticias/'.$noticias['id'].'">';?><b><?php echo $noticias['titulo']; ?></b></div>
    <div class='desc'><?php echo $noticias['resumo']; ?></a></div>
    <div class='info'>
        <div class='comentarios' title="20 Comentários"></div>
        <div class='data' title="<?php echo $noticias['data']; ?>"></div>
        <div class='autor' title="<?php echo $noticias['autor']; ?>" style='background-image: url(https://www.habbo.com.br/habbo-imaging/avatarimage?img_format=gif&user=<?php echo $noticias['autor']; ?>&action=std&direction=2&head_direction=3&gesture=std&size=s);'></div>
    </div>
</div>
<?php } } else { ?>
<div>
    Nenhum registro foi encontrado.
</div>
<? } ?>

In the% wrapper% wrapper we check whether the returned SQL returned a record, if it is greater than zero, execute the query, otherwise we will post the message that no records were found.

    
24.07.2015 / 00:18