Jump to content

Recommended Posts

This might be a very easy problem to solve. I am literally brand new to php.

I am trying to create a simple site that will load content depending on what page 'id' is in the URL and if it doesn't exist to show a page for that scenario.

eg index.php?id=1 will show the page 1.php

 

It works fine apart from showing an error when there is no id at all,

eg  www.website.com will show an error but www.website.com/index.php?id=1 will show a page

I want to be able to show the homepage if there is no 'id' at all.

 

<?php
$id = $_GET['id'];


if(file_exists("./".$id.".php"))
  {
  include ("./".$id.".php");
  }
else
  {
  include ("404.php");
  }

?>

 

Any help  is appreciated

Thanks

Link to comment
https://forums.phpfreaks.com/topic/235178-simple-dynamic-content-site-help/
Share on other sites

Add an if statement which checks to see if $_GET['id'] isset. If it does then set $id to the value of $_GET['id']. Otherwise set $id to 1

The above translated into PHP code

$id = isset($_GET['id']) ? $_GET['id'] : 1;

Note the ?: syntax is called a ternary operator (an in-line if/else statement)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.