The right way to place an image tag inside a php array loop

I want to place an image tag inside a PHP array dynamically loaded from a database. The array will ultimately be displayed directly on the page.

What is the correct syntax for an image tag inside an array; that is, as soon as the array is displayed on the page, the image tag should be displayed as an image, and not as plain text.

Perhaps it would 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 displays plain text on the page, not the image.

+4
source share
2 answers

Try the following:

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

No need for php tags ( <?= And ?> ) Again inside src attribute

+2
source
  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 understandable and simple, its not a good practice to store html tags in php variables

0
source

Source: https://habr.com/ru/post/1493194/


All Articles