-1
<?php if (isset($_GET['p'])) { $variable = $_GET['p']; ?>
<?php if ($variable == '1'): ?>
<?php include('pages/home.php');?>
<?php endif;?>
<?php };?>  
<?php if (isset($_GET['p'])) { $variable = $_GET['p']; ?>
<?php if ($variable == '2'): 
       include('pages/about.php'); 
else: include('pages/home.php'); endif; };?>

The above is what I have used to try and fix it, but if I don't put it, errors show up. For example if I used "ifelse", and it tells me to change it to else, or endif. But when I use endif, it tells me to use ifelse. I'm trying to make it so http://localhost/?p=PAGE_ID just simply shows the page using an include(FILE_PATH) statement.

Does anyone know where I went wrong? Or how I can fix it :)

Jay S.
  • 9
  • 2
  • 5
    Dont use a `` on every line and it is just possible you will be able to read and debug your code – RiggsFolly Oct 13 '22 at 19:37
  • 5
    Also you dont need a `;` after a closing `}` of an IF like you used here `};` – RiggsFolly Oct 13 '22 at 19:41
  • 3
    I'd also argue...stick to one syntax style. If you're going to use `if { }`, use it consistently, don't switch randomly between that and the `if: endif` style - again it makes the code harder to read and understand. – ADyson Oct 13 '22 at 19:44

2 Answers2

3

You seem to have thoroughly tied yourself in knots. To my mind, this is a much cleanrer and more flexible and maintainable way to approach the whole thing:

$pages = [
    "1" => "pages/home.php",
    "2" => "pages/about.php"
];

if (isset($_GET['p'])) {
    if (isset($pages[$_GET['p']]))
    {
        $page = $pages[$_GET['p']];
    }
    else
    {
        $page = $pages["1"];
    }
}
else
{
  $page = $pages["1"];
}

include($page);

This way, you have a list of pages, and the system simply looks up the required page in the array by its index number (as passed in via the GET parameter). If it can't find that index, it just uses the default page.

You can now easily add pages by just adding items to the array, without needing to write more if/else logic. You could even store the array data in a database instead of it being hard-coded.

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • For some reason that gives me the same result as before, a blank screen :/ – Jay S. Oct 13 '22 at 19:57
  • Or even shorter : `include($pages[$_GET['p'] ?? '1'] ?? $pages['1']);` ;) – Syscall Oct 13 '22 at 20:12
  • @JayS. Look at your webserver logs to see what is your error. Seems like you have PHP syntax error. – Syscall Oct 13 '22 at 20:13
  • @Syscall Weird. As far as I can see, there were no errors. – Jay S. Oct 13 '22 at 20:43
  • I found what happened. It needs the query `p` and a value of more than `1`. Is there any way I can possibly automatically redirect a user if they haven't gone to the url with that? – Jay S. Oct 13 '22 at 21:19
  • Or the fact that the logs now say: ``[Thu Oct 13 22:28:19.129767 2022] [php:error] [pid 26344:tid 1848] [client ::1:49676] PHP Parse error: syntax error, unexpected token "else" in C:\\xampp\\htdocs\\Silicon\\index.php on line 49, referer: http://localhost/silicon/?p=1 `` – Jay S. Oct 13 '22 at 21:33
  • @JayS. see [PHP parse/syntax errors; and how to solve them](https://stackoverflow.com/q/18050071/9193372) – Syscall Oct 14 '22 at 06:59
  • @JayS. That syntax error can't be coming from what I've shown you here - demo: https://3v4l.org/B6Mhm ...if you eval that, there are no syntax errors. It must be elsewhere in your code. – ADyson Oct 14 '22 at 08:02
  • @JayS. `Is there any way I can possibly automatically redirect a user if they haven't gone to the url with that`...sure, just add another `else`...I updated the post. – ADyson Oct 14 '22 at 08:03
1

You dont need or want an elseif in this flow, simple do

<?php 
if (isset($_GET['p'])) { 
    if ($_GET['p'] == '1') {
        include('pages/home.php');
    }
    if ($_GET['p'] == '2') {
       include('pages/about.php'); 
    } 
}

If the value of $_GET['p'] can only be 1 or 2 you could do

<?php 
if (isset($_GET['p'])) { 
    if ($_GET['p'] == '1') {
        include('pages/home.php');
    } else {
       include('pages/about.php'); 
    }
}

Or if $_GET['p'] could be multiple values a switch may be more useful

<?php 
if (isset($_GET['p'])) { 

    switch($_GET['p']) {
        case 1:
            include('pages/home.php');
            break;
        case 2:
            include('pages/about.php'); 
            break;
        default:
            include('pages/somethingelse.php'); 
    }
}

Note I also left out the intermediate variable, as its not needed. The $_GET and $_POST arrays are, once filled by PHP all yours to use as you want. There is no need to waste CPU cycles and memory creating unnecessary variables

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149