Sitepoint Members,
I have a 3 column website. Isn’t there a way to use php includes so I can use one solitary includes file (not copies) to create the left column for all the pages and another includes file to create the right column for all the pages? This way one change in an includes file would make the change for all of the pages of the website.
stick <?php include_once(‘left.php’); ?> in where the left column should be (and then create a ‘left.php’, obviously)
But what you might want to consider doing is taking everything before your main content, put it into a ‘header’ file, take everything after your main content, and put it into a footer file.
Now, your main pages would look like:
<?php include_once('header.php'); ?>
<p>This is the main content page. Stuff goes here.</p>
<?php include_once('footer.php'); ?>
StarLion,
So if I create a file collage.html (where everything is assembled to make the page), I can write the peripheral files of includes as left.php and right.php and in collage.html I would call for these includes php files with
<?php include_once(‘left.php’); ?>
and
<?php include_once(‘right.php’); ?>
In order for the PHP code to be read, unless your server is very oddly configured, you should make it college.php instead of college.html. HTML files dont get fed through the PHP parser.
Include IS a structure call, btw, but the ()'s are indeed optional (as they are with echo(), etc). It is equivalent to if(). A structural element.
As far as the / or not /, the simple explanation is that without a /, the system will look in the file’s current directory for the include file. If you put the leading /, it will go to the siteroot, and then navigate to the file.
EX:
If i had a file located at /example/test.php…
include_once('foo.php'); //Looks for http://www.yoursite.com/example/foo.php
include_once('/bar.php'); //Looks for http://www.yoursite.com/bar.php
include_once('/example/cow.php'); //Looks for http://www.yoursite.com/example/cow.php
(it actually looks in the filestructure rather than http:, but the example holds)
There are multiple ways to template your PHP website. The include(“somefile.php”) is one way of doing this. You create multiple files, each consisting of a “block” of html that fits into your content page.
<!-- header.php -->
<h1>Header</h1>
<!-- sidebar.php -->
<div>Sidebar</div>
and so on.
get_something() is another way of doing the same. I’ve seen similar pattern used in Joomla. You can create one file containing all the blocks that you need to use on your content pages. Then you wrap the blocks inside functions, something like:
<!-- template.php -->
<?php function get_header() { ?>
<h1>Header</h1>
<?php } ?>
<?php function get_sidebar() { ?>
<div>Sidebar</div>
<?php } ?>
<?php function get_footer() { ?>
<p>Footer</p>
<?php } ?>
The second approach is more easier to manage but that is just my opinion.
WebdesignHouston, StarLion,
I think one file (not folder) containing all the blocks would would be fastest for a browser, instead of includes where I create several files.
So in my index.php file I have a bunch of calls that are used get the blocks to build the collage (the collage being the assembly of the different parts used to create the page.)
So we have
File: index.html
Inside index.php would be
<?php function get_leftcolumn() { ?>
<?php function get_rightcolumn() { ?>
.
.
.
}}
</body>
</html>
Is this right? What do I put for “function”? Are those close brackets at the bottom placed correctly? If these close brackets are actually placed outside this file, I probably won’t do it this way.
What is the structure of the one file containing blocks look like"
File: thegetfile.php
Inside thegetfile.php would be
<?php
code for left column
code for right column
.
.
.
?>
I’m working on getting rid of the CMS running my site. Everything can be changed to html and a few php files, but how can I keep my page address ending with “/”, as they do now, instead of “.html”? I’d like to avoid a long list of 302 redirects in my htaccess file.
If you want to see /somename/ instead of somename.html, rename somename.html to index.html and put it in a folder called /somename/. Whenever you point a browser to a folder, it looks for a file called index.html or index.php etc. If it finds it, you just see the folder name. It’s just like when you point a browser to the index page in the root folder. You see mysite.com, rather than mysite.com/index.html.
There are other ways to do the same thing, such as via a .htaccess file, but I prefer the above.
Ralph,
I was hoping to dump my CMS yet keep my ending slash. It looks like if I have 50 web pages I’m going to need 50 folders. Oh I see, the folder gets you /…/ and the index.html will gets you not showing index.html. I’ll have to think about it. I wonder if there’s a php way. Maybe I’m making a wrong assumption - will search engines look at /abc/ as the same as abc.html? I’m assuming not.
Not sure, but I don’t think so. I guess if you have that many pages, you might be better off taking the option to rewrite your URLs via Apache (if that’s what your site is hosted on). In a .htaccess file, you can formulate a rule that removes the .html extension, leaving just the file name. I guess you could add a slash at the end too. You’d need to consult the Apache forum for more info on doing that.