Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I am currently using two fields from a database, The fields have words which are seperated by comma's (,).

Here is my code

    $tags = explode(",",$tags);
$tagsdesc = explode(",",$tagsdesc);

foreach($tags as &$key) 
{
    foreach($tagsdesc as &$value) 
    {
        echo "<img src='images/".$key."' width='20' height='20' title='".$value."'></img> ";
    }
}

The two values, $tags and $tagsdesc are fed into a function. I am having difficulty spitting out the html where the $key displays but not the $value for the image title, thank you.

share|improve this question
2  
Learn about database normalization – Mr. Alien May 17 '13 at 7:27
2  
@Mr.Alien There are some cases whereby comma separated values are okay. – Ja͢ck May 17 '13 at 7:28

If the values should go together you need this:

foreach($tags as $key => $tag) {
    $desc = $tagsdesc[$key];
    echo sprintf('<img src="images/%s" width="20" height="20" title="%s" />',
        urlencode($tag),
        htmlspecialchars($desc, ENT_QUOTES, 'UTF-8')
    );
}

Also, learn about HTML escaping; not doing this properly can lead to XSS.

share|improve this answer
    
Hi @Jack , Thank you for your reply, I seem to be getting an error, when inspected the HTML now looks like this <img src="images/0" width="20" height="20" title="" /> – Alex May 17 '13 at 7:45
    
@AzzerB Ah, used the wrong variable for the image source :) updated. – Ja͢ck May 17 '13 at 7:54
    
Hi @Jack , I am sorry to be such a pain. The revised code now shows the image but unfortunately I still don't get the title text through – Alex May 17 '13 at 8:14
    
@AzzerB Have you debugged the contents of both variables? – Ja͢ck May 17 '13 at 8:23
    
Hi @Jack, Many apologies as there was an error on my end! The code is working but the Title is now showing just one letter when I hover on the image. – Alex May 17 '13 at 9:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.