Submit Your Article Webforumz RegistrationAnnouncements Contact Webforumz StaffContact
Home Resources Blogs Meet the Team Contact Register
 

Go Back   WebForumz.com > The Code > PHP

Reply
 
LinkBack Thread Tools
Old January 18th, 2008, 03:36 PM   #1
Most Reputable Member
 

Join Date: May 2007
Location: Cornwall, England
Posts: 1,421
Blog Entries: 8
Thanks: 18
Thanked 14 Times in 14 Posts
Rep Altering Power: 0 Jack Franklin will become famous soon enough
Help with an IF Else statement & MySQL

I know this is an obvious question, but I cannot find an answer!

At the moment the page simpyl displays nothing if there are no comments in the database. I would like it to display something like 'There are no comments yet.'. This is the code for displaying the comments:
Code:
$display_comments = @mysql_query("SELECT commentsunique, commentsid, name, comment FROM comments WHERE commentsid='$contentid' ORDER BY commentsunique DESC");
while (
$comments mysql_fetch_array($display_comments)) {
$commentid $comments['commentsid'];
$name $comments['name'];
$comment $comments['comment'];
echo 
'<h5 class="commentsname">' $name '</h5>';
echo 
'<p class="commentstext">' $comment '</p>'
It would be something like:
Code:
IF (there are no comments) {
echo 
'<p>No comments</p>';
} else {
display comments...

Thank you!
__________________
Yours is the Earth and everything that's in it
And - which is more - you'll be a Man my son!
Jack Franklin is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old January 18th, 2008, 03:43 PM   #2
WebForumz Admin Badge
SuperMember
 
Marc's Avatar
 

Join Date: Apr 2007
Location: Scotland, UK
Posts: 2,086
Thanks: 2
Thanked 23 Times in 23 Posts
Rep Altering Power: 89 Marc is just really nice Marc is just really nice Marc is just really nice Marc is just really nice
Re: Help with an IF Else statement & MySQL

Why not count the rows on your comments query??

so your IF would look like:
Code:
if($rows_count >= 1) {
//show the comments
} else {
// no comments to show!
print "Sorry, currently there are no comments to show";

__________________
Regards
Marc
Administrator - Webforumz.com

Web Design and Development Blog - Marc Fraser
Marc is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old January 18th, 2008, 03:45 PM   #3
Highly Reputable Member
SuperMember
 
Rakuli's Avatar
 

Join Date: Sep 2007
Location: Australia
Age: 25
Posts: 954
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0 Rakuli is a splendid one to behold Rakuli is a splendid one to behold Rakuli is a splendid one to behold Rakuli is a splendid one to behold Rakuli is a splendid one to behold Rakuli is a splendid one to behold
Re: Help with an IF Else statement & MySQL

Check how many rows were retrieved link this
Code:
if (!mysql_num_rows($display_comments))
         echo 
'Oh no! There\'s no comments. This is so sad for me :( ';
else {
 
// do other stuff here

__________________
Luke Dingle . com
Rakuli is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old January 18th, 2008, 03:46 PM   #4
WebForumz Member
 

Join Date: Jan 2008
Location: London
Age: 19
Posts: 64
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0 Bravo81 is on a distinguished road
Re: Help with an IF Else statement & MySQL

Code:
$display_comments = @mysql_query("SELECT commentsunique, commentsid, name, comment FROM comments WHERE commentsid='$contentid' ORDER BY commentsunique DESC");
while (
$comments mysql_fetch_array($display_comments)) {
$commentid $comments['commentsid'];
$name $comments['name'];
$comment $comments['comment'];
if (
$comments == 0) {
echo 
'<p>There are no comments</p>'
} else {
echo 
'<h5 class="commentsname">' $name '</h5>';
echo 
'<p class="commentstext">' $comment '</p>'
I think?
Good luck

~Dean

Last edited by c010depunkk; January 19th, 2008 at 05:17 AM.. Reason: please use [PHP] tags when posting PHP
Bravo81 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old January 18th, 2008, 03:50 PM   #5
WebForumz Admin Badge
SuperMember
 
Marc's Avatar
 

Join Date: Apr 2007
Location: Scotland, UK
Posts: 2,086
Thanks: 2
Thanked 23 Times in 23 Posts
Rep Altering Power: 89 Marc is just really nice Marc is just really nice Marc is just really nice Marc is just really nice
Re: Help with an IF Else statement & MySQL

Code:
<?php
$display_comments 
= @mysql_query("SELECT commentsunique, commentsid, name, comment FROM comments WHERE commentsid='$contentid' ORDER BY commentsunique DESC");
$rows mysql_num_rows($display_comments);
if(
$rows >= 1) {
    while (
$comments mysql_fetch_array($display_comments)) {
    
$commentid $comments['commentsid'];
    
$name $comments['name'];
    
$comment $comments['comment'];
    echo 
'<h5 class="commentsname">' $name '</h5>';
    echo 
'<p class="commentstext">' $comment '</p>'
    }
} else {
    
// no commetns to show
}
?>
__________________
Regards
Marc
Administrator - Webforumz.com

Web Design and Development Blog - Marc Fraser
Marc is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old January 18th, 2008, 04:08 PM   #6
Most Reputable Member
 

Join Date: May 2007
Location: Cornwall, England
Posts: 1,421
Blog Entries: 8
Thanks: 18
Thanked 14 Times in 14 Posts
Rep Altering Power: 0 Jack Franklin will become famous soon enough
Re: Help with an IF Else statement & MySQL

Thanks guys. I'll try it now

Rakuli, yours works perfectly. Thanks for the help guys
__________________
Yours is the Earth and everything that's in it
And - which is more - you'll be a Man my son!

Last edited by Jack Franklin; January 18th, 2008 at 04:11 PM..
Jack Franklin is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP If Statement... mcdanielnc89 PHP 16 December 9th, 2007 01:44 PM
If..Else statement help IanW PHP 3 October 6th, 2006 09:40 AM
SELECT statement gecastill Databases 1 February 15th, 2006 07:27 PM
Help with If Statement. JohnMitch Classic ASP 2 January 4th, 2005 09:05 PM
With Statement Trebz Classic ASP 2 February 2nd, 2004 10:56 AM


Search Engine Optimization by vBSEO 3.2.0 RC8