Any problem if i will have only index.php instead of - php

i am trying to create a website, a dynamic one.
i wanted to know if is any problem if i will not divide the index.php file, so it will have the header , footer, sidebar etc, in one file. is any thing wrong here? So instead having, header.php, index.php, footer.php, sidebar.php, i will create only index.php..
need some suggestions from experts please
Thank you for reading this post.

absolutely nothing wrong. well. at the start.
you'll have issues with maintainability of that single file later when it gets bit. if you're planning to grow big.

No there is nothing wrong in that approach, it might be a pain to maintain though.

It's almost always a good idea to split things up this. My typical "simple" site layout has a header, a footer, and a body, each with its own files. It's rare for more than a couple pages to have different headers/footers, so almost every page ends up looking like:
<?php include('header.inc'); ?>
body goes here
<?php include('footer.inc'); ?>
And the header/footer files boil down to <html><body> and </body></html> respectively (with all the usual css/javascript/meta/headers, etc... of course).
Nothing wrong with keeping everything in a single file, but then you'd have to hard-code your menus/sidebars/footers/headers/etc... into every PHP file.

You need not any additional files.
This files are used only for easy edit, more obvious struct and having main code in different pages

I think it's great. Since your PHP-processor only needs to interpretet 1 file (instead of 4), it'll probably go a lot faster (when you are not using any OP code compiler).

Segregating your code is good practice as it helps prevent duplicate code. Say you want page2.php and you need the same header, footer, sidebar, etc. Are you going to copy/paste in that code? Just make your template views like they're supposed to and piece together your structure for optimal code redundancy avoidance.
If you NEVER plan on scaling this project, one page is fine.

While there is nothing wrong theoretically, this is by definition a bad practice.
By separating functional / organisational wholes into different files / classes / modules, you achieve decoupling and get a larger control level over your application, it will be much easier to maintain, you wont have to harcode and copy/paste much etc...
Why would you want to put all of your code into a single file?

Related

How to include a site-wide banner?

I realize this question is similar to this one but there didn't seem to be a definitive answer there. Currently I'm using <?php include "banner.html"; ?> but in order for this to work I need to change the ext of ALL pages to .php instead of .html.
I don't think this is really best practice. I'd prefer to keep my basic webpages as html files and reserve php files for server-side logic - isn't that the way it's supposed to be??
The banner should be displayed at the top of each page by default as soon as the page loads, so from what I've read jquery.load() is not appropriate as that should typically be triggered by user input. This is not an ASP.NET project so master pages is not an option. Also heard about HTML templating but after googling it I walked away more confused than ever.
I don't think this is really best practice.
It's fine, especially for small sites.
I'd prefer to keep my basic webpages as html files and reserve php files for server-side logic - isn't that the way it's supposed to be??
Keeping business logic and view logic apart is a good idea, but you don't need to go so far as to ban .php extensions on files that don't include business logic.
You might want to investigate the MVC pattern and the approaches taken by frameworks such as CodeIgniter or CakePHP if you want to see how other people keep their logic and HTML separate.
Either go ahead and convert the files to .php (totally fine practice), or use file_get_contents rather than include to get the HTML content:
<?php echo file_get_contents('banner.html'); ?>

Is it bad to include header and footer as a template?

