• 0

PHP question.


Question

Currently, my site is (except the jquery menu) entirely HTML/CSS.

I'm building it with Includes to make updates easier: update the Menu file and

it's updated everywhere that menu is loaded.

Think Lego blocks. The header is its own file, the footer, etc. Like so:

<include header.css>

<include content.css>

<include footer.css>

And thus, the webpage is displayed.

The only thing that changes on my site is the Content area. So, I'm curious if

PHP can perform this function for me:

1. Instead of having different content.css file, have a $Content variable. When

the site first loads, the $Content variable is set to Home. Therefore, for the

content section, it includes the Home.css file.

2. If you click Stories in the main menu, the $Content variable is then set to

Stories. The exact same load sequence happens but instead of home.css it

now loads stories.css file.

I'm thinking dynamically. When you have a rollover image effect, there is one image

displayed on the page. You hover your mouse over the image and the image updates,

but the rest of the page stays exactly the same. I'd like to do this for my content area.

Link to comment
Share on other sites

22 answers to this question

Recommended Posts

  • 0

The simplest way is to have one css file for the entire webpage. Also, one main HTML and a new file for every subpage content.

In that case, the PHP code is simple:

Top of the main page:

&lt;?php
if(isset($_GET['pg']) &amp;&amp; !empty($_GET['pg']))
{
$pg = $_GET['pg'];
switch($pg)
{
case 'page1':
break;
case 'page2':
break;
default:
$pg = 'page0';
break;
}
}
else
{
$pg = 'page0';
}
?&gt;

Place where you want the content to display on the main page:

&lt;?php
require("./" . $pg . ".php");
?&gt;

After that, you can simply call the content via:

&lt;a href="page1.html" title="Page 1"&gt;Page 1&lt;/a&gt;

Link to comment
Share on other sites

  • 0

I'm at a loss.

So I should have an index.html or index.php?

At the top of the page... before the body or head?

And what do you mean I call the content? I'm trying to figure out where the code

grabs the user's menu choice.

If I combine all the elements for the main page, it looks like this:

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
&lt;title&gt; Author: Richard M. McCord II&lt;/title&gt;
&lt;meta name="Author" content="Richard M. McCord II" /&gt;
&lt;link rel="stylesheet" type="text/css" href="Code/layout.css" /&gt;
&lt;link rel="shortcut icon" href="favicon.ico" /&gt;
&lt;script type="text/javascript" src="Code/jquery-1.4.2.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="Code/jquery.easing.1.3.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="Code/jquery.lavalamp-1.3.4b2.js"&gt;&lt;/script&gt;

&lt;/head&gt;


&lt;body&gt;
		&lt;!-- START Page --&gt;

&lt;div id="wrapper"&gt;

		&lt;!-- START Header --&gt;
&lt;div id="Header"&gt;

	&lt;div id="Header-Left"&gt;
	&lt;a href="http://richardmccord.com"&gt; &lt;img src="Graphics/logo.png" alt="" img border="0"/&gt;&lt;/a&gt;
	&lt;/div&gt;

&lt;img src="Graphics/separator.png" alt="" img border="0"/&gt;

	&lt;div id="Header-Right"&gt;
	My thoughts. My words. My world.
	&lt;/div&gt;

&lt;/div&gt;
		&lt;!-- END Header --&gt;


		&lt;!-- START Menu --&gt;
&lt;div id="menu-box"&gt;
&lt;ul id="menu"&gt;
&lt;li&gt;&lt;a href="http://www.richardmccord.com/biography.html"&gt;About the author&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.richardmccord.com/Game/"&gt;My Game&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.richardmccord.com/Writing/"&gt;Stories&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.richardmccord.com/Reviews/"&gt;Reviews&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.richardmccord.com/Opinions/"&gt;Opinions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.richardmccord.com/Videos/"&gt;Videos&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;

&lt;script type="text/javascript"&gt;
	$(function() {
	$('ul#menu').lavaLamp();
	});
 &lt;/script&gt;
		&lt;!-- END Menu --&gt;


		&lt;!-- START content --&gt;
&lt;!--#include file="content.css" --&gt;
		&lt;!-- END content --&gt;


		&lt;!-- START Footer --&gt;
&lt;div id="Footer"&gt;
© 1998-2010 Richard M. McCord II
&lt;/div&gt;
		&lt;!-- END Footer --&gt;


&lt;/div&gt;
		&lt;!-- END page --&gt;

