Mod-Jay Posted February 19, 2011 Share Posted February 19, 2011 I am making a script that has a Notification/Inbox system in it. Right know it's close to being done, but I have one problem.. My problem is that if a user has >= 1 message in their inbox it shows what it should, but if the user has nothing in the inbox it shows nothing instead of the simple error "You have no message in your inbox". Code 1: $inbox_a = mysql_query("SELECT * FROM notifications WHERE recieverid='". $_SESSION['id'] ."'") or die(mysql_error()); Code 2: <?php if(!mysql_num_rows($inbox_a) <= 0) { ?> <tr class="table"> <td class="name"><a href="usercp.php?usercp=inbox&id=<?php echo $msgid; ?>"><h3><?php echo ucFirst($msgname); ?></h3></td> <td class="revision"><h3><a href="#"><?php echo $msgsendername ?></a></h3></td> <td class="votes"><h3><?php echo date('D, M d, Y h:i A', strtotime($msgdate)) ?></h3></td> </tr> <?php } else { ?> <tr class="table"> <td class="name">You have no messages in your inbox.</td> <td class="revision"></td> <td class="votes"></td> </tr> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/228173-problem-display-error/ Share on other sites More sharing options...
PFMaBiSmAd Posted February 19, 2011 Share Posted February 19, 2011 So, the code you posted is inside of a loop of some kind? Quote Link to comment https://forums.phpfreaks.com/topic/228173-problem-display-error/#findComment-1176642 Share on other sites More sharing options...
eutu9 Posted February 19, 2011 Share Posted February 19, 2011 Why so complicated? Instead of this: if(!mysql_num_rows($inbox_a) <= 0) { try if(mysql_num_rows($inbox_a) >0) { or simply: if(mysql_num_rows($inbox_a) ) { Btw, don't you need a loop for showing all messages? Quote Link to comment https://forums.phpfreaks.com/topic/228173-problem-display-error/#findComment-1176645 Share on other sites More sharing options...
Mod-Jay Posted February 19, 2011 Author Share Posted February 19, 2011 It worked eutu9, Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/228173-problem-display-error/#findComment-1176785 Share on other sites More sharing options...
Mod-Jay Posted February 20, 2011 Author Share Posted February 20, 2011 I was wrong, it didnt work. well it did but it needs to be in the loop otherwise it wont show every one Quote Link to comment https://forums.phpfreaks.com/topic/228173-problem-display-error/#findComment-1176985 Share on other sites More sharing options...
PFMaBiSmAd Posted February 20, 2011 Share Posted February 20, 2011 If the code you did post is inside of a loop, it won't work, no matter what logic you use, because the code inside the loop is not executed when there are zero rows in the result set. You must test before the start of the loop if there are rows or not and print your "You have no messages ..." if there are no rows and execute the loop if there are rows. Quote Link to comment https://forums.phpfreaks.com/topic/228173-problem-display-error/#findComment-1176989 Share on other sites More sharing options...
eutu9 Posted February 20, 2011 Share Posted February 20, 2011 Then use a loop <?php if(mysql_num_rows($inbox_a) ) { ?> <?php while($row = mysql_fetch_array($inbox_a)){ ?> <tr class="table"> <td class="name"><a href="usercp.php?usercp=inbox&id=<?php echo $row['msgid']; ?>"><h3><?php echo ucFirst($row['msgname']); ?></h3></td> <td class="revision"><h3><a href="#"><?php echo $row['msgsendername']; ?></a></h3></td> <td class="votes"><h3><?php echo date('D, M d, Y h:i A', strtotime($row['msgdate'])) ?></h3></td> </tr> <?php }} else { ?> <tr class="table"> <td class="name">You have no messages in your inbox.</td> <td class="revision"></td> <td class="votes"></td> </tr> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/228173-problem-display-error/#findComment-1177101 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.