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();
    ?>

Link to comment
Share on other sites

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();
    ?>

Link to comment
Share on other sites

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();
    ?>

Link to comment
Share on other sites

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 ???

Link to comment
Share on other sites

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();
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.