Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.