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
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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.