I was wondering if it's the right thing to include the tags like etc via another page.
What I mean by that is:
<?php include_once("header.php")?>
The content for each individual page
<?php include_once("footer.php")?>
Header.php contains:
<!DOCTYPE html>
<head>
<title>My Website</title>
</head>
<body>
Footer.php contains:
</body>
</html>
I am using it that way because when I have a lot of pages, it's a lot easier to just change one/two page(s). That way I spare time.
But the question is, is it bad to use a "template" style for my website?
( I saw some similair questions like this one but they didn't really answer my question, that's why I started a new topic - Sorry if it's wrong )
No problem with this I think - it's certainly better than duplicating the same content across multiple pages. Don't Repeat Yourself is a core tenant of programming, and this approach helps in that regard. The performance impact of the PHP includes is absolutely minimal and not worth worrying about, compared to advantage of easier maintenance.
You might be able to go further still though. Do you really need to repeat the includes on every page ? If all the pages have identical headers and footers, then you could make just one page that has them, and an area for dynamic content in the middle instead (this is the approach ASP.NET takes).
No, this is great. You will actually find there are more and more things you would like to move into included files, for example the sidebar for your website could be in its own file. This is exactly what PHP what made for, to allow for easier maintenance of your website.
Any PHP will be slower than only HTML. For example people use cacheing plugins for wordpress sites, so just HTML will be requested and the php server wont have to work as hard, and the page will be quicker. But the benefits of using PHP clearly outweigh this with the popularity of wordpress.
All php calls are made once, so multiple calls of php is not like multiple calls for css files or JavaScript.
As the site grows you will be happy to make one change that will affect all pages, rather than have to change each page. A simple example is a seasonal greeting in the header. Simple to ad and take away with your template. not so without. And that is before the more obvious link changes.
This is a good direction, just don't repeat yourself. Anyway, to get some more robustness from this templating idea you can try to take a look on the following libraries:
Twig : http://twig.sensiolabs.org
Smarty: http://www.smarty.net

Update multiple pages

I am doing some freelance work for a client and I need to re-code an old menu. The entire site is static which will make this process extremely slow and redundant, does anyone have a good technique for updating multiple pages automatically?
The old developer used "Allwebmenus" which is a automatic menu creation tool. It is implemented by using JavaScript which writes HTML to the DOM. I'm going to replace this with a clean html menu and some simple jQuery.
Right now I think the best way is to create a separate .html file with the menu code, and use PHP includes on all the pages but this still requires me to update every page on the site. Can anyone give me better idea? Or do you think this is the best option?
Thanks for the help!
create menu.php and include("menu.php") into each file where the old menu's are written.
It will make your life easier going forward too.
As far as fixing all the static pages, you will have to go in and do that yourself.
include("menu.html");
You can use includes, but it might also make since to put them onto a CMS like Drupal. Handles a lot of that for you.
Using the PHP includes is a good method. If the "Allwebmenus" has Javascript code on each page, you'll have to edit each file anyways, so adding the includes is no big deal.

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

A client wants me to do CSS coding (only) but doesn't want to provide me the php files

I have a client who wants me to do CSS coding only, but doesn't want to give me the php files.
Right now, I just have access to the live website (with no CSS).
It is entirely made with tables and I want to use divs instead
I'm not sure if it is possible to do the coding
I thought about copying and pasting the generated HTML code from each page
Will this cause possible problems with the end result?
Yes, this will cause huge problems: you'll do an awesome job, client will have trouble integrating it with their site, client will abandon your awesome work.
IMO, you should let the client know that you'll do the best you can with what they have given you, but you would be able to save them a lot of work and do a better job if you could have access to the source code.
If you know that you can't make the client happy with what they have given you, though, it would be doing everyone a disservice for you to try.
If you absolutely can't convince them to give you access to the source, then this client sounds stupid:
He has a layout which is table based.
He wants you to magically make it look better with CSS, without having access to the source.
"#Phoenix I don't see any classes or IDs." - there are no classes or ids to hook into.
You might be able to do it if you used some CSS3 selectors to, for example, select the 3rd td inside a td inside the 2nd table to apply styles to ;)
But, that won't help if you have to support older browsers, which makes this impossible at the moment without doing something differently.
I don't have full knowledge of your situation, but here's what I would probably do (if I couldn't convince them to give me access to the source):
Open the live site.
Copy the HTML source code.
Paste it into a new local file.
Add this into the <head> section: <base href="http://the-clients-site.com/" />.
This will let all the assets on the page load from the client's actual site.
Now, you have something to work with.
You have to keep track of ALL changes you make to the file.
The first change should be adding your own blank style tag.
Then, you can add id and class to whichever elements you feel need it.
You should try to avoid moving around elements, unless it's absolutely required. Those changes are a whole lot harder to explain to someone. I know from experience.
You should be able to style the page properly now.
Then, you deliver the completed page, and the documented list of changes you had to make to the HTML (add id, here add class there).
The client should then be able to integrate the changes into his site.
Well, at a bare minimum they'll need to modify ther PHP to reference your CSS. More importantly, you need to be able to hook your CS up to elements - Do tables/rows/etc. have Ids or classes attached?
If they are clever and have some good separation between code and presentation (using a templating engine or similar) then you can probably just edit the template / css.
If they won't let you edit the PHP and you come up with a new awesome layout, they will have a nightmare job trying to integrate it and probably won't bother.
I don't see the problem. You can style tables just as easily as divs. You don't have to know how the wall is built to know how to paint it, which is pretty much all you've been hired to do. Only problem I could see would be if they haven't added any classes or ids to the elements yet. After all, what the browser/client sees is the only thing that needs styling, and since you can see everything that the browser sees, you can see everything that needs styling.
If they have added classes/ids, then just take a copy of a page and style it in a testing area, and then once it looks nice, you take a copy of another page and make sure it looks nice with it too, add to the CSS if there are any new unstyled elements that didn't exist on the first page, once it looks nice, then move on to another page, and another repeating the process until you are satisfied that it appears that every page within reason would look nice with it.
If they haven't added classes/ids, tell them they need to in some capacity before you can work on it, perhaps provide some guidance on the issue.
I'm actually doing this right now for SO.
I'm working on a userscript that provides an alternate "clean" stylesheet for the StackExchange network. I have no access to the SO engine. I am using the Chrome Inspector to look at how the elements are set up. I recommend the same. (Although it is a little different, since I'm modifying the original CSS file.)
You can easily identify what you want to style with the Inspector and then work from there. I would suggest that you ask your client for a list of classes and IDs though. (I got that in the form of an existing stylesheet, you can go about it in a different way, if that suits you and your client.)

Categories