When you don’t have news

Asked

Viewed 55 times

1

Hi, I have a news system and wanted a message to appear when there was no 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 } ?>

1 answer

0

Do it this way:

<?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 stretch if(mysql_num_rows($news8)>0){we check if the SQL traveled returned any record, if it is greater than zero, executes the query, if not, we put the message that no record was found.

  • Did my answer work? If so, please accept it.

Browser other questions tagged

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