Saturday, December 27, 2014

Alternative ways to use PHP scripts.

Over the past 10 years I have used PHP quite extensively in my websites. Mostly I use php as a way to build each page pulling in files from around my server organising them and then displaying the finished result. It has worked very well most of the time.

There is a downside that I have noticed the more people visit my websites. The trouble revolves around the server hosting company where I host my websites. Occasionally the php will stop working, effectively leaving my website with blank or error pages. While the php scripts stop working the pure html pages that are stored on the server are still visible to visitors. Of course I could just create the html text files by hand and that would make sure this particular error doesn`t occur anymore, however with over 200000 pages to remake it is an impossible task. Impossible except of course I can use php to generate my webpages and store them as pure text files.

Usually a webpage made using php would look something like this in code

The below file would be saved onto your server with a name like helloworld.php



echo is the most basic/easy way of printing text to the browser window.

helloworld.php

An alternative way to use the php script would be to do something like this. I`ll save this as generatepage.php on my server.



generatepage.php
helloworld.html

So what we are doing here is making a string variable $str and we are assigning it with the same information as the helloworld.php program but this time we are saving it using the fopen, fwrite and fclose instructions to a file called helloworld.html Okay so you might be thinking that is just making one file to make another and that is true but the real power in this method is that I can create one php file that could generate millions of pages from hundreds of files.

When we are trying to make a website one of the aspects we want to replicate each page is the title bar, layout and page ending (faq links, about links).

This is quite easy by using either includes or file_get_contents

Save a file called mywebsitetitle.txt with the contents



and we shall save the following in the file generatepage2.php



generatepage2.php
helloworld2.html

And for the final example lets make a slightly more complex file that will generate multiple files. First lets make a list of files that we want to create minus the ending .html. We will call this mywebsitefiles.txt and will look like this

Now we will update our page generator to load this file and for each filename go through the same process effectively creating 3 new files with the same layout and title bar. We will call the generator generatepage3.php and it looks like this. generatepage3.php

mywebsiteindex.html
mywebsitefaq.html
mywebsiteaboutus.html

The key is the foreach loop which allows us run the same instructions many times. We can add titles, meta tags, descriptions, text files and images to create differenet pages but with the same layout we makes for a good user experience.

I hope this has been of use to you and if you have any questions don`t hesitate to ask!

Thursday, December 11, 2014

How to Build To Withstand the Weather

My wife is a Filipina. Most of her family live in and around Cebu and Leyte. You might remember Leyte from Typhoon Haiyan last year that killed over 10000 Filipinos. A sad day for sure.

My wife didn't have direct relations living there but she did have friends who lost loved ones.

The Philippines gets at least 20 typhoons a year not to mention earthquakes, floods, and many other natural occurances. I keep thinking about our future and wanting to live in Cebu but the natural disasters there are certainly something I have to consider.

I look at the types of buildings available from nipa huts which are just wooden houses on stilts to the more regular style of breeze block buildings with wooden upper floors. Most of the houses are built as cheaply as possible due to the low wages and cost of materials. One thing always hits me, there are no round/cylindrical buildings. When you think of modern house building in fact virtually nowhere in the world will you find round house styles which is strange really.

The round shape gives a much stronger design while costing on average only 88% of material costs. If you imagine a force pushing against a round wall the force doesn't act on one point but is partly transfered around the whole wall making it able to withstand impacts better than a flat face.


Admittedly a round house needs a slightly wider land to achieve the same area for example a 10 metre by 10 metre house gives 100 square metres of floor space where as an 11.283 metre diameter round house gives the same area and with land at a premium this is out of the question for many Filipinos however not for me. I would prefer more land and build for less.

There are other bonuses to building round houses such as the shape which allows water and wind to flow around it instead of meeting resitance on flat faced dwellings.



The wind is also is less likely to try to lift the roof on a round house as the shape naturally means wind pushes the roof downward.

The more I think about the idea the more I am certain I will be building this style of house. Two floors with a central concrete column that will be used to support the roof and also replace load bearing walls. I can have curved balconies and a spiral staircase.

The best part of course is the ability to withstand all the types of weather that the Philippines can throw at me.