iseriouslyneedhelp Posted August 6, 2011 Share Posted August 6, 2011 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(); ?> Quote Link to comment Share on other sites More sharing options...
phpSensei Posted August 6, 2011 Share Posted August 6, 2011 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(); ?> Quote Link to comment Share on other sites More sharing options...
iseriouslyneedhelp Posted August 6, 2011 Author Share Posted August 6, 2011 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? Quote Link to comment Share on other sites More sharing options...
phpSensei Posted August 6, 2011 Share Posted August 6, 2011 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(); ?> Quote Link to comment Share on other sites More sharing options...
radiations3 Posted August 6, 2011 Share Posted August 6, 2011 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 ??? Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 6, 2011 Share Posted August 6, 2011 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(); ?> Quote Link to comment Share on other sites More sharing options...
iseriouslyneedhelp Posted August 6, 2011 Author Share Posted August 6, 2011 Awesome! Thanks for the education!! Quote Link to comment Share on other sites More sharing options...
phpSensei Posted August 6, 2011 Share Posted August 6, 2011 Np, mjdamato's formatting is far better, so I advice you go with that one. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.