Jump to content

i'm such a noob at php include


spacka

Recommended Posts

ok basically im making a website and i know what the srtucture is going to be like and i know that i want something like the following:

 

index.php?course=sport&level=2

 

this page would be located in "/pages/level1/sport.htm" or something similar.

 

i know i havent got this far yet with the following code, but even what i have got is bringing up an error:

 

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

if (!file_exists("$page.htm") && isset($page)) {
include($page . ".htm");
}
else {
include ("main.htm");
}
?>

 

as you can see - http://69.41.171.40/arctic/vocationalacademy/ - its not quite working.

Link to comment
Share on other sites

i mean, i know that at the minute theres not enough code to go as far as "index.php?course=sport&level=2" but what i should be able to do with the code ive got for right now is something like "index.php?page=level2" to bring up whats in "/pages/level2.htm"

Link to comment
Share on other sites

yeah no, sorry. thats just what i WANT to have when ive sorted everything. i'll be able to change words once im done, but at the minute im just stuck with the whole idea of getting it working. its been ages since ive used php, and i was never THAT great anyway.

 

but at the minute i have:

 

VOCATIONAL ACADEMY FOLDER-->

--index.php

--main.htm

--PAGES FOLDER-->

----level1.htm

----level2.htm

----level3.htm

----entrylevel.htm

 

"main.htm" contains something like 'IF YOU ARE READING THIS THEN ITS WORKING' and it loads fine, but theres also that line 23 error, and theres also the factt hat i cant include the filesi want to include from pages folder.

Link to comment
Share on other sites

here's my whole index.php file:

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<link rel="stylesheet" href="new.css">

</head>

<body marginheight="0" marginwidth="0">
<table width="781" border="0" cellpadding="0" cellspacing="0" class="contentmain">
  <tr>
    <td><p><img src="images/header.png" width="781" height="142"></p>
    <p> </p></td>
  </tr>
  <tr>
    <td><img src="images/sidebar.png" width="658" height="31"></td>
  </tr>
  <tr>
    <td><div align="left">
<?php
$page = $_GET['page'];

if (!file_exists("/pages/$page.htm") && isset($page)) {
include($page . ".htm");
}
else {
include ("main.htm");
}
?>
</div></td>
  </tr>
  <tr>
    <td><img src="images/footer.png" width="781" height="68"></td>
  </tr>
</table>
</body>
</html>

Link to comment
Share on other sites

this

 

if (!file_exists("/pages/$page.htm") && isset($page)) {
include($page . ".htm");
}
else {
include ("main.htm");
}

 

should of been

 

 

if (file_exists("/pages/$page.htm") && isset($page)) {
include($page . ".htm");
}
else {
include ("main.htm");
}

 

notice the !file_exists is changed to file_exists it will take out the error.. its a logical error..

Link to comment
Share on other sites

aaah yes, thats it.

 

you see, i knew it was something about the way i pointed to the directories, because when i had index.php in the same folder as level1.htm, it worked, but then i could figure out how to do the following line:

 

include("pages/{$page}.htm");

 

i kept trying to add quotations and came up with things like

 

include("pages/$page} . ".htm""); which obvously came up with errors.

thanks for the help.

Link to comment
Share on other sites

sorry to be a pain, but now that we know how to do this, what code would i add to get something like this:

 

index.php?page=level1&course=sport

 

obviously we know that level1.htm is in /pages/, but sport.htm is in /pages/level1/

 

any ideas?

Link to comment
Share on other sites

your code should be

 

<?
$page = $_GET['page'];
$course = @$_GET['course'];

if (isset($page) && isset($course) && file_exists("pages/{$page}/{$course}.htm")) {
include("pages/{$page}/{$course}.htm");
}
else if (isset($page) && file_exists("pages/{$page}.htm")) {
include("pages/{$page}.htm");
}
else {
include ("main.htm");
}
?>

 

this will take care of

 

index.php?page=level1 - show level1.htm in pages/ folder

index.php?page=level1&course=sport - show sport.html in pages/level1 folder

 

hope its helpful

Link to comment
Share on other sites

yeah that sounds good.

 

also, i will have the same for level 2, level 3 and maybe others.

 

so would it be easiest to just repeat the code to goto /pages/level2/sport.htm, /pages/level3/sport.htm etc, or is there an easier/better way?

 

this sounds good so far, though, thanks.

Link to comment
Share on other sites

thats so excellent, thanks man.

 

also, i may have got the whole "main.htm" part wrong.

 

what i want to do is basically have the else include (or even just echo) some kind of error message like "error" or "sorry, this doesnt exist" because this is whats going to show if the code does not work.

 

however, on the index.php i DO want to have a display, such as a welcome page or something, but i dont think this should be part of the php include script.

 

hopefully you know what i mean. if not, i can probably explain it more simply.

Link to comment
Share on other sites

try this out

 

<?php
$page = $_GET['page'];
$course = @$_GET['course'];

if (isset($page) && isset($course) && file_exists("pages/{$page}/{$course}.htm")) {
include("pages/{$page}/{$course}.htm");
}
else if (isset($page) && file_exists("pages/{$page}.htm")) {
include("pages/{$page}.htm");
}
else if ((isset($page) && !isset($course) && !file_exists("pages/{$page}.htm")) || (isset($page) && isset($course) && !file_exists("pages/{$page}/{$course}.htm"))) {
include("error.htm");
}
else {
include ("main.htm");
}
?>

 

make a error.htm and put it in your main folder with the error message..

hope its helpful

Link to comment
Share on other sites

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.