Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If value==nothing, value=something 1

Status
Not open for further replies.

curvv

IS-IT--Management
Jan 11, 2001
103
ZA
Hi,
Im retrieving data from a DB with WHILE structure:

while(odbc_fetch_row($result)){
$user_id = odbc_result($result, 1);
$name = odbc_result($result, 4);
$uma = odbc_result($result, 2);
$ma = odbc_result($result, 3);
$tel = odbc_result($result, 5);
$fax = odbc_result($result, 6);
$cell = odbc_result($result, 7);
$loc = odbc_result($result, 8);
$mail = odbc_result($result, 9);
$dept = odbc_result($result, 10);

printf(&quot;<tr>
<td>$name</td>
<td>$user_id</td>
<td>$uma</td>
<td>$ma</td>
<td>$tel</td>
<td>$fax</td>
<td>$cell</td>
<td>$loc</td>
<td><a href=\&quot;mailto:$mail\&quot;>$mail</a></td>
<td>$dept</td>
</tr>\n&quot;);
}

My question:

If no value is found for a certain field, I want to specify a default value.

example:

If no telephone number was found for some employees, a default value (ex:1234 instead of nothing  ) must be displayed.

Please can you help me with this?

thanx
chris
 
You could use the standard if...else routing, but this is a perfect place to use the ternary operator (
while(odbc_fetch_row($result)){
$user_id = odbc_result($result, 1);
$name = odbc_result($result, 4);
$uma = odbc_result($result, 2);
$ma = odbc_result($result, 3);
$tel = (odbc_result($result, 5) == 0) ? 1234 : odbc_result($result, 5);
$fax = odbc_result($result, 6);
$cell = odbc_result($result, 7);
$loc = odbc_result($result, 8);
$mail = odbc_result($result, 9);
$dept = odbc_result($result, 10);

printf(&quot;<tr>
<td>$name</td>
<td>$user_id</td>
<td>$uma</td>
<td>$ma</td>
<td>$tel</td>
<td>$fax</td>
<td>$cell</td>
<td>$loc</td>
<td><a href=\&quot;mailto:$mail\&quot;>$mail</a></td>
<td>$dept</td>
</tr>\n&quot;);
}

The expression that is on the right side of the &quot;=&quot; for $tel basically asks if &quot;(odbc_result($result, 5)&quot; has a value of zero, and if so, then sets the whole expression equal to 1234, but if it is not true, it sets the expression equal to &quot;(odbc_result($result, 5)&quot;.

If the $tel field is a string instead of a numerical datatype, then you could use:

$tel = (odbc_result($result, 5) == &quot;&quot;) ? &quot;1234&quot; : odbc_result($result, 5);
 
Hi,

Thanx alot. Exactly what I was looking for. At the moment I'm using the if else routing and was looking for an alternative.

thanx again ######## CtN ########
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top