Jump to content

[SOLVED] Small Code Help


Ell20

Recommended Posts

Hey,

 

Im currently trying to produce some code which displays the 3 latest news items followed by a link to Read More = Working fine.

However I also wanted to display a message saying if there are no news messages then echo "No news yet".

 

Here is my code:

 

<?php
$query = "SELECT * FROM news WHERE club_id = '$club_id' ORDER BY 'news_id' DESC LIMIT 3";
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
echo "<b>Title: </b>" ;
echo $row['title'];
?>
<hr>
<?php } ?>
<a href='news.php'>Read More</a>
<br>
<?php 
if ($row == 0) {
echo "No news yet";
}
?>

 

When I use this code the message "No news yet" is always there whether there is news or not.

 

Appreciate any help

 

Cheers

Link to comment
https://forums.phpfreaks.com/topic/74793-solved-small-code-help/
Share on other sites

<?php
$query = "SELECT * FROM news WHERE club_id = '$club_id' ORDER BY 'news_id' DESC LIMIT 3";
$result = mysql_query($query);
$rows = mysql_num_rows($result);
while ($row = mysql_fetch_assoc($result)) {
echo "<b>Title: </b>" ;
echo $row['title'];
?>
<hr>
<?php } ?>
<a href='news.php'>Read More</a>
<br>
<?php 
if ($rows < 1) {
echo "No news yet";
}
?>

 

Should fix it. Since $row was an array that was not a valid check.

 

www.php.net/mysql_num_rows

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.