&lt;/body&gt;
&lt;/html&gt;

Link to comment
Share on other sites

  • 0

you should use index.php if your using php code.

also if your using the code bluefish posted, you need to call the links like this

index.php?pg=pagename

where pagename is the name of the file with the content is.

that way, index.php knows which page to include (from the url).

Link to comment
Share on other sites

  • 0

Slightly off-topic, CSS stands for Cascading Style Sheet. I highly doubt your content is that. Change your file extensions to something more appropriate, such as html, php, anything but css.

Link to comment
Share on other sites

  • 0

You could certainly do that but I wouldn't recommend it. While it certainly sounds efficient it really depends on how much content you have. Would you prefer one long scrolling page holding content for all pages ? I don't think editing that would be easy with all the scrolling. Besides also remember to sanitize the page variable because it open to being abused.

I would rather have individual php pages with repeating content as includes.

Link to comment
Share on other sites

  • 0

Well, that's certainly the question.

For efficiency and easy-of-use, I was going to go with a CMS. That came with some

MAJOR headaches that I don't get when I have my own system in place. So I'm back

to doing it my way.

As you can see above, I was using separate HTML pages and completely loading

these every time. But the HTML pages were completely made up of external file that

were brought in (Lego blocks style) with the Include function. Someone suggested this

wasn't the most efficient way to go about it and that I should use PHP for my purposes.

Well, I don't know PHP. I used to use Frontpage- so I can put together some HTML, but

that's about it.

Link to comment
Share on other sites

  • 0

You could certainly do that but I wouldn't recommend it. While it certainly sounds efficient it really depends on how much content you have. Would you prefer one long scrolling page holding content for all pages ? I don't think editing that would be easy with all the scrolling. Besides also remember to sanitize the page variable because it open to being abused.

I would rather have individual php pages with repeating content as includes.

Ok, how do you mean? My site is going to have to many pages to do it the way blufish posted. Some of the content is multi-page

stories, for example.

Link to comment
Share on other sites

  • 0

Here is how I would do it. Say you are running a site on articles about web development. The url structure would be something like...

http://www.mysite.com/articles/css/navigation_using_unordered_list
http://www.mysite.com/articles/html/accessible_table_markup
http://www.mysite.com/articles/php/using_includes_for_easy_maintenance

Now the folder structure

--css
  |--navigation_using_unordered_list_01.php
  |--navigation_using_unordered_list_02.php
  |--navigation_using_unordered_list_03.php
--html
  |--accessible_table_markup_01.php

and so on...

And finally the router. Setup a file using procedural or oop based to analyze the url and extract the article details and rebuild the path to the file and include the file in a template. A folder structure like that will help you keep from repeating data at the same time make pinpoint editing a breeze since you would know where exactly the data is coming from.

Link to comment
Share on other sites

  • 0

Yea, that looks like my current folder setup.

Instead of PHP pages inside the folders, there are .css files with

the content of each page.

They aren't actually CSS pages. I was using that to identify the individual files

here for the purpose of example. The point being, the Content area changes.

Therefore, only the different content areas are given their own files. The plan

is to have one main page that loads, then switch out the content area based

on menu choices while keeping the rest of the page the same.

For example, here is the content file for my Welcome screen:

&lt;div id="Content"&gt;
&lt;div id="Content-BG"&gt;
	&lt;div id="Content_top"&gt;
		&lt;div class="title"&gt; &lt;p&gt;Welcome&lt;/p&gt; &lt;/div&gt;
	&lt;/div&gt;


		&lt;div id="left"&gt;
&lt;p&gt; You have managed to stumble upon the official website of Richard M. McCord II.&lt;/p&gt;
&lt;p&gt;  This   may have just been bad luck on your part, or perhaps the end result of some cruel joke by one of your friends. Regardless, here you
  are. There are a few things you should know before clicking any of the links on this website:&lt;br /&gt;
  1. This is an adult website. If you are not of an adult age in your region, you should leave this website right now.&lt;br /&gt;
  2.   This is the personal website of Richard M. McCord II. All material   contained on this website should be considered the personal 
  opinion of Richard M. McCord II unless expressly noted as being a direct quote of someone else.&lt;br /&gt;
  3.   Richard M. McCord II is a very offensive person. He uses adult   language, big words, and makes fun of just about everything. If you 
  are easily offended, or have ever found that you were offended by   anything, you may want to consider leaving this website. Richard M. McCord II will not aplogize for offending you; he didn't invite you here   in the first place, nor is he forcing you to stay. You are free 
  to leave right now.&lt;br /&gt;
  4. Richard M. McCord II does not care if you like   his writing and/or videos. He will not change them for you. If you do   not like his 
  writing or his videos then you are free to leave and never come back to this website. Power to the people.&lt;br /&gt;
  5.   You are not authorized to copy, or otherwise use, anything on this   website. You may not repost the writing here on another website, 
  or in any form offline such as- but not limited to- newspapers,   magazines, fliers, or your kitchen table. You may not download the 
  videos or play the videos on your website, or any other website. If you   wish to show off the work of Richard M. McCord II then send 
  people to this website... be sure to warn them ahead of time that the content here is adult in nature.&lt;/p&gt;
