0

I just want to store HTML data in a PHP variable. But the problem is that the data also contains a PHP function

 $CartItem = "<table style='width:100%'>
<tr>
<th style='text-align:left;'><strong>Code</strong></th>
<th style='text-align:left;'><strong>ItemName</strong></th>
<th style='text-align:right;'><strong>Quantity</strong></th>
</tr>". foreach ($_SESSION["cart_item"] as $item){ .
 "
<tr>
<td style='text-align:left;border-bottom:#F0F0F0 1px solid;'>" .echo $item["ItemCode"]; ."</td>
<td style='text-align:left;border-bottom:#F0F0F0 1px solid;'><strong>" .echo $item["ItemName"];."</strong></td>
<td style='text-align:right;border-bottom:#F0F0F0 1px solid;'>". echo $item["quantity"];."</td>
</tr>".
} ."


 </table>"; 

I just want to create HTML code using a PHP foreach loop and store them in $CartItem.

Please help me with correct code.

My code has an error

( ! ) Parse error: syntax error, unexpected 'foreach' (T_FOREACH) in C:\wamp\www\drupal-7.38\shoppingcart\initiateOrder\index.php on line 70

Kirby
  • 15,127
  • 10
  • 89
  • 104
Sanjog Mittal
  • 118
  • 2
  • 14

3 Answers3

2

Don't concatenate your foreach to the string;

$CartItem = "<table style='width:100%'>
<tr>
<th style='text-align:left;'><strong>Code</strong></th>
<th style='text-align:left;'><strong>ItemName</strong></th>
<th style='text-align:right;'><strong>Quantity</strong></th>
</tr>";
foreach ($_SESSION["cart_item"] as $item){
  $CartItem .= "
  <tr>
  <td style='text-align:left;border-bottom:#F0F0F0 1px solid;'>" . $item["ItemCode"] ."</td>
  <td style='text-align:left;border-bottom:#F0F0F0 1px solid;'><strong>" . $item["ItemName"] ."</strong></td>
  <td style='text-align:right;border-bottom:#F0F0F0 1px solid;'>".  $item["quantity"] ."</td>
  </tr>";
}

$CartItem .= "</table>";
brombeer
  • 8,716
  • 5
  • 21
  • 27
2

This is correct way to concatenate:

<?php
$CartItem = "<table style='width:100%'>
<tr>
<th style='text-align:left;'><strong>Code</strong></th>
<th style='text-align:left;'><strong>ItemName</strong></th>
<th style='text-align:right;'><strong>Quantity</strong></th>
</tr>"; 

foreach ($_SESSION["cart_item"] as $item) {

$CartItem .= "
<tr>
<td style='text-align:left;border-bottom:#F0F0F0 1px solid;'>" . $item["ItemCode"] ."</td>
<td style='text-align:left;border-bottom:#F0F0F0 1px solid;'><strong>" .  $item["ItemName"] . "</strong></td>
<td style='text-align:right;border-bottom:#F0F0F0 1px solid;'>" . $item["quantity"] . "</td></tr>";

} 

$CartItem .= "</table>";

echo $CartItem;
?>
AnTrakS
  • 733
  • 4
  • 18
1

You have misused of concatenation. Don't concatenate foreach and echo

$CartItem = "<table style='width:100%'>
<tr>
<th style='text-align:left;'><strong>Code</strong></th>
<th style='text-align:left;'><strong>ItemName</strong></th>
<th style='text-align:right;'><strong>Quantity</strong></th>
</tr>";
foreach ($_SESSION["cart_item"] as $item){ 

   $CartItem .= "
   <tr>
<td style='text-align:left;border-bottom:#F0F0F0 1px solid;'>" . 
$item["ItemCode"] ."</td>
<td style='text-align:left;border-bottom:#F0F0F0 1px solid;'><strong>" .
$item["ItemName"]."</strong></td>
<td style='text-align:right;border-bottom:#F0F0F0 1px solid;'>".  
$item["quantity"]."</td>
</tr>";
}


    $CartItem .="</table>"; 
Zain Farooq
  • 2,956
  • 3
  • 20
  • 42