rondog Posted September 20, 2010 Share Posted September 20, 2010 I have about 7 fields in one of my tables. Some of the fields are null or blank since they arent required (like address2) Is their a better way of formatting an address rather than doing something like: <?php while ($row = mysql_fetch_array($query)) { if ($row['address2'] != "") { echo $row['address2'] . "<br/"; } if ($row['otherField'] != "") { echo $row['otherField'] . "<br/"; } if ($row['anotherField'] != "") { echo $row['anotherField'] . "<br/"; } } ?> I have more fields than just address2, I just don't want a bunch of if statements Quote Link to comment https://forums.phpfreaks.com/topic/213847-displaying-address-from-db/ Share on other sites More sharing options...
PFMaBiSmAd Posted September 20, 2010 Share Posted September 20, 2010 Since the data is an array, just loop over the array - foreach($row as $value){ if($value != ''){ echo $value . '<br />'; } } If you don't want to do this for each element of the $row array or they are not in the order that you want, create an array of the index names in the order that you want and use the foreach() loop on this array of index names to let you iterate over the corresponding values in the $row array. Edit: The second method would look like - $index = array('address2','otherField','anotherField'); foreach($index as $key){ if($row[$key] != ''){ echo $row[$key] . '<br />'; } } Quote Link to comment https://forums.phpfreaks.com/topic/213847-displaying-address-from-db/#findComment-1112999 Share on other sites More sharing options...
fortnox007 Posted September 20, 2010 Share Posted September 20, 2010 I am not sure if it's easier, but what if you just show all fields to the viewer and if their value is empty just leave it empty. (this might even stimulate them to fill it in) Like this with a heredoc: <?php $first_name = 'John'; $last_name = 'Doe'; //etc echo <<<AWESOME <ul> <li>First name: $first_name</li> <li>Last name: $last_name</li> <li>Address: $etc</li> <li>Zip: $etc</li> <li>City: $etc</li> <li>Company address: $etc</li> <li>Zip: $etc</li> <li>City: $etc</li> </ul> AWESOME;// make sure this last 'AWESOME' one is at the complete beginning of the line. A space in front will fack it up. ?> -edit and the Loop as mentioned above here is a smart idea so in your for each loop you could use <li> $var </li> Quote Link to comment https://forums.phpfreaks.com/topic/213847-displaying-address-from-db/#findComment-1113000 Share on other sites More sharing options...
rondog Posted September 20, 2010 Author Share Posted September 20, 2010 Thanks guys, I went with PFMaBiSmAd second method. The first one was displaying things like ID which I didn't really need displayed anyway. Fortnox, I am not worried about the users filling or not filling the fields so no need for that, thanks though! Quote Link to comment https://forums.phpfreaks.com/topic/213847-displaying-address-from-db/#findComment-1113028 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.