Sunday 21 August 2016

php - unexpected T_ELSEIF




$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


Answer



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.




// 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);

?>

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...