PHP Nubsauce Posted August 5, 2008 Share Posted August 5, 2008 Can somebody tell me why the if works fine - it shows the database information, but the else does not give the message saying faq hasent been added yet - <?php if(mysql_num_rows($links) != 0){ ?><?php echo($question); ?></strong> <?php echo($answer); } else { echo('There is currently no FAQ added'); } ?> </p> <p class="text"> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/118177-solved-if-else-not-working/ Share on other sites More sharing options...
DeanWhitehouse Posted August 5, 2008 Share Posted August 5, 2008 try an elseif elseif(mysql_num_rows($links) >= 1) Quote Link to comment https://forums.phpfreaks.com/topic/118177-solved-if-else-not-working/#findComment-608106 Share on other sites More sharing options...
DarkWater Posted August 5, 2008 Share Posted August 5, 2008 No, don't try an elseif. Also, why do you close the strong tag in the IF but not in the ELSE? It would probably make everything bold then, but whatever. You're allowed to use whitespace for a reason, by the way. Try spacing it out and see what you can find. Quote Link to comment https://forums.phpfreaks.com/topic/118177-solved-if-else-not-working/#findComment-608107 Share on other sites More sharing options...
kenrbnsn Posted August 5, 2008 Share Posted August 5, 2008 We need to see the code preceding this snippet to be able to tell what's wrong. Here is your code fixed up a bit... <?php if(mysql_num_rows($links) != 0) echo $question . '</strong>' . $answer; else echo 'There is currently no FAQ added'; } ?> </p> <p class="text"> Ken Quote Link to comment https://forums.phpfreaks.com/topic/118177-solved-if-else-not-working/#findComment-608113 Share on other sites More sharing options...
DeanWhitehouse Posted August 5, 2008 Share Posted August 5, 2008 why not try elseif? it will do the same thing Quote Link to comment https://forums.phpfreaks.com/topic/118177-solved-if-else-not-working/#findComment-608114 Share on other sites More sharing options...
revraz Posted August 5, 2008 Share Posted August 5, 2008 Then why bother? why not try elseif? it will do the same thing Quote Link to comment https://forums.phpfreaks.com/topic/118177-solved-if-else-not-working/#findComment-608117 Share on other sites More sharing options...
DeanWhitehouse Posted August 5, 2008 Share Posted August 5, 2008 just to be sure Quote Link to comment https://forums.phpfreaks.com/topic/118177-solved-if-else-not-working/#findComment-608129 Share on other sites More sharing options...
PHP Nubsauce Posted August 5, 2008 Author Share Posted August 5, 2008 I tried else if as well, im not getting any errors, it just doesent display the "None" text heres whats before it .. <?php $links = @mysql_query("SELECT * FROM ".$mysql_pretext."_faq WHERE faq_type_id = '1' ORDER BY question DESC"); if (!$links) { echo("Error retrieving faq from the database!<br>Error: " . mysql_error()); exit(); } while ($link = mysql_fetch_array($links)) { $id = $link["ID"]; $question = $link["question"]; $answer = $link["answer"]; ?> </p> </h1> <p class="text"><strong> <?php if(mysql_num_rows($links) != 0){ ?><?php echo($question); ?></strong> <?php echo($answer); } else { echo('There is currently no FAQ added'); } ?> </p> <p class="text"> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/118177-solved-if-else-not-working/#findComment-608141 Share on other sites More sharing options...
DarkWater Posted August 5, 2008 Share Posted August 5, 2008 If there are no rows, the while loop isn't executed at all, thereby bypassing your else. Rework your logic to include the check at the top before the while, and inside the else put the while (make the if that "Sorry there are no faq's" thing). Quote Link to comment https://forums.phpfreaks.com/topic/118177-solved-if-else-not-working/#findComment-608143 Share on other sites More sharing options...
kenrbnsn Posted August 5, 2008 Share Posted August 5, 2008 Since the "if" is included within the "while" loop that will only get executed when there are rows, the "else" path will never get followed. You need to do the "if" before the while. Try: <?php $q = "SELECT * FROM ".$mysql_pretext."_faq WHERE faq_type_id = '1' ORDER BY question DESC"; $links = mysql_query($q) or die ("Error retrieving faq from the database!<br>Error: " . mysql_error()); if (mysql_num_rows($links) == 0) echo 'There is currently no FAQ added'; else { while ($link = mysql_fetch_array($links)) { $id = $link["ID"]; $question = $link["question"]; $answer = $link["answer"]; echo '</p></h1><p class="text"><strong>'; echo $question . '</strong>' . $answer . '</p>'; echo '<p class="text">'; } } ?> Code fixed & properly indented. Ken Quote Link to comment https://forums.phpfreaks.com/topic/118177-solved-if-else-not-working/#findComment-608149 Share on other sites More sharing options...
PHP Nubsauce Posted August 5, 2008 Author Share Posted August 5, 2008 Thanks for the help guys, You were right - I had to do it before the where. Thanks, Nubsauce. Quote Link to comment https://forums.phpfreaks.com/topic/118177-solved-if-else-not-working/#findComment-608157 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.