if / elseif in Loop - SOLVED

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

if / elseif in Loop - SOLVED

Post 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();
	?>
Last edited by psurrena on Tue Aug 29, 2006 6:21 pm, edited 1 time in total.
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Post by blackbeard »

else if is two words, not one
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post 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.
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Post 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
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Post 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?
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Post 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">';
}
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Post 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";
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Post 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
}
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Post by psurrena »

Why would one use !is_null instead of ==True?
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

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