Page 1 of 2

Not showing nulls.....HELP

Posted: Thu Oct 02, 2003 7:34 am
by melindaSA
I am quering a MySQL db, for contact information, and some of the fields are NULL allowed. How do I not pull the NULL info to the web page (a empty line on the web page)? For example, some entries do not have an Address 2.
Here is my code:

Code: Select all

<?php
function display_physicians_details($physicians)
{
  // display all details about this physician
  if (is_array($physicians))
  
  {
    echo '<table><tr>'; 
    //display the picture if there is one 
    if (@file_exists('images/'.($physicians['last_name']).'.jpg'))
    {
      $size = GetImageSize('images/'.$physicians['last_name'].'.jpg');
      if($size[0]>0 && $size[1]>0)
        echo '<td><img src=''images/'.$physicians['last_name'].'.jpg'' border=0 '.$size[1].'></td>';
    
    }
 	
    echo '<td>';
    echo '<H2>';
    echo $physicians['title'];
    echo '.<b></b> ';
    echo $physicians['first_name'];
    echo '<b></b> ';
    echo $physicians['last_name'];
    echo '<b></b></H2> ';
    echo '<b></b> ';
    echo $physicians['specialty_ID'];
    echo '<br><b></b><br> ';
    echo $physicians['address'];
    echo '<b></b> ';
    echo $physicians['address2'];
    echo '<br><b></b> ';
    echo $physicians['city'];
    echo '<b></b> ';
    echo $physicians['state'];
    echo '<b></b> ';
    echo $physicians['zip'];
    echo '<br>Phone: ';
    echo $physicians['telephone'];
    echo '<br>Fax: ';
    echo $physicians['fax'];
    echo '<br><br><b>Medical Degree School</b><br> ';
    echo $physicians['school_attended'];
    echo '<br><br><b>Board Certified</b><br> ';
    echo $physicians['board_certification_ID'];
    echo '</td></tr></table>'; 
  }
  else
    echo 'The details of this Physician cannot be displayed at this time.';
  echo '<br>';
}

Posted: Thu Oct 02, 2003 7:52 am
by Derfel Cadarn
You could try it with this:

Code: Select all

<?php
if ($physicians['address2']) {
      echo $physicians['address2'];
      echo '<br><b></b> '; 
}
?>
That should check wether the adress2 exists and if not, there's no output.

Posted: Thu Oct 02, 2003 8:15 am
by melindaSA
:D Thank you that works!

Posted: Thu Oct 02, 2003 8:29 am
by Derfel Cadarn
Glad to be of help.

btw why don't you compress your echo-commands into something like:

Code: Select all

<?php
foreach ($physician as $k) {
     echo "<b>".key($k)."</b>: ".$k."<br>";
}

?>
or something like that...

Posted: Thu Oct 02, 2003 8:46 am
by melindaSA
Thanks, I will try that, I learn something new everyday!

Posted: Thu Oct 02, 2003 8:49 am
by Derfel Cadarn
me 2! :P

Posted: Thu Oct 02, 2003 8:59 am
by Nay
Same here :D. I didn't know you could do that? LOL :P

-Nay

Posted: Thu Oct 02, 2003 9:10 am
by Derfel Cadarn
LOL, that's about the only thing I understand about array's in PHP...I hate the things! 8O

btw Nay, where is $random-city?

Posted: Thu Oct 02, 2003 9:29 am
by JAM
In $random-country. ;)

Posted: Thu Oct 02, 2003 10:33 am
by Nay
haha nice one JAM :D.

nah, I'm stuck in Ipoh, Malaysia...

-Nay

Posted: Thu Oct 02, 2003 1:01 pm
by melindaSA
Okay, I am stuck again......with the same function above.

Can I call a function within a function, if so how? I am trying to pull the board_certification name for the board_certification_ID, by using the following function:

Code: Select all

<?php
function get_board_certification_name($board_certification_ID)
{
   // query database for the name for a board certification id
   $conn = db_connect();
   $query = "select board_certification
             from board_certification 
             where board_certification_ID = $board_certification_ID"; 
   $result = @mysql_query($query);
   if (!$result)
     return false;
   $num_board_certification = @mysql_num_rows($result);
   if ($num_board_certification ==0)
      return false;  
   $result = mysql_result($result, 0, 'board_certification');
   return $result; 
}




?>

Posted: Fri Oct 03, 2003 4:54 am
by JAM
Yes you can. Don't take me wrong, but it seems that you know enough to manage to do this allready... Simple example:

Code: Select all

function incr($value) {
    return $value + 1;
}
function test($value) {
    return incr($value);
}

echo test(1);
// 2
Should be understandable enough to fit your needs i hope.

Posted: Fri Oct 03, 2003 9:09 am
by melindaSA
Thank you, I am new to PHP......

Posted: Fri Oct 03, 2003 10:23 am
by JAM
Ah... Based on the function you pasted above, I thought you made that one yourself, hence a non-saying example and perhaps a too easy example.

So if you are new to php, and still need more info, let us know. ;)

Re: Not showing nulls.....HELP

Posted: Fri Oct 03, 2003 11:15 am
by chris22
melindaSA wrote:

Code: Select all

<?php
// snip
 	
    echo '<td>';
    echo '<H2>';
    echo $physicians['title'];
    echo '.<b></b> ';
    echo $physicians['first_name'];
    echo '<b></b> ';
    echo $physicians['last_name'];
    echo '<b></b></H2> ';
// snip
?>
What's with all of the extra junk tags? Those bold tags aren't doing anything, just get rid of them! Learning HTML from a wizzywig is a bad idea!

Code: Select all

<?
echo '<td><h2>' . $physicians['title'] . ' ' . $physicians['first_name'] . ' ' . $physicians['last_name'] . '</h2>';
?>
or even better:

Code: Select all

<?
function remove_empty_elements($var) {
	return !empty($var);
}

$name_array = array($physicians['title'], $physicians['first_name'], $physicians['last_name']);

// filtering out empty elements, to eliminate extra spaces

$name_array = array_filter($name_array, 'remove_empty_elements');

print '<td><h2>' . implode(' ' , $name_array) . '</h2>';
?>
The same concept could be applied by the address as well:

Code: Select all

<?
$address_array = array($address1, $address2, $city . ', ' . $state . ' ' . $zip); // add elements as needed.

// filter out the empty elements

$address_array = array_filter($address_array, 'remove_empty_elements');

print implode('<br>', $address_array);
?>