Page 1 of 1

if / elseif in Loop - SOLVED

Posted: Tue Aug 29, 2006 3:15 pm
by psurrena
My if /elseif statement does not work properly in the while loop. Can someone explain why?

Code: Select all

<?php
		include 'includes/connect.php';
					
		$query   = "SELECT * FROM article, image WHERE article.a_id=image.a_id ORDER BY article.a_id DESC";
		$result  = mysql_query($query) or die('Error : ' . mysql_error());  
	
		while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { 
			
			$a_id		= $row['a_id'];
			$title 	 	= $row['title'];
			$abstract	= $row['abstract'];
			$date	 	= $row['date'];
			$path	 	= $row['path'];
			$iurl		= $row['iurl'];
			$none		= $row['none'];

			$organize	= $row['organize'];
			$date		= date("F d Y");
		
			$content	 = '<div id="entry">';
			
			if (path == TRUE) {
				$content 	.= '<img class="entry" src="' . $path . '" width="150" align="right">';
			} elseif (iurl == TRUE) {
				$content 	.= '<img class="entry" src="' . $iurl . '" width="150" align="right">';
			}
		
			$content	.= '<div id="entry-copy">';
			more $content......
			

			echo $content;	
		}
			
		mysql_close();
	?>

Posted: Tue Aug 29, 2006 3:16 pm
by blackbeard
else if is two words, not one

Posted: Tue Aug 29, 2006 3:19 pm
by Oren
blackbeard wrote:else if is two words, not one
It doesn't matter, both will work.

psurrena, it should be:

Code: Select all

if (condition)
{

}

else
{

}
Not:

Code: Select all

if (condition)
{

}

elseif
{

}
P.S I didn't read the rest of the code, so maybe there are more mistakes.

Posted: Tue Aug 29, 2006 3:21 pm
by psurrena
That didn't fix it. It seems to only check if path==true and then if it's true once it doesn't check for iurl

Posted: Tue Aug 29, 2006 3:26 pm
by blackbeard
If I understand your last post, what you need is two if statements. One to check path, and the other to check the iurl.

Also, inside your if statement, do you mean to check $path and $iurl?

Posted: Tue Aug 29, 2006 3:33 pm
by psurrena
Got it

Thank you, it has to be a variable since both would always be true if it just check the DB and both were true at least once. Right?

Code: Select all

if ($iurl == TRUE) {
	$content  .= '<img class="entry" src="' . $iurl . '" width="150" align="right">';
} elseif ($path == TRUE) {
	$content 	.= '<img class="entry" src="' . $path . '" width="150" align="right">';
}

Posted: Tue Aug 29, 2006 3:37 pm
by blackbeard
Change the elseif to else. You only use the elseif when you have two or more comparisons.

Code: Select all

if ($i ==1)
  echo "true";

if ($i == 1)
  echo "true";
else
  echo "false";

if ($i == 1)
  echo "I is 1";
else if ($i == 2)
  echo "I is 2";
else
  echo "I is not 1 or 2";

Posted: Tue Aug 29, 2006 3:45 pm
by blackbeard
Re-reading your post, I think what you need is something like this:

Code: Select all

if (!is_null($iurl)) {
        $content  .= '<img class="entry" src="' . $iurl . '" width="150" align="right">';
} elseif (!is_null($path)) {
        $content        .= '<img class="entry" src="' . $path . '" width="150" align="right">';
}
else {
  //  error handling
}

Posted: Tue Aug 29, 2006 3:50 pm
by psurrena
Why would one use !is_null instead of ==True?

Posted: Tue Aug 29, 2006 4:01 pm
by blackbeard
In your specific case, I don't see an problem using == TRUE, for me, it's user preference.

However, you can have it where !is_null($var) evaluates to true, but ($var == TRUE) evaluates to false. As long as you know what to expect from the database, you can code it using your preferences.