0

I am trying to add a div onto the output of each loop. Without the div the code runs.

  foreach($arr as $data){
    echo "<div class=\"rcorners\"> $data['bookID'], $data['saleId']</div>";
  }

gives

Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)

GMB
  • 337
  • 2
  • 6
  • 21
user2892730
  • 31
  • 1
  • 7

2 Answers2

2

Just use . to concat your string.

foreach($arr as $data){
   echo "<div class='rcorners'> " . $data['bookID'] . ",". $data['saleId'] ."</div>";
}

Or even

echo "<div class=\"rcorners\"> " . $data['bookID'] . ",". $data['saleId'] ."</div>";

You can't put Array variables inside a string.

FedeCaceres
  • 158
  • 1
  • 11
0

It's the arrays killing it. Try this:

foreach($arr as $data){
echo "<div class='rcorners']> {$data['bookID']}, {$data['saleId']}</div>";
}

The curly brackets within the string tell the parser where the variable starts and stops. Without them it doesn't know if you want a $data or $data['bookID'].

Sarah K
  • 363
  • 2
  • 15