Not showing nulls.....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

melindaSA
Forum Commoner
Posts: 99
Joined: Thu Oct 02, 2003 7:34 am

Not showing nulls.....HELP

Post 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>';
}
User avatar
Derfel Cadarn
Forum Contributor
Posts: 193
Joined: Thu Jul 17, 2003 12:02 pm
Location: Berlin, Germany

Post 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.
melindaSA
Forum Commoner
Posts: 99
Joined: Thu Oct 02, 2003 7:34 am

Post by melindaSA »

:D Thank you that works!
User avatar
Derfel Cadarn
Forum Contributor
Posts: 193
Joined: Thu Jul 17, 2003 12:02 pm
Location: Berlin, Germany

Post 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...
melindaSA
Forum Commoner
Posts: 99
Joined: Thu Oct 02, 2003 7:34 am

Post by melindaSA »

Thanks, I will try that, I learn something new everyday!
User avatar
Derfel Cadarn
Forum Contributor
Posts: 193
Joined: Thu Jul 17, 2003 12:02 pm
Location: Berlin, Germany

Post by Derfel Cadarn »

me 2! :P
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

Same here :D. I didn't know you could do that? LOL :P

-Nay
User avatar
Derfel Cadarn
Forum Contributor
Posts: 193
Joined: Thu Jul 17, 2003 12:02 pm
Location: Berlin, Germany

Post 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?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

In $random-country. ;)
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

haha nice one JAM :D.

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

-Nay
melindaSA
Forum Commoner
Posts: 99
Joined: Thu Oct 02, 2003 7:34 am

Post 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; 
}




?>
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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.
melindaSA
Forum Commoner
Posts: 99
Joined: Thu Oct 02, 2003 7:34 am

Post by melindaSA »

Thank you, I am new to PHP......
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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. ;)
chris22
Forum Newbie
Posts: 11
Joined: Tue Apr 22, 2003 9:45 pm

Re: Not showing nulls.....HELP

Post 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);
?>
Post Reply