{"id":1501,"date":"2003-07-25T17:01:52","date_gmt":"2003-07-25T22:01:52","guid":{"rendered":"https:\/\/www.quirkspace.com\/quirkblog\/?p=246"},"modified":"2003-07-25T17:01:52","modified_gmt":"2003-07-25T22:01:52","slug":"last-geek-of-the-week","status":"publish","type":"post","link":"https:\/\/www.quirkspace.com\/quirkblog\/?p=1501","title":{"rendered":"Last geek of the week"},"content":{"rendered":"<p>I promise this&#8217;ll be the last geek out I do this week.  If I post something tomorrow it&#8217;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&#8217;d like to give them something to make it worth their bother&#8230;<\/p>\n<p>Plus, sometimes I&#8217;m a total geek.<\/p>\n<p>Anyway, I&#8217;d just like to say &#8220;Wow Cool!&#8221; to whoever decided that PHP should be able to have dynamic variables&#8230;<br \/>\n<!--more--><br \/>\nI actually wrote a PHP script from the ground up that processes data from a form&#8230;  It determines what kind of file a client wants to download based on their selection of radio buttons and such&#8230; It looked all very simple until I realized that there were 40 possible combinations of results.  That&#8217;s a lot of If and Case statements&#8230;  Then I realized that there was an easy way to do it.<\/p>\n<p>I&#8217;m going to simplify things a bit for this example by cutting back on the number of possible results&#8230;<\/p>\n<p>The form passes a couple of values to the script: $Platform is &#8216;W&#8217; or &#8216;M&#8217; depending on if it&#8217;s a Windows or Mac file that the user wants, $Program is &#8216;A&#8217;, &#8216;B&#8217;, or &#8216;C&#8217;, for the program it&#8217;s made with, and $Size is either 1 or 2.  That&#8217;s 12 possible files.  In the PHP script, we create variables pointing to the name of each file&#8230;<\/p>\n<p><code><br \/>\n$WA1 = \"Windows, program A, size 1\";<br \/>\n$WA2 = \"Windows, program A, size 2\";<br \/>\n$WB1 = \"Windows, program B, size 1\";<br \/>\n<\/code><\/p>\n<p>&#8230; and so on.  Now we create a variable that contains the data passed by the form.<\/p>\n<p><code><br \/>\n$Filename = $Platform . $Program . $Size;<\/br><br \/>\n<\/code><\/p>\n<p>And this is how PHP lets us look at the value of the variable whose name matches the value of the $Filename variable!<\/p>\n<p><code><br \/>\n${$Filename}<br \/>\n<\/code><\/p>\n<p>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!<\/p>\n<p><code><br \/>\necho \"<br \/>\n&lt;html&gt;<br \/>\n&lt;head&gt;<br \/>\n&lt;title&gt;Automatic Download&lt;\/title&gt;<br \/>\n&lt;meta HTTP-EQUIV=\\\"Refresh\\\" CONTENT=\\\"1; URL=$Path${$Filename}\\\"&gt;<br \/>\n&lt;\/head&gt;<br \/>\n&lt;body&gt;<br \/>\n&lt;p&gt;Download should begin automatically in a few seconds.&lt;\/p&gt;<br \/>\n&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;\";<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I promise this&#8217;ll be the last geek out I do this week. If I post something tomorrow it&#8217;ll be cute pictures of birds or chipmunks or something, I promise. I put things like this in here because sometimes people get &hellip; <a href=\"https:\/\/www.quirkspace.com\/quirkblog\/?p=1501\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[26],"class_list":["post-1501","post","type-post","status-publish","format-standard","hentry","tag-technical-notes"],"_links":{"self":[{"href":"https:\/\/www.quirkspace.com\/quirkblog\/index.php?rest_route=\/wp\/v2\/posts\/1501","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.quirkspace.com\/quirkblog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.quirkspace.com\/quirkblog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.quirkspace.com\/quirkblog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.quirkspace.com\/quirkblog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1501"}],"version-history":[{"count":0,"href":"https:\/\/www.quirkspace.com\/quirkblog\/index.php?rest_route=\/wp\/v2\/posts\/1501\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.quirkspace.com\/quirkblog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1501"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.quirkspace.com\/quirkblog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1501"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.quirkspace.com\/quirkblog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1501"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}