RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.20% (5.24%*) • Home Loan Fixed: 5.48% (6.24%*) • Fixed: 5.48% (6.24%*) • Variable: 5.20% (5.24%*) • Investment IO: 5.78% (6.81%*) • Investment PI: 5.49% (6.32%*)

Footer or Menu Link Unit from a PHP Array

This post will show you how to build a string of links from an array using PHP.

In our Internoetics downtime, we would have put together about 70 or so of our own (mostly crappy) websites. For virtually all of the sites that we built, we had a simple configuration file that contained global variables meaning that we could quickly and easily effect site changes. That included using user-defined arrays to generate header and footer menus.

The Output

Here's a snapshot from one of the site that utilises similar code. The footer menu is in the bottom right of the image. Simple, right?

The Code

1
<?php 
2
/*
3
 Create a Footer or Menu Link Unit from a PHP Array
4
 http://www.beliefmedia.com/code/php-snippets/array-menu
5
*/
6
 
7
function beliefmedia_footer_links($sep = '  |  ') {
8
 
9
 $footerLinks = array(
10
    'http://www.martinkhoury.com/' => 'Home',
11
    'http://www.internoetics.com/' => 'Internoetics',
12
    'http://www.flight.org' => 'Flight',
13
    'http://www.beliefmedia.com/' => 'BeliefMedia'
14
 );
15
 
16
 $count = count($footerLinks); $i = 1;
17
  foreach($footerLinks as $link => $linkname) {
18
    $links .= '<a href="' . $link . '">' . $linkname . '</a>';
19
    if ($i != $count) $links .= $sep;
20
   $i++;
21
  }
22
 return $links;
23
}

Usage

Use the function as follows:

echo beliefmedia_footer_links();

The HTML that's generated would output as follows:

Home | Internoetics | Flight | BeliefMedia

We use a pipe separator by default, but if you wanted to change that to say, double colons, simply use the function as follows:

echo beliefmedia_footer_links($sep = ' :: ');

This time, the generate HTML would output as follows:

Home :: Internoetics :: Flight :: BeliefMedia

Keeping the Array in a Functions File

If you sensibly include your functions in its own file and include the array in a configuration file, remember to add global $footerLinks; so the array is available inside the function.

Download our 650-page guide on Finance Marketing. We'll show you exactly how we generate Billions in volume for our clients.

  E. Australia Standard Time [ UTC+10, Default ] [ CHECK TO CHANGE ]

  Want to have a chat?
 

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment