0

I think I'm picking this up pretty well, but I'm just stuck on this. I want to display this menu using an array and foreach loop:

<a href="/design/payments"><img src="/img/page-icons/credit-card21.png" />Payments</a>
<a target="_blank" href="/cloud"><img src="/img/page-icons/upload123.png" />Cloud</a>
<a target="_blank" href="/portal"><img src="/img/page-icons/earth208.png" />Portal</a>

So to do that I need to turn that into this line in PHP:

echo '<a href="' . $link . '" target="' . $target . '"><img src="/img/page-icons/' . $image . '" />' . $title . '</a>';

To fill that out in the loop we need to create something like this... which is where I'm stuck:

foreach( $stamp as $link => $target ){
    echo '<a href="/' . $link . '" target="' . $target . '">';

    foreach( $stamp[] as $title => $image ){
        echo '<img src="/img/page-icons/' . $image . '" />' . $title;
    }

    echo '</a>';
}

I don't really know how to go about the above, just been messing around with it for a while today. I also don't want to always display target="' . $target . '" on every link.

The array would probably be a two dimensional array? Something like this any way:

$stamp = array(
    'link' => array('title' => 'image'),
    'link' => array('title' => 'image'),
    'link' => array('title' => 'image')
);

EDIT:

There's some confusion of what 'target' is, I want to echo 4 values from an array into a link, target is one of the values. I didn't know how to use that in the array so I just left it out.

1
  • waht is target here? Commented May 5, 2015 at 20:25

2 Answers 2

2

When you do:

foreach( $stamp as $link => $target )

The $link variable contains the string "link" and the $target variable is an array such as ['title' => 'image'].

What you should probably do is have an array like this:

// Init links
$links = array();

// Add links
$links[] = array(
   'title' => 'My Title',
   'href' => 'http://www.google.com',
   'target' => '_blank',
   'image' => 'image.png',
);

foreach ($links as $link)
{
   echo '<a href="'.$link['href'].'" target="'.$link['target'].'">';
       echo '<img src="/img/page-icons/' . $link['image'] . '" />';
       echo $link['title']; 
   echo '</a>';
}

This is a bit more flexible approach that lets you add other data items to the structure in the future. That $links array could easily be generated in a loop if you have a different data source such as a database as well.


EDIT

To answer your further question, you can prefix the link building with a set of sane defaults like this:

foreach ($links as $link)
{
   // Use the ternary operator to specify a default if empty
   $href = empty($link['href']) ? '#' : $link['href'];
   $target = empty($link['target']) ? '_self' : $link['target'];
   $image = empty($link['image']) ? 'no-icon.png' : $link['image'];
   $title = empty($link['title']) ? 'Untitled' : $link['title'];

   // Write link
   echo "<a href='$href' target='$target'>";
       echo "<img src='/img/page-icons/$image' />";
       echo $title; 
   echo "</a>";
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! Now with what you've provided, how would I see if target exists in the array and if it does then echo target if not then echo the link without the target=""? In my mind I see it being done through an if statement like if (array_key_exists('target', $links)) {}?
I updated my answer with some additional help. If my answer has helped you, can you please accept it?
Is there a way to check if target is actually in the array though?
0

you can set your array like:

$stamp = array(
 '0' => array('title'=>$title 'image'=>$image,'link'=>$link,'target'=>$target),
 '1' => array('title'=>$title, 'image'=>$image,'link'=>$link,,'target'=>$target),
 '2' => array('title'=>$title 'image'=>$image,'link'=>$link,'target'=>$target)
);

and in foreach you can write

$i=0;
foreach( $stamp as $st=> $target ){
echo '<a href="/' . $st['link'] . '" target="' . $st['target'] . '">';


    echo '<img src="/img/page-icons/' . $st['image'] . '" />' . $st['title'];


echo '</a>';

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.