Jump to content

How can I change included php file with browser?


Darkstasis

Recommended Posts

Before I look stupid. I am a new user to this forum, and I HAVE searched for what I'm looking for and haven't found it. I might be an idiot and probably searched for the wrong thing, if so, please put me in my place.

I plan to stick to this forums and ask questions, and hopefully help with newbie questions like mine in the future. I'm pretty sure my question is basic, help me start off please.

 

I've been building a site for the past few weeks and have gone through HTML pages after another.. I've recently noticed that PHP is what I need to learn and put to use, and possibly learn how to use other types of programming with it. I've learned so far this is wonderful to have, its much better than frames, lol :)

<?php
include 'news.php';
?>

 

After doing this to all my pages in attempts to separate the content from the layout, I realized I now have a directory that looks like:

 

index.php

page1.php

page2.php

content/news.php

content/page1content.php

content/page2content.php

 

I've had to create a duplicate of the index.php that originally includes news.php and rename it to page1.php and change the php code just to include the page1content.php.

 

The thing is, I don't want to duplicate the index with the layout because I want the content to change. I separated the content for a reason, so I don't have to update so many pages. I don't want to keep making these page1.php, page2.php, page89.php to keep the layout and include the different content php file.

 

So my link is like http://www.mysite.com/folder/ which automatically pulls the index.php and includes my news.php content.

How can I have a link thats http://www.mysite.com/folder/index.php?page2 - Assuming this would be correct, I'm wanting this to change the php code in index.php automatically from news.php to page2content.php. Ultimately, I have my index.php and my content pages, that is it. I need at least a way for the URL typed in the browser to change what content is included within the index.php.

 

Any help would be appreciated, hoping this is no trouble and easy to answer, also hoping to start working on it as soon as I get a solution.

Link to comment
Share on other sites

Is it something like this you are looking for?

 

$page_mapping = array(
  'page1' => 'content/page1content.php',
  'page2' => 'content/page2content.php',
  'news' => 'content/news.php',
);

$page = $_REQUEST['page'];
if (!empty($page_mapping[$page])) {
  include_once($page_mapping[$page]);
}

 

There are 2 reasons I use an array to map names to pages

 

1.  Users can only request a page that appears in your list (security)

2.  You can have a different name for the filename

 

You would call this script as

 

http://www.mysite.com/folder/index.php?page=page2

Link to comment
Share on other sites

EDIT 1: Spellcheck.

 

Okay, so its called an array? Interesting.

Anyways, thanks btherl, you have answered the question I was looking for.

Now, do you by chance know how to make it 3 letter shorter? maybe p=page2? If not, thats fine. I think this will work for now.

Link to comment
Share on other sites

Yep, changing page to p is simple:

 

$p = $_REQUEST['p'];
if (!empty($page_mapping[$p])) {
  include_once($page_mapping[$p]);
}

 

Be aware that php's arrays are something quite different to arrays in other languages.  In PHP, an array also acts as a hash table and a linked list, as well as a simple array.

 

The array in the code I posted is called an "associative array", becasue it associates strings with values.  Most other languages would call it a hash table.  A "normal array" is just a group of items in order, such as a list of products with name and price.

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.