IF $id for user is empty or null, then do else if?? HELP

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
danjapro
Forum Commoner
Posts: 72
Joined: Mon Sep 27, 2004 10:56 am

IF $id for user is empty or null, then do else if?? HELP

Post by danjapro »

This piece of code works perfect if the user has a $id listed in DB and is returned from the SQL Query that collects the need values, which first follws this piece of code.

However, if the user does not have a $id in that table, it should do the else if, but it goes straight to the last else.

What should the else if($id == ' ') or else if(empty($id == ' '))??
PLEASE HELP..

Code: Select all

	if ($id) {
		if ($user->get('id') == 0 || $userid == 0 || $userid <> $user->get('id')){	
     		//$url = JURI::root() . 'index.php?' . $component . '&id=' . $id . '&tmpl=component&print=1';
      	 	$url=JURI::root().'index.php?'.$component.'&id='.$id.'&tmpl=component&print=1';
	   		for($i=1;$i<count($type);$i++){
	    		$url.='&'.$type[$i].'='.$layout[$i];
	  		}
		}
	} else if($id == ' ') {
      	 	$url=JURI::root().'index.php?option=com_muscol&view=album&id=27&tmpl=component&print=1';
	   		for($i=1;$i<count($type);$i++){
	    		$url.='&'.$type[$i].'='.$layout[$i];
	  }
    } else {
		echo 'User Album Loading...';
		} 
		
}
Last edited by califdon on Thu Apr 07, 2011 11:50 pm, edited 1 time in total.
Reason: Moderator changed [code] tags to [syntax=php] tags for easier reading.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: IF $id for user is empty or null, then do else if?? HEL

Post by califdon »

Your else if($id == ' ') condition is apparently not being met. That's not surprising, since it would be quite odd if the $id value was a space character. You probably mean either a Null value or a zero-length string. These are all seen as different values by PHP. The easy solution is to use the PHP function empty(), which tests for ANY of these conditions: a numeric value of 0, a zero-length string, OR a Null.

else if(empty($id) )
danjapro
Forum Commoner
Posts: 72
Joined: Mon Sep 27, 2004 10:56 am

Re: IF $id for user is empty or null, then do else if?? HEL

Post by danjapro »

Tried this and still NOTHING;

Code: Select all

	if ($id) {
		if ($user->get('id') == 0 || $userid == 0 || $userid <> $user->get('id')){	
     		//$url = JURI::root() . 'index.php?' . $component . '&id=' . $id . '&tmpl=component&print=1';
      	 	$url=JURI::root().'index.php?'.$component.'&id='.$id.'&tmpl=component&print=1';
	   		for($i=1;$i<count($type);$i++){
	    		$url.='&'.$type[$i].'='.$layout[$i];
	  		}
		}
	/*MG made this change here to get the user to got ot set Default Account if they have not one yet. Account Album ID = 27. CaribbeanHeat.tv Main Account.Yah. Mg all the way baby. 042776*/
			
	} else if(empty($id)){
      	 	$url=JURI::root().'index.php?option=com_muscol&view=album&id=27&tmpl=component&print=1';
	   		for($i=1;$i<count($type);$i++){
	    		$url.='&'.$type[$i].'='.$layout[$i];
	  }
    } else {
		echo 'User Album Loading...';
		} 
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: IF $id for user is empty or null, then do else if?? HEL

Post by califdon »

I just noticed that you have separated "else if". Change that to "elseif".
Post Reply