Jump to content

Displaying address' from DB


rondog

Recommended Posts

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

Link to comment
Share on other sites

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 />';
}
}

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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!

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.