Current location: Hot Scripts Forums » Programming Languages » PHP » rookie question about blank fields

rookie question about blank fields

Reply
  #1 (permalink)  
Old 11-02-09, 01:14 PM
topsidemarketing topsidemarketing is offline
Newbie Coder
 
Join Date: Aug 2009
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
rookie question about blank fields

Hi guys,

I'm successfully pulling data from the MySQL database and displaying the output in an HTML table using 'echo $_variable' within the table tags. This is working famously.

My only problem is that in just a few cases in the database records, one of the fields has been left blank or empty and that blank field leaves an obvious white space (empty table row) in the output table - example: If all the fields have content, the display looks like this:

Company name
Contact name
Address 1
Address 2
City, State Zip

And that's fine. However, if, for example, the 'contact name' field is blank in the database, the output will look like this:

Company name

Address 1
Address 2
City, State Zip.

Is there a way, using the PHP in the display page, to get the output to skip the blank field altogether, so that with a blank 'contact name' field, the output will be:

Company name
Address 1
Address 2
City, State zip...

with no blank table row where the contact name should be?

I suspect this is a rookie question and that there's an easy fix, but don't know what it should be.

THANKS in advance...all help appreciated!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 11-02-09, 05:51 PM
Jcbones Jcbones is offline
Coding Addict
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 280
Thanks: 3
Thanked 5 Times in 5 Posts
Without your code, I cannot help alot, but you could add some if statements to it.

