Jump to content

Recommended Posts

See if I can explain this right.

 

I am currently using static files for a very basic set of pages.

 

Right now i have a main file that has three includes. a header include, body and footer.

 

Due to needing to add pages, i don't want to keep adding more and more files. instead I want to use php to change one of the includes based on the url.

 

so thissite.com/index.php?lesson1 would  change this

<?php include("header.php"); ?>
<?php include("content-lessons.html"); ?>
<?php include("footer.php"); ?>

to this

<?php include("header.php"); ?>
<?php include("lesson1.html"); ?>
<?php include("footer.php"); ?>

or whichever way would be the best way to set the second include.

 

my guess was that if the second line had something like this?

if(isset($_GET['lesson1'])){
include('include/in-'.$_GET['lesson1.html']

can someone give me a starting place here? i am pretty sure i can pick ti up if someone gets me going in the right direction.

 

thanks a ton.

Edited by story97

Your last example could do this for you although I would think you might use something other than 'filename.html' as the parm in the url. Maybe just a code or a word that your called script can use to lookup on a table or a static list of actual filenames and THEN execute the include.

 

Say your current web page has some kind of action that chooses the next web page to be built and loaded. The script that it triggers does this:

if (isset($_GET['file']) && $_GET['file']<>'')
{
$file = strtolower($_GET['file']);
if (array_key_exists($file,$files_ar))
$include_name = $files_ar[$file];
else
die("Invalid url value");
include('header.php');
include($include_name);
include('footer.php');
exit();
}
else
die("Missing url value")
exit();

 

All your pages would call this one script. Each page would be responsible for passing the next page's code/name in the url when it calls this.

hmm

 

Well, i tried that code and get no return. empty page

 

not sure why.

 

I also get no return in the console which is how i debug jquery, and see nothing in the php error logs. course, this isn't javascript so I can't really debug with firebug

Edited by story97

Actually i thought taht WAS the entire code. so wrap in php and go? then the call would be site/index.php?lesson1

 

just so you know, i know absolutely nothing about writing php. I can recognize things, but i am not a developer. i thought you meant to construct it just like that

Edited by story97

I did, i wrapped it in php and followed the url

 

Some of the things didn't make sense to me. i thought this would just require an if statement and a _GET to see what i put after the URL, and include the corresponding file. so if the URL ended ?lessonq1 then the second include would bring in the file named lesson1, which is an html template

<?php
if (isset($_GET['file']) && $_GET['file']<>'')
{
$file = strtolower($_GET['file']);
if (array_key_exists($file,$files_ar))
$include_name = $files_ar[$file];
else
die("Invalid url value");
include('header.php');
include($include_name);
include('footer.php');
exit();
}
else
die("Missing url value")
exit();
?>
Edited by story97

I'm not sure what you aren't undersdtanding. i put that in a blank PHP file and uploaded it

 

the example I showed you in the first post is all the existing page is. a single file with 3 includes that bring in a header, body and footer

Edited by story97

maybe I'm not explaining this right. i don't know.

 

anyone else that reads this, all i'm trying to do is set a variable so the end of the URL string defines the second include. so file.php?lesson1 would create an include for the file named lesson1.

 

I don't know exactly what I'm not explaining right

You explained it perfectly. You didn't understand what I proposed to you and you don't understand how to utilize the code I gave you. I gave you something YOU could use in conjunction with your existing code but apparently you don't have any existing code.

again, the only thing I can figure is I'm not explaining this right. instead of having 10 files each named differently with a different second include, i need to write a statement that will change the name of the second include based on what is on the URL since I know I can establish varibales using the URL string with ? & and =

You apparently didn't understand my original proposal for solving this problem. Perhaps a re-read would help but probably not. You don't have a good enough understanding of how web pages interact and how php can be used to act and react to events.

 

Good luck.

ginerjm's code relies on your having an array of filenames ($files_ar), which he didn't show. This could be hard-coded as in my example below or you could read the list into an array from a text file.

<?php

$files_ar = array (
        1 => 'Lesson1.html',
             'Lesson2.html',
             'Lesson3.html',
             'Lesson4.html',
             'Lesson5.html',
    );
    
if (isset($_GET['file']) && $_GET['file']<>'')
{
    $file = strtolower($_GET['file']);
    if (array_key_exists($file,$files_ar))
        $include_name = $files_ar[$file];
    else
        die("Invalid url value");
    include('header.php');
    include($include_name);
    include('footer.php');
    exit();
}
else
    die("Missing url value");
exit();

?>

You would then call the page with

 

thissite.com/index.php?file=1

 

etc

Edit:  Durp  :headslap: Did not see Barands reply

 

@story97 the code provided by ginerjm in post #2 will include the name of the file that is in the url query string parameter named file. However in order for the code to function correctly you first need to whitelist the files that can be included, this is prevent a user from including any file. You do this by adding them to an array called   $files_ar

 

Example

$files_ar = array(
   'lesson1' => 'full/path/to/lesson1.html';
   'lesson2' => 'full/path/to/lesson2.html';
   ...etc
);

// followed by ginerjm code

The url for including lession1.html will be site.com/your_page.php?file=lession1 this will then include the file lession1.html

Edited by Ch0cu3r

I think what our OP doesn't connect on is that he has to modify his existing(?) pages to pass the appropriate query string in his url to feed my code. Tried to get him to show me his code, but for some reason he couldn't. And that was that.

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.