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 trialJohn Hirleman
11,378 PointsHow to add the href for each icon?
I'm sure other people wondered how to do it, so here's how I did it.
- 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'
);
?>
- 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>
1 Answer
John Hirleman
11,378 Points...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?
Jeff Lemay
14,268 PointsJeff Lemay
14,268 PointsGood stuff, John! Thanks for sharing the example!