Jump to content

render address-blocks like a boss


bernhard
 Share

Recommended Posts

i'm sure this is nothing fancy for all the pros here, but at least i did that a lot more inconvenient for a long time and maybe it will help someone else...

$lines = array();
$lines[] = $page->company;
$lines[] = $page->forename . ' ' . $page->surname;
$lines[] = $page->address;
$lines[] = $page->zip . ' ' . $page->city;
$lines[] = $page->country;

echo implode("<br>", array_filter($lines));

// or like this

$lines = array(
    $page->company,
    implode(" ", array_filter(array(
        $page->prefix, $page->forename, $page->surname, $page->suffix
    ))),
    $page->address,
    $page->zip . ' ' . $page->city,
    $page->country
);
echo implode("<br>", array_filter($lines)); 

it will skip all empty lines automatically (for example if there is no "company" or no country) and it's a whole more readable then with lots of IFS and ECHOS :)

  • Like 7
Link to comment
Share on other sites

Nice! Have you considered adding microformats somehow (eg spans)?

i dont need any because i'm rendering it for a PDF; but would be a valid point ;)

can you use this for a form ?

i don't understand your question. what do you mean?

^-^

Probably not relevant in your case, but just an FYI, array_filter considers zero values as empties...Will be a gotcha in use cases where you actually want zeroes :-)

thanks for the hint and link :)

Link to comment
Share on other sites

  • 2 months later...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...