Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

John Hirleman
John Hirleman
11,378 Points

How to add the href for each icon?

I'm sure other people wondered how to do it, so here's how I did it.

  1. Turn the $social_icons array into an associative array, like so...
<?php

$social_icons = array(
  'twitter' => 'http://www.twitter.com', 
  'facebook' => 'http://www.facebook.com', 
  'google' => 'http://www.google.com'
);

?>
  1. Then down in your foreach() loop change argument to be set as the $key and $value of the $social_icons array.
<ul class="social">
        <?php
          foreach($social_icons as $key => $value){
        ?>
          <li><a href="<?php echo $value ?>"><span class="icon <?php echo $key ?>"></span></a></li>
        <?php
          }
        ?>
      </ul>
Jeff Lemay
Jeff Lemay
14,268 Points

Good stuff, John! Thanks for sharing the example!

1 Answer

John Hirleman
John Hirleman
11,378 Points

Found this: https://teamtreehouse.com/community/how-can-i-also-loop-through-the-href-for-different-social-links-in-each-list

...and other's have found a bunch of different ways to do it. I was just wondering if the way I did it was any easier? Seems you can have either a more complex array, or a more complex code block for the loop. Are there any best practice approaches to this?