-1

I’m going mad over this – I can’t find the reason for the "unexpected {"-error being thrown right after the last "else" (7th line from below). Does anyone see something that I don’t?

<?php
  $i = 0;
  foreach($bgs as $bg) { ?>
  <?php $i++; ?>
      <div class="item <?php if($i == '1') echo "active"; ?> img-responsive" style="background-image: url('/new/images/<?=$bg['b_url']?>')" >
        <div class="metabox">
          <?php if(($bg['b_weight']) != '1000') { ?>
            <h1><?=$bg['w_titel']?></h1>
            <p><?=$bg['w_info']?> // <?=$bg['w_jahr']?> // <?=$bg['w_ort']?><?=$bg['w_function']?></p>
              <?php if (isset($_GET['w']) && (is_numeric($_GET['w']))) { ?>
                  <?=$bg['w_desc']?>
               <?php } else { ?>
                <p><a href="index.php?w=<?=$bg['werkid']?>&t=<?=$bg['urlslug']?>" target="_self">More</a></p>
                <?php } ?>
          <?php } else { ?>
            <h1><?=$bg['w_titel']?></h1>
            <p><?=$bg['w_info']?></p>
          <?php } ?>
        </div>
      </div>
  <?php } ?>
  • 1
    The error is not with the provided code https://3v4l.org/svKbY – user3783243 Dec 19 '18 at 20:10
  • Thank you! Though the problem had to do with the pasted code, you helped me anyway: Right before said last "else" was an invisible item – when I deleted the space between the preceeding "}" and "else", and inserted a new blank space, the code parsed fine. Thank you for pushing me into the right direction with your link to eval! – Martinvie Dec 19 '18 at 20:18

3 Answers3

0

This is very hard to read, you have my sympathies. Have you thought about using an alternative syntax for if

try using

<?php if (condition) :?> <?php do something; ?>

<?php else: do something; ?>

<?php endif; ?>

user4922
  • 101
  • 2
0

Everything looks right. Try to use following syntax it will be more readable.

foreach($bgs as $bg) :
 $i++; 
 if(1 != '1000') :
  echo 3;

  if ( 1 != 2) :
   echo 4;
  else:
   echo 1;
  endif;

 else:  
  echo 2; 
 endif;

endforeach;

Also try to replace:

<?php if($i == '1') echo "active"; ?>

to:

<?= $i == '1' ? "active" : ''; ?>
user3783243
  • 5,368
  • 5
  • 22
  • 41
Maxim
  • 2,233
  • 6
  • 16
0

This was odd: Right before said last else statement was an invisible item (have no idea what it could have been?) – when I deleted the space between the preceding } and else, and inserted a new blank space, the code parsed fine.