0
$pages = array("grac", "zamknij", "dolaczyc");
$pagesid = array("showNews", "showThread", "showProfile");

foreach ($pagesid as $page) {
  if (isset($_GET[$page])) {
  include('sobra/'.$page.'.php');
  }
}

// just pages
elseif (in_array($_GET['page'], $pages)) {
include("$_GET[page].php");
}

// error
else include('error.php');

gives:
Parse error: syntax error, unexpected T_ELSEIF in C:\WAMP\www\sdgag\index.php on line 33

This should work i think.. what the problem can be?

Thanks

4 Answers 4

8

The elseif and the else aren't attached to the if, you've put them outside of the foreach loop block.

4

Perhaps another approach. Do your logic, and determine what page you want to include ultimately. After all of the logic has been done, include your determined page.

The following is untested, and may contain errors. Let me know, and I'll update the code.

<?php

  // Predefined list of acceptable pages
  $pages = array("one","two","three");
  $pagesid = array("four","five","six");

  // Gather any user-defined page request
  $desPage = trim($_GET["page"]);

  // Assume they are wrong, and need to see error.php
  $pageToLoad = "error.php";

  // If the user request is not empty
  if (!empty($desPage)) {
    if (in_array($desPage,$pages)) {
      $pageToLoad = $desPage . ".php";
    }
  } else {
  // User request is empty, check other variables
    foreach ($pagesid as $pageid) {
      if (isset($_GET[$pageid])) {
        $pageToLoad = $pageid . ".php";
      }
    }
  }

  // Show output page
  include($pageToLoad);

?>
3

There is one closing bracket to much before the else.

It should read:

$pages = array("grac", "zamknij", "dolaczyc");
$pagesid = array("showNews", "showThread", "showProfile");

foreach ($pagesid as $page) {
  if (isset($_GET[$page])) {
    include('sobra/'.$page.'.php');
  }
  // just pages
  else if (in_array($_GET['page'], $pages)) {
    include("$_GET[page].php");
  }
  // error
  else include('error.php');
}   

If you indent your source code correctly such errors show up fast and you can fix them yourself.

5
  • Your "else" is not connected to the conditional statements. It's trailing your foreach().
    – Sampson
    Commented Feb 19, 2009 at 20:35
  • that last else is still out of the if clause
    – Jayrox
    Commented Feb 19, 2009 at 20:36
  • Oah, sh** your right, fixed it
    – theomega
    Commented Feb 19, 2009 at 20:39
  • that outputs everything in error.php 3 times :/
    – duderklumpero
    Commented Feb 19, 2009 at 20:43
  • Because that's what your code is currently written to do. If that's not what you're expecting, you should probably re-think your approach.
    – Chad Birch
    Commented Feb 19, 2009 at 20:47
-2
$pages = array('grac', 'zamknij', 'dolaczyc');
$pagesid = array('showNews', 'showThread', 'showProfile');

foreach ($pagesid as $page) {
  if (isset($_GET[$page])) {
    include('sobra/'.$page.'.php');
  }
  // just pages
  else if (in_array($_GET['page'], $pages)) {
    include($_GET[$page].'.php'); // fixed missing $, restylized to match previous style
  }
  else include('error.php');
}