McCordRm Posted August 18, 2010 Share Posted August 18, 2010 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 https://www.neowin.net/forum/topic/930690-php-question/ Share on other sites More sharing options...
0 deactivated01032015 Posted August 18, 2010 Share Posted August 18, 2010 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: <?php if(isset($_GET['pg']) && !empty($_GET['pg'])) { $pg = $_GET['pg']; switch($pg) { case 'page1': break; case 'page2': break; default: $pg = 'page0'; break; } } else { $pg = 'page0'; } ?> Place where you want the content to display on the main page: <?php require("./" . $pg . ".php"); ?> After that, you can simply call the content via: <a href="page1.html" title="Page 1">Page 1</a> Link to comment https://www.neowin.net/forum/topic/930690-php-question/#findComment-593044494 Share on other sites More sharing options...
0 McCordRm Posted August 18, 2010 Author Share Posted August 18, 2010 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: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title> Author: Richard M. McCord II</title> <meta name="Author" content="Richard M. McCord II" /> <link rel="stylesheet" type="text/css" href="Code/layout.css" /> <link rel="shortcut icon" href="favicon.ico" /> <script type="text/javascript" src="Code/jquery-1.4.2.js"></script> <script type="text/javascript" src="Code/jquery.easing.1.3.js"></script> <script type="text/javascript" src="Code/jquery.lavalamp-1.3.4b2.js"></script> </head> <body> <!-- START Page --> <div id="wrapper"> <!-- START Header --> <div id="Header"> <div id="Header-Left"> <a href="http://richardmccord.com">;img src="Graphics/logo.png" alt="" img border="0"/></a> </div> <img src="Graphics/separator.png" alt="" img border="0"/> <div id="Header-Right"> My thoughts. My words. My world. </div> </div> <!-- END Header --> <!-- START Menu --> <div id="menu-box"> <ul id="menu"> <li><a href="http://www.richardmccord.com/biography.html">Aboutauthor</a></li> <li><a href="http://www.richardmccord.com/Game/">My</a></li> <li><a href="http://www.richardmccord.com/Writing/">Stories</a></li>;li><a href="http://www.richardmccord.com/Reviews/">Reviews</a></li>;li><a href="http://www.richardmccord.com/Opinions/">Opinions</a></li>;li><a href="http://www.richardmccord.com/Videos/">Videos</a></li>;/ul> </div> <script type="text/javascript"> $(function() { $('ul#menu').lavaLamp(); }); </script> <!-- END Menu --> <!-- START content --> <!--#include file="content.css" --> <!-- END content --> <!-- START Footer --> <div id="Footer"> © 1998-2010 Richard M. McCord II </div> <!-- END Footer --> </div> <!-- END page --> </body> </html> Link to comment https://www.neowin.net/forum/topic/930690-php-question/#findComment-593045324 Share on other sites More sharing options...
0 jbrooksuk Posted August 18, 2010 Share Posted August 18, 2010 By default servers will only execute PHP code in *.php extensions. Link to comment https://www.neowin.net/forum/topic/930690-php-question/#findComment-593045364 Share on other sites More sharing options...
0 McCordRm Posted August 18, 2010 Author Share Posted August 18, 2010 I get that... but he mentioned having one main HTML file. Link to comment https://www.neowin.net/forum/topic/930690-php-question/#findComment-593045410 Share on other sites More sharing options...
0 Colin-uk Veteran Posted August 18, 2010 Veteran Share Posted August 18, 2010 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 https://www.neowin.net/forum/topic/930690-php-question/#findComment-593045444 Share on other sites More sharing options...
0 deactivated01032015 Posted August 18, 2010 Share Posted August 18, 2010 On 18/08/2010 at 14:45, McCordRm said: I get that... but he mentioned having one main HTML file. Sorry, my mistake. Link to comment https://www.neowin.net/forum/topic/930690-php-question/#findComment-593045620 Share on other sites More sharing options...
0 Kudos Veteran Posted August 18, 2010 Veteran Share Posted August 18, 2010 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 https://www.neowin.net/forum/topic/930690-php-question/#findComment-593045630 Share on other sites More sharing options...
0 sweetsam Posted August 18, 2010 Share Posted August 18, 2010 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 https://www.neowin.net/forum/topic/930690-php-question/#findComment-593045640 Share on other sites More sharing options...
0 McCordRm Posted August 19, 2010 Author Share Posted August 19, 2010 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 https://www.neowin.net/forum/topic/930690-php-question/#findComment-593047676 Share on other sites More sharing options...
0 McCordRm Posted August 19, 2010 Author Share Posted August 19, 2010 On 18/08/2010 at 15:52, sweetsam said: 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 https://www.neowin.net/forum/topic/930690-php-question/#findComment-593051462 Share on other sites More sharing options...
0 sweetsam Posted August 20, 2010 Share Posted August 20, 2010 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 https://www.neowin.net/forum/topic/930690-php-question/#findComment-593051728 Share on other sites More sharing options...
0 McCordRm Posted August 20, 2010 Author Share Posted August 20, 2010 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: <div id="Content"> <div id="Content-BG"> <div id="Content_top"> <div class="title"> <p>Welcome</p> </div> </div> <div id="left"> <p> You have managed to stumble upon the official website of Richard M. McCord II.</p> <p> 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:<br /> 1. This is an adult website. If you are not of an adult age in your region, you should leave this website right now.<br /> 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.<br /> 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.<br /> 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.<br /> 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.</p> <p>And with that, welcome to the official website of Richard M. McCord II. Enjoy your stay.</p> </div> <div id="right"> </div> <div id="Content_bottom"></div> </div> </div> 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 https://www.neowin.net/forum/topic/930690-php-question/#findComment-593054716 Share on other sites More sharing options...
0 McCordRm Posted August 20, 2010 Author Share Posted August 20, 2010 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 https://www.neowin.net/forum/topic/930690-php-question/#findComment-593055046 Share on other sites More sharing options...
0 sweetsam Posted August 20, 2010 Share Posted August 20, 2010 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 https://www.neowin.net/forum/topic/930690-php-question/#findComment-593055314 Share on other sites More sharing options...
0 McCordRm Posted August 20, 2010 Author Share Posted August 20, 2010 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 https://www.neowin.net/forum/topic/930690-php-question/#findComment-593055368 Share on other sites More sharing options...
0 sweetsam Posted August 21, 2010 Share Posted August 21, 2010 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.zipFetching info... Link to comment https://www.neowin.net/forum/topic/930690-php-question/#findComment-593058404 Share on other sites More sharing options...
0 McCordRm Posted August 21, 2010 Author Share Posted August 21, 2010 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: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title> Author: Richard M. McCord II</title> <meta name="Author" content="Richard M. McCord II" /> <link rel="stylesheet" type="text/css" href="../Code/layout.css" /> <link rel="shortcut icon" href="favicon.ico" /> <script type="text/javascript" src="../Code/jquery-1.4.2.js"></script> <script type="text/javascript" src="../Code/jquery.easing.1.3.js"></script> <script type="text/javascript" src="../Code/jquery.lavalamp-1.3.4b2.js"></script> </head> <body> <!-- START PAGE --> <div id="wrapper"> <!-- START HEADER --> <div id="Header"> <div id="Header-Left"> <a href="http://richardmccord.com">;img src="../Graphics/logo.png" alt="" img border="0"/></a> </div> <img src="../Graphics/separator.png" alt="" img border="0"/> <div id="Header-Right"> My thoughts. My words. My world. </div> </div> <!-- END HEADER --> <!-- START Menu --> <!--#include file="MainMenu.inc" --> <script type="text/javascript"> $(function() { $('ul#menu').lavaLamp(); }); </script> <!-- END Menu --> <!-- START content --> <div id="Content"> <div id="Content-BG"> <div id="Content_top"> <div class="title"> <p>Opinions</p> </div> </div> <div id="left"> <!--#include file="Opinions.inc" --> </div> <div id="right"> <!--#include file="Opinions-Sidebar.inc" --> </div> <div id="Content_bottom"></div> </div> </div> <!-- END content --> <!-- START Footer --> <div id="Footer"> © 1998-2010 Richard M. McCord II </div> <!-- END Footer --> </div> <!-- END page --> </body> </html> The Left Div loads the include file for the content, and the Right Div loads the include file for the menu. The content being: <p>Welcome to my Opinions area.</p> <p>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.</p> And the menu being: <p class="righttitle">Opinions</p> <p class="rightsubtitle"><a href="http://www.richardmccord.com/Opinions/Agnostic_Realist.html">Mygion</a></p> <p class="rightsubtitle"><a href="http://www.richardmccord.com/Opinions/Death_Penalty.html">Deathlty</a></p> <p class="rightsubtitle"><a href="http://www.richardmccord.com/Opinions/Freedom.html">Freedom</a></p>;p class="rightsubtitle"><a href="http://www.richardmccord.com/Opinions/Normality.html">Normality</a></p>;p class="rightsubtitle"><a href="http://www.richardmccord.com/Opinions/Organ_Donor.html">Organr</a></p> <p class="rightsubtitle"><a href="http://www.richardmccord.com/Opinions/Religious_Text.html">Religious</a></p> 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 https://www.neowin.net/forum/topic/930690-php-question/#findComment-593058688 Share on other sites More sharing options...
0 McCordRm Posted August 22, 2010 Author Share Posted August 22, 2010 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 https://www.neowin.net/forum/topic/930690-php-question/#findComment-593059946 Share on other sites More sharing options...
0 McCordRm Posted August 22, 2010 Author Share Posted August 22, 2010 OK, so I changed my Index.html file to Index.php. I changed the code inside to: <?php include 'Head.inc'; ?> <!-- START CONTENT --> <?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 => $value ): $val = explode( '====', $value ); $name = trim( strtolower($val[0]) ); $pageData[$name] = nl2br( trim( $val[1] ) ); endforeach; ?> <div id="Content"> <div id="Content-BG"> <div id="Content_top"> <div class="title"> <p><?php echo $pageData['title']; ?></p> </div> </div> <div id="left"> <p><?php echo $pageData['content']; ?></p> </div> <div id="right"> </div> <div id="Content_bottom"></div> </div> </div> <!-- END CONTENT --> <?php include 'Foot.inc'; ?> 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 https://www.neowin.net/forum/topic/930690-php-question/#findComment-593060030 Share on other sites More sharing options...
0 McCordRm Posted August 22, 2010 Author Share Posted August 22, 2010 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 https://www.neowin.net/forum/topic/930690-php-question/#findComment-593060330 Share on other sites More sharing options...
0 sweetsam Posted August 22, 2010 Share Posted August 22, 2010 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 https://www.neowin.net/forum/topic/930690-php-question/#findComment-593061206 Share on other sites More sharing options...
0 McCordRm Posted August 22, 2010 Author Share Posted August 22, 2010 Arg... I'm an idiot. I was trying to figure out how to pass the variable inside the code, itself. I changed my original link for About the Author to: http://www.richardmccord.com/index.php?page=Biography and it worked. Obviously, now I get it. Thanks. Link to comment https://www.neowin.net/forum/topic/930690-php-question/#findComment-593062600 Share on other sites More sharing options...
Question
McCordRm
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
https://www.neowin.net/forum/topic/930690-php-question/Share on other sites
22 answers to this question
Recommended Posts