混合PHP和HTML代码[关闭]

I cannot figure this out after looking and googling for hours.

<?php           
foreach ($myResults as $rowNumber => $myResult) {
if ($rowNumber<$numberOfResults/2) {
print "<h2>";
print render($myResult->field_field_item_category[0]['rendered']['#title']) ;       
print "</h2>";
}?>
  html code here

<?php 
 elseif ($rowNumber>$numberOfResults/2) {   
 print "<h2>";
 print render($myResult->field_field_item_category[0]['rendered']['#title']) ;      
 print "</h2>";
}
} ?>

I am assuming you want the html code here block to only display if the first if test is TRUE. If that is the case, move the trailing } before the html block to after.

<?php           
  foreach ($myResults as $rowNumber => $myResult) {
    if ($rowNumber<$numberOfResults/2) {
      print "<h2>";
      print render($myResult->field_field_item_category[0]['rendered']['#title']) ;       
      print "</h2>";
?>
  html code here
<?php 
     }elseif ($rowNumber>$numberOfResults/2) {   
       print "<h2>";
       print render($myResult->field_field_item_category[0]['rendered']['#title']) ;      
       print "</h2>";
     }
  } 
?>

if you want the html code here to go after both move it out of the middle of the if statement.

<?php           
  foreach ($myResults as $rowNumber => $myResult) {
    if ($rowNumber<$numberOfResults/2) {
      print "<h2>";
      print render($myResult->field_field_item_category[0]['rendered']['#title']) ;       
      print "</h2>";
    }elseif ($rowNumber>$numberOfResults/2) {   
      print "<h2>";
      print render($myResult->field_field_item_category[0]['rendered']['#title']) ;      
      print "</h2>";
    }
  } 
?>
  html code here

Also, indent. It makes the source of errors like this more obvious.

You're attempting to echo HTML code between a the end of your IF statement and the start of your ELSEIF

if(something)
{
  // conditional code here
}

// do not put code here

elseif (somethingelse)
{
  // conditional code here
}

The sections between <?php and ?> are interpreted as PHP code. The rest is plain HTML code.

There can not be anythig between if() { } and elseif() { } in PHP (and any language), neither HTML code.

Change elseif to if and it will work.