0

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.

2
  • 2
    Learn about database normalization Commented May 17, 2013 at 7:27
  • 2
    @Mr.Alien There are some cases whereby comma separated values are okay. Commented May 17, 2013 at 7:28

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

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="" />
@AzzerB Ah, used the wrong variable for the image source :) updated.
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
@AzzerB Have you debugged the contents of both variables?
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.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.