Don't Display Empty Database Fields

liunx

Guest
Greetings. I have returned with yet another maybe-not-so-odd question. ...<br /><br />I have a working database table with 20+ fields in it. Due to the nature of the information submitted only about half of these will have content in any given unique row. I can connect and display the entire contents but as you may imagine it looks pretty ridiculous with half the fields missing.<br /><br />I'd like to be able to display ONLY fields that have something in them, not empty ones (geez - I am so far over my head). I've tried adding conditions and I am not doing it right. Here's a snippet of what does work (only a few fields though) :<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1--><?<br />$dbuser  = ""; <br />$dbserver    = "localhost"; <br />$dbpass  = ""; <br />$dbname  = ""; <br /><br />mysql_connect($dbserver, $dbuser, $dbpass)<br />or die ("UNABLE TO CONNECT TO DATABASE");<br />mysql_select_db($dbname)<br />or die ("UNABLE TO SELECT DATABASE");<br /><br />$sql = "SELECT * FROM mydata";<br />$result = mysql_query($sql);<br />if ($myrow = mysql_fetch_array($result)) {<br />do<br />{<br />$first_name=$myrow["first_name"];<br />$last_name=$myrow["last_name"];<br />$phone=$myrow["phone"];<br />$email=$myrow["email"];<br />$website=$myrow["website"];<br />$comments=$myrow["comments"];<br /><br />echo "<BR>Name:  $first_name"; <br />echo "  $last_name"; <br />echo "<BR>Phone:  $phone"; <br />echo "<BR>Email:  $email"; <br />echo "<BR>Website:  $website"; <br />echo "<BR>Comments:  $comments"; <br />echo "<BR><BR>";<br /><br />}<br />while ($myrow = mysql_fetch_array($result));<br />}<br />?><!--c2--></div><!--ec2--><br /><br />Any suggestions? I've been looking and looking, reading and reading and I'm just not getting it. Perhaps I need to redo the whole thing as maybe this is a bad way to even start to do this. <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/wallbash.gif" style="vertical-align:middle" emoid=":wallbash:" border="0" alt="wallbash.gif" /> <br /><br />Any guidance is always greatly appreciated. <br />Thank you.<!--content-->
While I am not fully clear on what you are trying to accomplish, I did find this <a href="http://www.freewebmasterhelp.com/tutorials/phpmysql/4" target="_blank">Page</a> to be similar to what your doing. It may help you.<!--content-->
I don't know how you'd do it in PHP, but in Perl you would do this:<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->if(length($phone)>0) {<br />   print "<BR>Phone:  $phone"; <br />}<!--c2--></div><!--ec2--><br /><br />Hope that helps!<br /><br />Bill<!--content-->
I think it would look something like this.<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->if ($phone != "") {<br />     echo "<BR>Phone:  $phone"; <br />}<!--c2--></div><!--ec2--><!--content-->
Thank you, I will try. At the moment my site is not responding but I'll let you know.<!--content-->
Thanks Bruce! That did it and I could/should kick myself.<br /><br />I had it before - forgot the ( ) grrrrrr<br /><br />One last thing - are email addresses susceptible to bots when displayed like this?<br /><br />Thanks again.<!--content-->
Ty if the e-mail can be seen when you view the source code of the finished page,<br />then spam bots will be able to see them.<!--content-->
Unless your pages are password protected and/or you use forms instead of displaying email address, the bots can almost certainly pick up the addresses. It's difficult if not impossible to display an email address to your visitors in such a way that a bot can't pick it up. But you can make life more difficult--munging the address can help:<br /><br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->if ($email != "") {<br /><br />$email = trim($email);<br />$email = preg_replace("/@/", " [at ] ", $email);<br />$email = preg_replace("/\./", " [dot] ", $email);<br /><br />echo "<br>Email: $email";<br />}<!--c2--></div><!--ec2--><br /><br /><br />I haven't tested the code, but in theory it should work. Well, if I got the regular expressions right. This should output:<br /><br />Email: username [at] domain [dot] ext <br /><br />Alternatively, you can use hex encode to "hide" the address from bots while displaying it to visitors -- see PHP's bin2hex function (<!-- m --><a class="postlink" href="http://uk2.php.net/bin2hex">http://uk2.php.net/bin2hex</a><!-- m -->). But bots can figure that out as well as (better than?) munging. Personally I use munging, with non-standard phrases (i.e. not [at] or [dot], both of which are easy to guess and look for from the spammer's perspective). Neither method is fool-proof, but either is better than nothing, you know?<!--content-->
I use javascript ... and that seems to work well:<br /><br />$email = 'me'+'@'+'mydomainname'+'.'+'c'+'om';<br />document.write($email);<br /><br />You can even hex code the whole thing too ... but you generally don't have to go that far.<!--content-->
Thank you all. Can you tell I'm a database noob? <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/tongue.gif" style="vertical-align:middle" emoid=":p" border="0" alt="tongue.gif" /><br /><br />Yes Don, I should have realised that - thanks for the reminder.<br /><br />owatagal, HC - I will try the munging as I'm not sure I could get php + javascript to play nice together.<br /><br />I really appreciate all the help. <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/yes.gif" style="vertical-align:middle" emoid=":yes:" border="0" alt="yes.gif" /><!--content-->
 
Top