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 PHP Basics (Retired) PHP Conditionals & Loops Foreach Loops

adrien kovanic
adrien kovanic
5,727 Points

hi, I wonder ; in those icones (facebook, twitter.) where are the external links to the sites?

because in the foreeach loops we displayed the icones according to our CSS, but I don't see the actual linking to http://facebook.com etc. is there in it or how to do that? thanks a lot :)

3 Answers

Hmm, I think this is just an example of using a foreach loop so doesn't go as far as creating a full link.

You might use a multidimensional array and hold more information about each icon:


$socialMediaLinks[] = array(
    'imageSrc'  => 'some-icon.png',
    'href'      => 'www.link.co.uk'

// Then loop around the social media links array and pull out data from the array inside.

foreach ($socialMediaLinks as $link) { ?>
    <li><a href="<?php echo $link['href'] ?> "><img src="<?php echo $link['imageSrc'] ?>"></a></li>
} ?>
Frank D
Frank D
14,042 Points

That's a much easier approach to the foreach() loop, for a beginner. It helped a lot! Thank you Tom.

Clint Broadhead
Clint Broadhead
8,218 Points
//This is a way that you can expand upon the video and add links using the same methods, with one little addition. You can create an associative array with all of the contents required within each pair like so

/* Here is the array from the video
 * $social_icons = array(
 *      'twitter',
 *      'facebook',
 *      'google'
 * );
 * This is not an associative array like we have already learned about if you're following the track.

//Here is the same array, but associative with links

    $social_icons = array(
        'twitter' => 'http://www.linktotwitt.com',
        'facebook' => 'http://www.linktoface.com',
        'google' => 'http://www.linktogoog.com'
//now loop through the array just like in the video, but since it is associative look for the little addition
<ul class="social">

        foreach($social_icons as $icon => $link){ // <<< Did you see the little addition in this line?

            echo '<li><a href="' . $link . '"><span class="icon ' . $icon . '"></span></a></li>';


Loop 1:  <li><a href="http://linktotwitt.com"><span class="icon twitter"></span></a></li>
Loop 2: <li><a href="http://linktoface.com"><span class="icon facebook"></span></a></li>
Loop 3: <li><a href="http://linktogoog.com"><span class="icon google"></span></a></li>


Hi Clint, thanks for your useful code.