&lt;p&gt;And with that, welcome to the official website of Richard M. McCord II. Enjoy your stay.&lt;/p&gt;
		&lt;/div&gt;

		&lt;div id="right"&gt;

		&lt;/div&gt;



	&lt;div id="Content_bottom"&gt;&lt;/div&gt;

&lt;/div&gt;
&lt;/div&gt;

So technically, I could put all those DIVS in the main page as well and have the content area JUST be the actual words

that appear there. But regardless, I run into the same issue. I may just have to keep doing it the way I have been in

the past and just use HTML pages for everything. Trying to make it dynamic is irritating.

Link to comment
Share on other sites

  • 0

Crap. I can't delete my own freakin posts.

Ignore the above. I've uploaded some content under the Opinions section to better show

how my site currently works.

When you go to the site, the default page is loaded and Includes the menu and specific

Content file for that page.

When you go to Opinions, it loads the Opinions.html file and Includes the specific content

file for that page, along with the sidebar menu. Easy. This just means that I'm creating

two files for each page: the HTML file which doesn't change except for the second file

which is the specific Content file for that page.

Link to comment
Share on other sites

  • 0

If you are making things dynamic and it is creating more work for you it means only one thing. You are not doing it right. I don't think I see a link to your site here. If you can post a link and a sample page that you are trying to template / make dynamic I can give you a very basic frame work to set it up.

Link to comment
Share on other sites

  • 0

SITE:

http://www.richardmccord.com

Currently, you can click on About the Author and Opinions. Under Opinions, you'll see

the sidebar menu.

The part I want to be dynamic is instead of having HTML files for each individual page,

just keep using the main page and change the INCLUDE file for the changing content.

Link to comment
Share on other sites

  • 0

I prefer to keep data files outside web accessible directory for two reasons. Stop unauthorized access and stop spiders from indexing the content directly. Following is the folder structure adapted.

Site Root
    |--opinions
        |--home (Default)
        |--normality
        |--religious_text
        |--perfect_os
    |--public_html (Web root)
        |--.htaccess (Mod Rewrite)
        |--opinions.php (Content Server)

Mod rewrite helps with seo friendly urls while keeping the site dynamic. Content server serves the content requested by importing and inserting data as required. The data is safely stored away out of reach of users and spiders, organized and served as required. Let me know if you have any questions. The attached files have been tested using a wamp install.

web files.zip

Link to comment
Share on other sites

  • 0

OK, a couple of things.

I'm not hosting the site myself, it's on a web host so there's the

public_html and then under that is richardmccord.com. If I put files

beneath public_html they will be accessible to other folks on the

webhost, won't they?

I created a new directory structure to show that the files work:

http://richardmccord.com/test/public_html/opinions.php

So now, how do I fit that into my actual site? It loads the content, as

you can see... but there's more to the page than just the content. Do I

keep everything else the same and change the INCLUDE section of each page

to the PHP code? (I don't do PHP so it's completely unfamiliar to me)

Everything on my pages is exactly the same throughout my site except for the

*** START CONTENT ***

area. You'll see the Left div and the Right div. If there's no menu, then the

Right Div will be empty. In the case of the Opinions section, there is a menu.

So here's the code for the initial Opinions page:

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
&lt;title&gt; Author: Richard M. McCord II&lt;/title&gt;
&lt;meta name="Author" content="Richard M. McCord II" /&gt;
&lt;link rel="stylesheet" type="text/css" href="../Code/layout.css" /&gt;
&lt;link rel="shortcut icon" href="favicon.ico" /&gt;
&lt;script type="text/javascript" src="../Code/jquery-1.4.2.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="../Code/jquery.easing.1.3.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="../Code/jquery.lavalamp-1.3.4b2.js"&gt;&lt;/script&gt;

