A while ago, I wrote a post on how to create a JavaScript Style Switcher, but recently I found the need to create a PHP based one. There are many ways to do this in PHP using sessions, arrays, cookies, etc. I created one that only takes a few lines of code and is super easy to implement.
This PHP Style Switcher can be done in two easy steps.
Step One: Setting styles
The first step is to create a new file on your server named switcher.php and paste the following code into it:
<?php
/* expire in 1 year */
setcookie("sitestyle", $_GET['set'], time()+31536000);
header("Location: $HTTP_REFERER");
?>
When it’s queried, this file will write a cookie for the user detailing which site style to use. The cookie is set to expire in one year, and then the script sends the user back to the referring page. All of this happens behind the scenes, so the user will never really “see” switcher.php in action.
Telling switcher.php which site style to use is an equally simple matter. You can query it using conventional HREF links in text, images, or even flash
<a href="switcher.php?set=red">change to RED style!</a>
Step Two: Detecting styles
<link rel="stylesheet" type="text/css" media="screen"
title="User Defined Style"' href="<?php echo
(!$_COOKIE["sitestyle"])
?'default':$_COOKIE["sitestyle"]
?>.css" />
This tiny script detects which style is in the user’s cookies and prints out the result. If the user doesn’t have cookies enabled, or just hasn’t chosen a site style for themselves yet, the script will default to “default.css”.
So there you have it. A cross-browser, backwards-compatible, forwards-compatible, standards-compliant style sheet switcher in just five lines of code. It’s fast, straightforward, and universally accessible because there’s no processing done on the client side.
Here is the code (.zip) as well for you to review.
The technique of splitting up a information into multiple pages is known as pagination. For web pages it normally means taking a resultset from a database query and only showing a specific number of records per page. There are many variations of how to do this no matter what technology (PHP, ASP, JSP, Coldfusion) you use.
read more…
In my attempt to learn PHP I thought I would create a simple web site that does some basic database interaction (select, insert, update and delete), sorting and paging, keyword search, and use some of the new PHP 5 features such as OOP, exception handling and the new mysqli extension for MySQL 4.1 and above.
read more…
Sean Hull wrote two great articles describing the differences between the latest versions of PHP (ver 5) and ASP.NET (ver 2.0). The first article gave a good introduction to both technologies, while the second article went into more detail of advanced features such as OOP in PHP and the .NET framework in ASP.NET.
As I mentioned in a previous post, in order to develop PHP web pages on your local PC you need a web server installed. You basically have two choices: Use a software package to install everything you need or you can install them separately to get the greatest control over your development environment. For Windows users if you do not have the Professional edition that includes Internet Information Server (IIS) you need to down the Apache web server instead.
Configuring either IIS or Apache can be tricky and somewhat confusing so I did the research for you and give you detailed steps on how to configure either IIS or Apache to work with PHP.
read more…
Once you learn the basics of PHP, you will want to know how to interact with a database and update its data from a web page. MySQL is the most obvious and recommended choice using PHP and is fairly easy to setup.
You can use phpMyAdmin to easiy setup your MySQL database, tables and perform any necessary SQL statements.
read more…
In order to setup a proper development environment for your home PC to create and test PHP and MySQL web pages you will need at least three components:
read more…
Only recently have I started to learn PHP and am starting to realize what all the fuss is about. PHP has been around for 10 years and has gained steady increasing popularity.
read more…