Jump to content

PHP Include Question


Mmarzex

Recommended Posts

I'm using a simple php include code that will include a text file with content inside the main layout of a page. Now its working fine for including the main default page but whenever I try and include another content page by navigating to whatever.com/index.php?=test it doesn't work and still just shows the main text file I'm not sure why. and the code I'm using is

<?php
if (!$id) {
$id = "main";
$include = $id . ".txt";
}
else {
$include = $id . ".txt";
}
if (is_file($include) == "1") {
        include $include;
}
else {
include "404.txt";
}
?>

I'm also getting this error

Notice: Undefined variable: id in /opt/lampp/htdocs/beta/index.php  on line 8

Link to comment
https://forums.phpfreaks.com/topic/198237-php-include-question/
Share on other sites

At the start of the script use

 

// this will set $id to the value in the URL, if none then use 'main'

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

 

Also, you say whatever.com/index.php?=test well that is wrong, you haven't specificed the id variable name. You'd need to use

 

whatever.com/index.php?id=test

At the start of the script use

 

// this will set $id to the value in the URL, if none then use 'main'

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

 

Also, you say whatever.com/index.php?=test well that is wrong, you haven't specificed the id variable name. You'd need to use

 

whatever.com/index.php?id=test

 

I meant to tyype index.php?id=test and the code you gave me solved the error of it not working but I'm still getting the undefined variable error.

 

This is the code I have now

<?php
if (!$id) {
$id = isset($_GET['id']) ? $_GET['id'] : 'main';
//$id = "main";
$include = $id . ".txt";
}
else {
$include = $id . ".html";
}
if (is_file($include) == "1") {
        include $include;
}
else {
include "404.txt";
}
?>

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.