&lt;/head&gt;


&lt;body&gt;
		&lt;!-- START PAGE --&gt;

&lt;div id="wrapper"&gt;

		&lt;!-- START HEADER --&gt;
&lt;div id="Header"&gt;

	&lt;div id="Header-Left"&gt;
	&lt;a href="http://richardmccord.com"&gt; &lt;img src="../Graphics/logo.png" alt="" img border="0"/&gt;&lt;/a&gt;
	&lt;/div&gt;

&lt;img src="../Graphics/separator.png" alt="" img border="0"/&gt;

	&lt;div id="Header-Right"&gt;
	My thoughts. My words. My world.
	&lt;/div&gt;

&lt;/div&gt;
		&lt;!-- END HEADER --&gt;


		&lt;!-- START Menu --&gt;
&lt;!--#include file="MainMenu.inc" --&gt;

	&lt;script type="text/javascript"&gt;
	$(function() {
	$('ul#menu').lavaLamp();
	});
	&lt;/script&gt;
		&lt;!-- END Menu --&gt;


		&lt;!-- START content --&gt;

	&lt;div id="Content"&gt;
	&lt;div id="Content-BG"&gt;
	&lt;div id="Content_top"&gt;
	&lt;div class="title"&gt;
&lt;p&gt;Opinions&lt;/p&gt; 
	&lt;/div&gt;
	&lt;/div&gt;
		&lt;div id="left"&gt;

&lt;!--#include file="Opinions.inc" --&gt;

		&lt;/div&gt;

&lt;div id="right"&gt;
&lt;!--#include file="Opinions-Sidebar.inc" --&gt;
&lt;/div&gt;

	&lt;div id="Content_bottom"&gt;&lt;/div&gt;
	&lt;/div&gt;
	&lt;/div&gt;

		&lt;!-- END content --&gt;


		&lt;!-- START Footer --&gt;
&lt;div id="Footer"&gt;

© 1998-2010 Richard M. McCord II

&lt;/div&gt;
		&lt;!-- END Footer --&gt;





&lt;/div&gt;
		&lt;!-- END page --&gt;

&lt;/body&gt;
&lt;/html&gt;

The Left Div loads the include file for the content, and the Right Div

loads the include file for the menu. The content being:

&lt;p&gt;Welcome to my Opinions area.&lt;/p&gt;
&lt;p&gt;Here you will find my personal thoughts on various topics which peak my interest. I should strongly suggest here that you do not attempt to use these thoughts as evidence in your college paper since they are entirely opinion-based. These papers are written from my personal perspective based on my personal observations. I do not claim these opinions to be entirely factual as Facts can change based on one's perspective... and I am privy only to my own.&lt;/p&gt;

And the menu being:

&lt;p class="righttitle"&gt;Opinions&lt;/p&gt;
&lt;p class="rightsubtitle"&gt;&lt;a href="http://www.richardmccord.com/Opinions/Agnostic_Realist.html"&gt;My Religion&lt;/a&gt;&lt;/p&gt;
&lt;p class="rightsubtitle"&gt;&lt;a href="http://www.richardmccord.com/Opinions/Death_Penalty.html"&gt;Death Penalty&lt;/a&gt;&lt;/p&gt;
&lt;p class="rightsubtitle"&gt;&lt;a href="http://www.richardmccord.com/Opinions/Freedom.html"&gt;Freedom&lt;/a&gt;&lt;/p&gt;
&lt;p class="rightsubtitle"&gt;&lt;a href="http://www.richardmccord.com/Opinions/Normality.html"&gt;Normality&lt;/a&gt;&lt;/p&gt;
&lt;p class="rightsubtitle"&gt;&lt;a href="http://www.richardmccord.com/Opinions/Organ_Donor.html"&gt;Organ Donor&lt;/a&gt;&lt;/p&gt;
&lt;p class="rightsubtitle"&gt;&lt;a href="http://www.richardmccord.com/Opinions/Religious_Text.html"&gt;Religious Text&lt;/a&gt;&lt;/p&gt;

I could have had my main menu be a drop-down, but I hate those. I think they're ugly as sin. Therefore, a section

has a sidebar menu if necessary. Every page under the Opinions section has one. Or, in the case of a story, the

