4

I wish to place an image tag within a PHP array, dynamically fed from a database. The array will eventually render directly onto a page.

What is the correct syntax for the image tag within the array; i.e. once the array is rendered on a page, the image tag should show as an image and not as simple text.

Perhaps it might be easier if I show you what I mean:

foreach($rows as &$row)
  {  
   $this->_rows[] = array(
    'thumnail'  => '<img style="width: 180px; height: 80px;" src="<?= THUMBNAILn_Assets::getProductThumbnail($row->id) ?>" />',
   );
  };

The image tag renders as simple text on the page, as opposed to an image.

1
  • <?= and ?> is something quite common for C# Visual Studio back-front end communication but the logic is not required in this specific case because you only need the PHP opening and closing syntax around PHP code, not around specific PHP methods/variable that are already inside of a legit PHP code. src="$foo" Would be just as legit if you would say $foo = THUMBNAILn_Assets::getProductThumbnail($row->id) somewhere above, no need for opening and closing tags. Commented Jul 24, 2013 at 13:13

2 Answers 2

2

Try this :

foreach($rows as &$row)
  {  
   $this->_rows[] = array(
    'thumnail'  => '<img style="width: 180px; height: 80px;" src="'. THUMBNAILn_Assets::getProductThumbnail($row->id) .'" />',
   );
  }

No need of php tags (<?= and ?>) again inside the src attribute

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

Comments

0
  foreach($rows as &$row)
  {  
     $t_pic['thumnail'] = THUMBNAILn_Assets::getProductThumbnail($row->id);

     $this->_rows[] = $t_pic;
  }

or

  $t_pic['thumnail'] = '<img style="width: 180px; height: 80px;" src="'. THUMBNAILn_Assets::getProductThumbnail($row->id) .'" />';

just make it more clear and simple, its not good practice to store the html tags in php variables

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.