If (row == NULL)

For a site I am working on I have a main table profielen (profiel_id, gebruikersnaam, etc). Next to that I have different relational tables i.e. profiel_burgelijke_staat (profiel_id, burgelijke_staat_id) and ofcource the tables holding the string values for those relational tables i.e. burgelijke_staat(burgelijke_staat_id, burgelijke_staat) . During the sign up process in many cases the new profiel_id will be added to the corresponding table but the relational value won’t . For example the table profiel_marital_status will have the following value after a new sign up of profiel 101( 101, NULL). The query for this part would look like this:


			$sql_pro = "SELECT P.profiel_id,
			                   P.gebruikersnaam,
							   BS.burgelijke_staat
							
                         FROM  profielen P
						LEFT
						 JOIN  profiel_burgelijke_staat PBS ON PBS.profiel_id = P.profiel_id
						LEFT
						 JOIN  burgelijke_staat BS ON BS.burgelijke_staat_id = PBS.burgelijke_staat_id						 						
						WHERE
						       P.profiel_id = :profiel
					      AND
						       isActive = :isActive";

In the while loop I need some kind of if{}else{} statement to determine if the value is not null, i.e. if ($row[‘burgelijke_staat_id’]== ‘’){$burgelijke_staat= “Tell you later”;}else{$burgelijke_staat=$row[‘burgelijke_staat’];} Obviously did I try this approach but that isn’t working. How should I approach this?.

Kind regards.

I use empty() in cases like this. Based on the code provided would be

if (empty($row['burgelijke_staat_id'])){
	$burgelijke_staat= "Tell you later";
}else{
	$burgelijke_staat=$row['burgelijke_staat'];
} 

Or at times you might want the order reversed so you would use IF (!empty()) which would be if not empty.

Hi Drummin. Do you think that I need to declare which burgelijke_staat_id (i.e. PBS.burgelijke_staat_id or BS.burgelijke_staat_id) ? Because to test this I have added a value to one of the rows in profiel_burgelijke_staat but I get Tell you later and it shoud give the value yes instead.

Thank you in advance

I suppose if I understood what the field names meant it would be more clear what you are talking about. Do you mean something like this?

if (empty($row['burgelijke_staat_id'])){
	$burgelijke_staat = "Tell you later";
}else{
	$burgelijke_staat = (!empty($row['burgelijke_staat']) ? "Yes" : "No");
}

Hi Drummin. What I mean is that if the value of the field burgelijke_staat_id in the table profiel_burgelijke_staat is empty or NULL I should get the message Tell you later. If there is a value, i.e. 1 = no, 2 = yes, 3 = separated. So if the value is not NULL or empty. (when someone subscribes the value goes in as NULL) it should display one of these string values.

ONE way to do this is to use an array with those numbers as the key then check if the key exists and use this to show the value of that key.

if (empty($row['burgelijke_staat_id'])){
	$burgelijke_staat = "Tell you later";
}else{
	$burgelijke_values = array(1 => 'no', 2 => 'yes', 3 => 'separated');
	$burgelijke_staat = (array_key_exists($row['burgelijke_staat_id'],$burgelijke_values) ? "{$burgelijke_values[$row['burgelijke_staat_id']]}" : "N/A");
}

Works great Drummin thanks a lot :tup:

Off Topic:

Why do you have a join table profiel_burgelijke_staat to join profiel and burgerlijke_staat? A person can have exactly one burgerlijke staat, so it would make sense to do away with the join table and just add burgerlijke_staat_id to the profiel table. Unless I’m missing something?

You are absolutely right ScallioXTX. This happened during the development period. There were so many features that actually needed that extra table that I automatically did the same for burgerlijke_staat as well. I have been thinking of changing it