Hi guys. I was wondering if I could use php to update my whole site? I have been directed to this in another post but I was wondering how I would go about doing this. For instance: Say I had a news box on all my pages, and I would like to update them without having to copy and paste on every page. I plan on growing a large website with hundreds of pages of content, so if I could update my whole site from one file then it would save me a lot of time! Thanks guys!
Provided that your host supports PHP, and you've taken the necessary steps (such as converting the content from static to dynamic) - yes. In this example, an include file would make the most sense. Let's say your current news looked something like this: SITE HAS BEEN UPDATED - Nov 22nd 2009, 4:15PM CST Hey guys, I just updated the site! -- Gilbert ------------------------------------------------------------ NEW PRODUCTS ADDED!! - Aug 13th 2009, 8:56AM CST I just added a new <a href="/products/view/doghouse.php">Dog House</a> as well as a new <a href="/products/view/sodamaker.php">Soda Maker</a>, check them out! -- Gilbert Code (markup): As it stands, it sounds as though that text would be copied and pasted on every page which displayed the current news. Instead of copying and pasting it, you'd create a new file, for example: my_updates.txt and inside of it, you'd store the above code. Then on the pages where you wish to display the news, you'd do something like: <?php echo file_get_contents("my_updates.txt");?> Once that's in place, to add a new entry - you'd just have to modify the text file mentioned above. There are better ways of displaying news, though. There's free (and paid) scripts that you can install on your site (with more than enough features) but in case you don't want to go that route, the above concept should work fine. You could also store the news entries in a database, but that'd require a little more work. If you clarify what you mean by "whole site", I could probably be more helpful. If you're just talking about updating the news part, then see above. Otherwise, explain the type of changes you'd be making (design related? content related? what?).
Thanks for taking so much time to answer my question! It was very informative, but I have a couple more. 1) how would I go about changing my website from static to dynamic? 2)Could I still use a template and set php include calls from inside specific divs? That way I don't have to change the appearance of my site by very much. 3)Do I have to use .php files for my website or can I still use html files?
I'm bored, so helping you helps me pass the time. It depends. Roughly how many pages does the site have right now? The answer to that question is pretty specific to your site. In general, anything that might be updated frequently (or at all regularly) you'd probably want to make dynamic. The news box we discussed above is a good example. Whereas something like the footer doesn't really have to be dynamic, since most of the time it won't change that often. What stays static and what becomes dynamic is entirely up to you, the entire site could be static and then just one or two things dynamic - it doesn't matter, the biggest advantage of making it dynamic is that it's easier to maintain. Yes, you can. Personally, the way I do it is: 1) Create a folder named pages. 2) Create a folder named includes. 3) Create includes/functions.php 4) Create includes/header.php 5) Create includes/footer.php 6) Create index.php 7) Create pages/main.php Then index.php is the only one that the user accesses. The site header/footer is automatically added on to the beginning and the end of each page (respectively) and index.php loads functions.php which determines what content to show. The functions.php file is also responsible for cleaning user-input, handling cookies/sessions, cleaning up the output, etc. The way the index.php file looks is usually something like: <?php // include main functions file require_once("includes/functions.php"); // include site header [logo, menu, css/js, etc] require_once("includes/header.php"); // determine what needs to be displayed, and display it show_content(); // include site footer [closing tags, copyright, footer links, etc] require_once("includes/footer.php"); ?> PHP: show_content() would be a function that exists inside of includes/functions.php - it determines what URL was requested (for example: http://fake.com/?page=faq) and loads the corresponding file (pages/faq.php) if it exists, otherwise it loads the default (pages/main.php). Of course, show_content() only accepts a-Z0-9 as the "page" parameter (and makes certain no NULL character or backslashes or periods or anything nasty exists) and if that check passes, then the page is loaded and displayed. This might all seem confusing or useless, but I'm just trying to give you a general idea as to how I do things. I can't say it's the best way, or that it's the only way - but personally, I think it's one of the easiest ways of doing it. What kind of webserver software are you running? Apache? If so, you can modify httpd.conf and add something like this: AddType application/x-httpd-php .htm AddType application/x-httpd-php .html That way, all .htm and .html files will be rendered as PHP. So if they contain PHP code, it'll be executed. Otherwise, the page will load as it normally would. Another answer: You can use both. It'd probably make more sense in the long run to have one extension (that way you don't end up with menu.php and menu.htm and not know which one is the correct one, as well as users trying to remember links to certain pages.. I can't speak for anyone else, but I personally remember lots of URLs [not just domain names] and I imagine it'd be a little bit harder to do if I had to also remember different extensions for different pages on the same site).
If you are not sure, how to make the site static to dynamic using php, you can try to ask a freelancer to provide the service and explain what he did. Another way can be using a cms like joomla or wordpress, which will make your life even easier in the long run.