pages will be listed. It looks like even with a PHP setup, I'll still need separate pages for the page itself, AND the

content info just like I have now? Only in PHP format instead of .HTML + .INC file?

P.S.

As a sidenote, black or brown?

Here's the brown look:

http://richardmccord.com/brown/

Link to comment
Share on other sites

  • 0

I've been playing with your example a bit...

I'm not getting how to:

1. Turn my site into PHP from the start. In the example above, make it legitimate PHP.

2. I understand (from SOME programming) what I'm looking at with the Opinions.PHP code...

but how do I get a link to change the $Page variable? It has a function in there to Get it, but

where is it getting it from? My Main Menu at the top would link to the Opinions.PHP file and

pull the Home.PHP as the Content since that's the default page for that section. Therefore,

linking to the sections is easy because I'm not passing any variables there; it's loaded automatically.

But once I'm in the Opinions section, how to I change $Page to, say, Normality.PHP?

Link to comment
Share on other sites

  • 0

OK, so I changed my Index.html file to Index.php.

I changed the code inside to:

&lt;?php
include 'Head.inc';
?&gt;

		&lt;!-- START CONTENT --&gt;
&lt;?php

define('ROOT', '');

$page = $_GET['page'];

if ( ! preg_match( '#^[a-z_]+$#iD', $_GET['page'] ) ):
	$page = 'home';
endif;

$file = ROOT . $page . '.php';

if ( ! is_file( $file ) ):
	$file = ROOT . 'Welcome.php';
endif;

$data = file_get_contents( $file );

$array = explode( '%%%%', $data );

foreach ( $array as $key =&gt; $value ):
	$val = explode( '====', $value );
	$name = trim( strtolower($val[0]) );
	$pageData[$name] = nl2br( trim( $val[1] ) );
endforeach;

?&gt;


	&lt;div id="Content"&gt;
	&lt;div id="Content-BG"&gt;
	&lt;div id="Content_top"&gt;
	&lt;div class="title"&gt;
	&lt;p&gt;&lt;?php echo $pageData['title']; ?&gt;&lt;/p&gt; 
	&lt;/div&gt;
	&lt;/div&gt;
		&lt;div id="left"&gt;

&lt;p&gt;&lt;?php echo $pageData['content']; ?&gt;&lt;/p&gt;

		&lt;/div&gt;

&lt;div id="right"&gt;
&lt;/div&gt;

	&lt;div id="Content_bottom"&gt;&lt;/div&gt;
	&lt;/div&gt;
	&lt;/div&gt;

		&lt;!-- END CONTENT --&gt;
&lt;?php
include 'Foot.inc';
?&gt;

Note: The Head.inc and Foot.inc file are JUST to clear away all the code so that I can concentrate on

the "working" Content area. It makes it easier to read.

So, the index.php file gets loaded when someone goes to my site. It pulls the Welcome.php files by default.

Works like a champ. I understand that, and the Welcome.php method is awesome since I don't have to use

<p></p> code inside it; I can just create my content with a normal text editor. Awesome.

But what if someone clicks About The Author in the main menu? I'm still trying to figure out the purpose

of the Get function to define the Page. I would assume in your Opinions example that the code is parsing the Opinions folder

to note all the PHP files there, but how does it determine which one to associate with Page as opposed to just going

with the default?

Link to comment
Share on other sites

  • 0

Arg. I wish we could at least get rid of the time countdown for Editing a post.

That way, I wouldn't have to keep creating a brand-new reply for changes.

Anyhow, if the code isn't parsing the Opinions directory, is it instead parsing

the file specified in the code? I thought Home.php was just being assigned as

the default, but it seems the code may just be parsing the Home.php file and

finding the Title and Content sections.

Link to comment
Share on other sites

  • 0

Let me explain how I set things up to work. Lets take a sample url say www.domain.com/opinions/perfect_os. This url is translated on the fly behind the scenes by the .htaccess file to www.domain.com/opinions.php?page=perfect_os. So $_GET['page'] is now defined.

The constant ROOT is simply the path to where the content files live. The page variable provided is first tested to make sure it doesn't have anything other than alphabets and underscore. The file with entire path is assembled and tested to see if the file exists. When either of these conditions fail default content from home.php file is loaded.

After the name of the content file has been determined the script parses the data in the files to obtain the title and content section which is then inserted in to the respective spots. '%%%%' is used to separate sections and '====' is used to separate name of the section and section data.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.