Vonder
Vonder

Reputation: 4061

How to add html to an array in php?

I am trying to add some html code before each tag printed as an array element.  
My code:

  $term_links = array();

  foreach ($vars['node']->taxonomy as $term) 
  {
    $term_links[] = l($term->name, 'taxonomy/term/' . $term->tid,
      array(
        'attributes' => array(
          'title' => $term->description
    )));
  }

  $vars['node_terms'] = implode(', ', $term_links);

At the moment the tags are printed seperated by a comma. I would like to add a small image before each tag element using img src="tag.png" How can I do this?

EDIT - My current Code, still not working.

 if (module_exists('taxonomy')) {

$img  = 'some html';
$text = $img . $term->name;
$path = 'taxonomy/term/' . $term->tid;


$term_links = array();
foreach ($vars['node']->taxonomy as $term) {



  $term_links[] = l($text, $path, array(
    'html' => TRUE,
      'attributes' => array(
        'title' => $term->description
    )));
 }
   $vars['node_terms'] = implode(', ', $term_links);
 }
}

Upvotes: 1

Views: 3036

Answers (1)

ioseb
ioseb

Reputation: 16951

Dupal's l() function has an option "html" you can set it to TRUE and use IMG + TITLE as a title.

Here is the example:

$img  = '<img src="..." />';
$text = $img . $term->name;
$path = 'taxonomy/term/' . $term->tid;

$term_links[] = l($text, $path, array(
  'html'       => TRUE,
  'attributes' => array(
    'title' => $term->description
  )
));

Upvotes: 2

Related Questions