skip to Main Content
<?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 🙂

2

Answers


  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

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search