Last geek of the week

I promise this’ll be the last geek out I do this week. If I post something tomorrow it’ll be cute pictures of birds or chipmunks or something, I promise. I put things like this in here because sometimes people get here by doing Google searches for PHP stuff and I’d like to give them something to make it worth their bother…

Plus, sometimes I’m a total geek.

Anyway, I’d just like to say “Wow Cool!” to whoever decided that PHP should be able to have dynamic variables…

I actually wrote a PHP script from the ground up that processes data from a form… It determines what kind of file a client wants to download based on their selection of radio buttons and such… It looked all very simple until I realized that there were 40 possible combinations of results. That’s a lot of If and Case statements… Then I realized that there was an easy way to do it.

I’m going to simplify things a bit for this example by cutting back on the number of possible results…

The form passes a couple of values to the script: $Platform is ‘W’ or ‘M’ depending on if it’s a Windows or Mac file that the user wants, $Program is ‘A’, ‘B’, or ‘C’, for the program it’s made with, and $Size is either 1 or 2. That’s 12 possible files. In the PHP script, we create variables pointing to the name of each file…


$WA1 = "Windows, program A, size 1";
$WA2 = "Windows, program A, size 2";
$WB1 = "Windows, program B, size 1";

… and so on. Now we create a variable that contains the data passed by the form.


$Filename = $Platform . $Program . $Size;

And this is how PHP lets us look at the value of the variable whose name matches the value of the $Filename variable!


${$Filename}

ta da! With all of the files in the same download directory (called $Path or something equally original), we can just have the script return an HTTP redirect that points to the file!


echo "
<html>
<head>
<title>Automatic Download</title>
<meta HTTP-EQUIV=\"Refresh\" CONTENT=\"1; URL=$Path${$Filename}\">
</head>
<body>
<p>Download should begin automatically in a few seconds.</p>
</body>

</html>";

Leave a Reply

Your email address will not be published. Required fields are marked *