Jump to content

Simple dynamic content site help


debbie-g85

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)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.