Jump to content

How to print nothing if doesn't exist in database


Recommended Posts

I am pulling contact info from my database which has ID, firstname, lastname, address, suite, state, zipcode in it. How do write the code so that if no address is in the system, it doesn't show "Suite #" and the "," in the address cell? Is there a better way to do this than how I am? Obviously I still want their name show up regardless of the address being there. Here is my code:

 

 <?php
   $ID=$_GET['ID'];
   
   require("xxxx.php");

   $sql = ("SELECT * FROM contacts ORDER BY firstname ASC") or die ('Error: ' .mysql_error());
   $query=mysql_query($sql);
   while ($row = mysql_fetch_array($query)) {
   ?>
   <tbody>
   <tr> 
   <td><input type="checkbox" name="" id="" value="<?php echo $row['ID']; ?>"></td>
   <td><a href="/backend/contacts/editcontact.php?ID= <?php echo $row['ID']; ?> "><strong><?php echo $row['firstname']; ?> <?php echo $row['lastname']; ?></strong></a></td>
   <td><?php echo $row['address']; ?> <? echo "Suite #{$row['suite']},"; ?> <? echo $row['zipcode']; ?></td>
   </tr>
   </tbody> 
   <?php  
    }
    mysql_close();
    ?>

if statement

 

 <?php
   $ID=$_GET['ID'];
   
   require("xxxx.php");

   $sql = ("SELECT * FROM contacts ORDER BY firstname ASC") or die ('Error: ' .mysql_error());
   $query=mysql_query($sql);
   while ($row = mysql_fetch_array($query)) {
   if($row['address'] != ""){
   ?>
   <tbody>
   <tr> 
   <td><input type="checkbox" name="" id="" value="<?php echo $row['ID']; ?>"></td>
   <td><a href="/backend/contacts/editcontact.php?ID= <?php echo $row['ID']; ?> "><strong><?php echo $row['firstname']; ?> <?php echo $row['lastname']; ?></strong></a></td>
   <td><?php echo $row['address']; ?> <? echo "Suite #{$row['suite']},"; ?> <? echo $row['zipcode']; ?></td>
   </tr>
   </tbody> 
   <?php
    }else{
   print "Address is empty";
    }  
    }
    mysql_close();
    ?>

Great! I have 6 users in the system now, but using this code you gave, it only prints out 2 contacts because the rest don't have an address. I still want the others to print, just not show the words "Suite #" and "," in that cell, I want it just blank. Does that make sense?

Do this then

 

 <?php
   $ID=$_GET['ID'];
   
   require("xxxx.php");

   $sql = ("SELECT * FROM contacts ORDER BY firstname ASC") or die ('Error: ' .mysql_error());
   $query=mysql_query($sql);
   while ($row = mysql_fetch_array($query)) {
    $address = ($row['address'] == "") ? "" :  $row['address'].  " Suite #{$row['suite']}," . $row['zipcode']; 
   ?>
   <tbody>
   <tr> 
   <td><input type="checkbox" name="" id="" value="<?php echo $row['ID']; ?>"></td>
   <td><a href="/backend/contacts/editcontact.php?ID= <?php echo $row['ID']; ?> "><strong><?php echo $row['firstname']; ?> <?php echo $row['lastname']; ?></strong></a></td>
   <td><?php print $address; ?></td>
   </tr>
   </tbody> 
   <?php

    }
    mysql_close();
    ?>

Do this then

 

 <?php
   $ID=$_GET['ID'];
   
   require("xxxx.php");

   $sql = ("SELECT * FROM contacts ORDER BY firstname ASC") or die ('Error: ' .mysql_error());
   $query=mysql_query($sql);
   while ($row = mysql_fetch_array($query)) {
    $address = ($row['address'] == "") ? "" :  $row['address'].  " Suite #{$row['suite']}," . $row['zipcode']; 
   ?>
   <tbody>
   <tr> 
   <td><input type="checkbox" name="" id="" value="<?php echo $row['ID']; ?>"></td>
   <td><a href="/backend/contacts/editcontact.php?ID= <?php echo $row['ID']; ?> "><strong><?php echo $row['firstname']; ?> <?php echo $row['lastname']; ?></strong></a></td>
   <td><?php print $address; ?></td>
   </tr>
   </tbody> 
   <?php

    }
    mysql_close();
    ?>

 

Isn't it a good approach to pass in IF statement such field that is used (compulsary) in all the saved data like id ???

Here's what I would do. This will make the address display correctly if any piece is missing

 

 <?php

   require("xxxx.php");
   $ID = $_GET['ID'];

   $sql = "SELECT * FROM contacts ORDER BY firstname ASC";
   $query = mysql_query($sql) or die ('Error: ' .mysql_error());
   while ($row = mysql_fetch_array($query))
   {
       $space = (!empty($row['firstname']) && !empty($row['lastname'])) ? ' ' : ''; 
       $name = $row['firstname'].$space.$row['lastname'];
       $address  = $row['address'];
       $address .= (!empty($row['suite'])) ? " Suite #{$row['suite']}" : '';
       $address .= (!empty($row['zipcode'])) ? ", {$row['zipcode']}" : '';
   ?>
   <tbody>
     <tr>
       <td><input type="checkbox" name="" id="" value="<?php echo $row['ID']; ?>"></td>
       <td><a href="/backend/contacts/editcontact.php?ID=<?php echo $row['ID']; ?>"><strong><?php echo $name; ?></strong></a></td>
       <td><?php echo $address; ?></td>
     </tr>
   </tbody>
   <?php
    }
    mysql_close();
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.