PHP Code:
$result mysql_query($sql);
while(
$row mysql_fetch_assoc($result)) {
if(!empty(
$row['company'])) {
  echo 
'<tr><td>Company name</td></tr>';
}
if(!empty(
$row['contact'])) {
  echo 
'<tr><td>Contact name</td></tr>';
}
if(!empty(
$row['add1'])) {
  echo 
'<tr><td>Address 1</td></tr>';
}
if(!empty(
$row['add2'])) {
  echo 
'<tr><td>Address 2</td></tr>';
}
if(!empty(
$row['city'])) {
  echo 
'<tr><td>City, State Zip</td></tr>';
}

Or something like that.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 11-03-09, 02:58 AM
topsidemarketing topsidemarketing is offline
Newbie Coder
 
Join Date: Aug 2009
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Here's the code...does it help?:

***************code****************

<?
include("connect.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM resellers WHERE state='CA' ORDER BY city, coname";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
$i=0;
while ($i < $num) {
$coname=mysql_result($result,$i,"coname");
$contactfirst=mysql_result($result,$i,"contactfirs t");
$contactlast=mysql_result($result,$i,"contactlast" );
$address1=mysql_result($result,$i,"address1");
$address2=mysql_result($result,$i,"address2");
$city=mysql_result($result,$i,"city");
$state=mysql_result($result,$i,"state");
$zip=mysql_result($result,$i,"zip");
$phone=mysql_result($result,$i,"phone");
$country=mysql_result($result,$i,"country");
$URL=mysql_result($result,$i,"URL");
$insurance=mysql_result($result,$i,"insurance");
$accreditation=mysql_result($result,$i,"accreditat ion");
$certfitter=mysql_result($result,$i,"certfitter");
?>
<table><TR><TD colSpan="2"><H6><? echo $city; ?></H6></TD>
</TR>
<TR>
<TD><strong><? echo $coname; ?></strong></TD>
<TD><? echo $certfitter; ?></TD>
</TR>
<TR>
<TD><? echo $contactfirst." ".$contactlast; ?></TD>
<TD><? echo $insurance; ?></TD>
</TR>
<TR>
<TD><? echo $address1." ".$address2; ?> </TD><TD><? echo $accreditation; ?></TD>
</TR>
<TR>
<TD colSpan="2"><? echo $city.", ".$state." ".$zip; ?></TD>
</TR>
<TR>
<TD colSpan="2"><? echo $phone; ?></TD>
</TR>
<TR>
<TD colSpan="2"><A href="http://<? echo $URL; ?>"><? echo $URL; ?></A></TD>
</TR>
<TR>
<TD colSpan="2"></TD>
</TR><tr colspan="2"><td>&nbsp;</td></tr><br>

</TBODY>
<?
$i++;
}
echo "</table>";
?>

Last edited by topsidemarketing; 11-03-09 at 03:02 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 11-03-09, 06:46 PM
Jcbones Jcbones is offline
Coding Addict
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 280
Thanks: 3
Thanked 5 Times in 5 Posts
Try this:

PHP Code:
<?php
include("connect.php");
mysql_connect(localhost,$username,$password);
@
mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM resellers WHERE state='CA' ORDER BY city, coname";
$result=mysql_query($query);
$num=mysql_numrows($result);
if(
$num 0) {
  echo 
'<table>';
    while (
$r mysql_fetch_assoc($result)) {
        
$coname=$r["coname"];
        
$contactfirst=$r["contactfirs t"];
        
$contactlast=$r["contactlast" ];
        
$address1=$r["address1"];
        
$address2=$r["address2"];
        
$city=$r["city"];
        
$state=$r["state"];
        
$zip=$r["zip"];
        
$phone=$r["phone"];
        
$country=$r["country"];
        
$URL=$r["URL"];
        
$insurance=$r["insurance"];
        
$accreditation=$r["accreditat ion"];
        
$certfitter=$r["certfitter"];

    
            if(!empty(
$city)) { 
                echo 
'<tr>'."/n"
                            
.'<td colSpan="2"><h6>' $city '</h6></td>'."/n"
                        
.'</tr>'."/n";
            }
            if(!empty(
$coname) || !empty($certfitter)) {
                echo 
'<tr>'."/n";
                if(!empty(
$coname)) {
                    echo 
'<td><strong>' $coname '</strong></td>'."/n";
                }
                if(!empty(
$certfitter)) {
                    echo 
'<td>' $certfitter '</td>'."/n";
                }
                echo 
'</tr>'."/n";
            }
            if(!empty(
$contactfirst) || !empty($contactlast) || !empty($insurance)) {
                echo 
'<tr>'."/n";
                if(!empty(
$contactfirst) || !empty($contactlast)) {
                    echo 
'<td>' $contactfirst." ".$contactlast '</td>'."/n";
                }
                if(!empty(
$insurance)) {
                    echo 
'<td>' $insurance '</td>'."/n";
                }
                echo 
'</tr>'."/n";
            }
            if(!empty(
$address1) || !empty($address2) || !empty($accreditation)) {
                echo 
'<tr>'."/n";
                if(!empty(
$address1) || !empty($address2)) {
                    echo 
'<td>' $address1." ".$address2 '</td>'."/n";
                }
                if(!empty(
$accreditation)) {
                echo 
'<td>' $accreditation '</td>'."/n";
                }
                echo 
'</tr>'."/n";
            }
            if(!empty(
$city) || !empty($state) || !empty($zip)) {
                echo 
'<tr>'."/n"
                            
.'<td colSpan="2">' $city.", ".$state." ".$zip '</td>'."/n"
                        
.'</tr>'."/n";
            }
            if(!empty(
$phone)) {
                echo 
'<tr>'."/n"
                            
.'<td colSpan="2">' $phone '</td>'."/n"
                        
.'</tr>';
            }
            if(!empty(
$URL)) {
                echo 
'<tr>'."/n"
                            
.'<td colSpan="2"><a href="http://' $URL '">' $URL '</a></td>'."/n"
                        
.'</tr>'."/n";
            }
            echo 
'<tr>'."/n"
                        
.'<td colSpan="2"></td>'."/n"
                    
.'</tr>'."/n"
                    
.'<tr colspan="2"><td>&nbsp;</td></tr><br>'."/n"

                    
.'</tbody>'."/n";

    }
} else {
    echo 
'No rows returned from database';
}
mysql_free_result($result);
echo 
"</table>";
?>

Last edited by Jcbones; 11-03-09 at 06:49 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Injecting a string into an If Statement ? nova912 PHP 4 07-21-06 03:04 PM
Form Validation - Prevent submit if fields are blank Cepeleon JavaScript 2 03-15-05 05:33 PM
formmail problem gscraper Perl 12 08-27-04 04:06 AM
Remove Duplicate and Blank Fields digitalje5u5 PHP 6 08-06-04 03:29 PM


All times are GMT -5. The time now is 12:50 PM.
vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.