-2

I am having trouble echoing out heredoc statements from one php file into another. I have one script which is used to retrieve API database information and then format that information into a heredoc to echo the information into the index.php page. The code that I have is:

while($artist_info = $artist_details_resource->fetch_assoc()){
   $artist = <<<DOC
             <img src="{$artist_info['image_url']}" alt="$artist_info['artist_name']" />
             <p>{$artist_name}</p>
DOC;
}

And in the index.php script I start a php clause on the place I want this heredoc to print. The code for that is:

<?php
  if($artist){
     echo $artist;
  }
?>

However this only prints the last heredoc string from the while loop, and not echoing out every heredoc through each iteration.

Pankit Kapadia
  • 1,579
  • 13
  • 25
Human
  • 545
  • 2
  • 7
  • 22

2 Answers2

2

Why would it? You aren't echoing it in a loop, nor you are concatenating the string. You're overwriting the string on every iteration.

while($artist_info = $artist_details_resource->fetch_assoc()){
   $artist .= <<<DOC
             <img src="{$artist_info['image_url']}" alt="$artist_info['artist_name']" />
             <p>{$artist_name}</p>
}

Note the .=

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
1

Of course this only prints the last string, because you use $artist = <<<DOC so you overwrite value of the $artist variable in every loop.

Try $artist .= <<<DOC or put it into array: $artists[] = <<<DOC

rzymek
  • 866
  • 7
  • 20