Jump to content

[SOLVED] if else not working


PHP Nubsauce

Recommended Posts

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 } ?>

Link to comment
https://forums.phpfreaks.com/topic/118177-solved-if-else-not-working/
Share on other sites

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.

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

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 } ?>

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

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.