including header.php and footer.php automatically - php

I have been trying to search a solution online and on stackoverflow but did not come across any satisfactory solution.
I wanted to know how can i include (if required) header.php and footer.php automatically in the new PHP pages that i create, at present whenever i create a new PHP file i manually add header.php on top and footer.php at the bottom, I dont really have a problem with doing this but i was wondering if this is how everyone does it and if not what is the best solution to this.
I know I could takeup any framework out there and use that because may be they include the header and footer automatically, but i am referring to custom PHP coding.
I will really appreciate views of experienced developers out there.

The usual approach is to use the front controller design pattern.
Use a single PHP file to load all the pages. Have it include the header and footer and use the URL (and whatever other information you like) to determine which content to include.
PHP developers often use mod_rewrite to map URLs onto a single script. You could also use the Apache alias directive.

Related

Adding jQuery code in Joomla

I need to add this code to Joomla please
$(".item-204").append('<ul class="nav-child unstyled small"><li class="item-205"><a href="index.php?option=com_content&view=article&id=9" >رؤيتنا</a></li>');
what is the best way?
One way to do this would be to use Regular Labs ReReplacer which could be set up to replace the .item-204 code with .item-204 plus any additional code you require. I am assuming .item-204 is unique on the page.
The advantage of using this extension is that you don't have to find or update the core code. Updating core code is often considered bad practice as any future template or extension updates can undo your changes.
It's really just about adding this code to every page? Instead of adding another plugin so your site you can put your code in a script tag located in your templates main php file. This file is named index.php and is located here: /templates/[your template name]/index.php
If you want to do it even better, place this code in the templates JS file. Some of them have a file like /templates/[your template name]/js/custom.jswhich is a good place to add custom JS to.

Best way of loading content that should appear on every page

Until now, I've been using the <iframe> tag to load things like headers/footers/navbars into my webpage. These cause so much hassle though and as I'm about to start building a new site I thought I'd get it sorted now.
I was thinking of having all the html code in a php file and just loading it in dynamically.. Ideally I'd like the code to become a part of the page. So it appears inline. But I also want to be able to edit one single file if I need to change one bit rather than editing the same file 100 times.
<iframe>'s did this well until recently and I don't want to use workarounds to solve my problems. Could someone please post some code I could adapt or post a link to something that tells me how to do this? Cheers
You can use PHP's include() function to include elements like headers and footers in your pages.
So:
include('header.php');
. . . will look for a file called header.php in the same directory and include it in your page. Then you just need to write this at the top of your pages.
That said, this isn't really a very good way to go about designing your site. How about looking for a content management system, that allows you to keep the design and content of your site separate?
Are PHP includes what you're looking for ? http://php.net/manual/en/function.include.php

Designing a content delivery system using PHP

Currently, I have a system that actively generates the pages on my site using PHP, more or less like so:
index.php ------include(query.php);-----> query.php grabs content from a file that corresponds to index.php
Query.php simply assembles the page from the mentioned index.php file and from header, footer, and navigation files.
The index.php file acts as a sort of proxy or label if you will so that when users visit the site, instead of having urls like "query.php?page=index" they have real pages.
The issue of course is that this is a bit convoluted. For each page of the site, I need 2 files: the "wrapper" file (such as index.php) and a content file to which it corresponds. I'd like to only be using a single file. The issue is that the single file should only contain the content of the page - not structure, header, footer etc.
So, what I'd like to be able to do is to be able to have index.php contain ONLYand a paragraph for example. When it is accessed, somehow PHP kicks in and applies a template and the header and footer.
Is PHP too high level a language to be able to do this? I know it is often done with Tomcat and java servlets, but I thought it would be cool to do with PHP.
EDIT: Important point, I don't want to use GET variables.
It's a bit hard to tell what you're trying to do, but have you looked into using a framework such as Kohana or Synfony? This will do pretty much exactly what you're asking.
If you're looking for a good template system, I suggest PHPTAL.
Failing that, it doesn't sound like you need to do anything that special. On the index.php page, why not just include header.php, the content, and footer.php? Short of using auto_append_file and auto_prepend_file, you can't add content to the page that is not explicitly in the code.
It sounds like what you want to do is route all requests through a single point (like frameworks do). Let's call it main.php.
On main.php you would have:
include header.php
include $_SERVER['PATH_INFO'] . ".php" //Requested file from URL. TODO handle this better
include footer.php
Then you would route all requests (using your server configuration) to "/main.php/pagename." Then pagename would only need its respective content.

Prestashop PHP Include in a .tpl does not work

I am creating a storefront for a client using Prestashop. Prestashop uses Smarty .TPL files. I read through smarty documentation and scoured the web but all of the suggestions are not working.
I first made a site using regular .php pages and I am including the header.php on every page.
I then made a directory for prestashop and got it set up. I edited the header.tpl file and was able to hard code in the header.php code. The problem with this is; when I want to edit the header (nav bar, images, social media), I will have to edit it in two different places. So I tried to "Include" my header.php file.
Though, when I try using smarty's {include_PHP "file.php"} and/or {PHP}include...{PHP}, Prestashop errors out and gives me a blank white page - no errors are given - (in chrome it gives me a "server error") until I take out the includes.
I tried replacing the entire header.tpl code with a smarty include and another piece of code that had a header hook, but none of these worked. Any suggestions? I just want one header where I only have to edit it once to make changes.
Using Prestashop v 1.4.4.0
Edit: I changed allow_php to true from false. Now it's trying to add the file, though it says it can't find the file. I placed it next to header.tpl and just used:
{php}
include('navBar.php');
{/php}
ANSWERED!
When using Smarty .TPL files, when you include something, you are not including from the path of the file you are working on. You are including from where the index is.
Example:
I am working on header.tpl, this is located in:
siteroot/smartyinstall/themes/themename/header.tpl
When the include is looking for the file, it is actually looking for it in the smarty root folder because the header.tpl is being pulled into the index.html page that is in the smartyinstall folder.
So, you have to go from there. In my case, the header that I was trying to include was in:
siteroot/includes/navBar.php
so, I had to write include('../includes/navBar.php');, only going up one directory, instead of four.
I hope this helps everyone that has a problem like this!
It's considered very bad practice to include php in smarty .tpl files so I would strongly recommend you don't add code this way. One of the main reasons for disabling the {php} tag is to help prevent code injection attacks. eCommerce sites are by their very nature natural targets for exploits.
A better approach would be to override the FrontController class to assign your custom code to a smarty variable - this could then be inserted into the header.tpl without resorting to using a php include().
With the class and controller overrides available in Prestashop 1.4.x there's no real reason you should need to resort to hacks and/or modifications to the core distribution.
Paul

How to use two Wordpress templates at once... or something similar?

I'm building a wordpress template with a lot of javascript, so I am also setting up a fallback version what requires different php in the index file of the template, and preferably, different header files.
The best way to do a fallback seems to be setting a javascript redirect on the "non-javascript site" to go to the "javascript version" of the site.
I am a bit confused on how the index.php file from the template folder trickles all the way down to the root directly. Yes, a understand php includes, but don't know how to jam a different file down the wordpress pipeline.
I was thinking I could do a redirect to mywebsite.com/index2.php but then I realized I didn't know how to get that to work.
Any ideas on how to do this?
It isn't the index.php that handles what template you use. Is a field in the database.
You would need to create a new theme that has both and switches them based on a session variable.
I am guessing this is nontrivial.

Categories