If it's good enough for Facebook, it's good enough for me!
I think of PHP as VB for the web. It works well, it's easy to
learn, you can do anything you need to do, you just need to be
careful.
Like VB, PHP's simplicity makes it really easy to write really,
really bad code. Don't let the simplicity allow you to disregard
good engineering discipline. For example, I recently re-wrote a
LAMP based website for a client, nearly 100 pages were on the site.
The median number of lines of code in the PHP files when I started
was around 500 LOC, when I was done the median was 4 LOC.
The DRY principle applies to PHP just as much as it does to any
other language, but it seems to overlooked far too often.
PHP random div loader
I only spent about 5 minutes searching for a simple and free PHP
Random div loader before inspiration hit me - as it turns out, it is
crazy simple to load random div's in PHP.
To be clear, I didn't want to randomly load just one of the
div's, I wanted the order of the div's to be different
for each page load. However, this could easily be adapted to
randomly load a single div.
-
Step one - put the div's that you want to randomize into
separate include files, database records, or whatever source is
easiest for you. In my case, I already happened to have them
in separate files so I went with the approach of having the
div's load randomly from separate PHP files.
-
Step two is to write this simple PHP script (name it
random-div.php):
<?php
$includes = array(
"random-div-1.php",
"random-div-2.php",
"random-div-3.php",
"random-div-4.php",
"random-div-5.php",
"random-div-6.php"
);
shuffle($includes);
foreach($includes as $include){
include($include);
echo "<div class=\"line\">";
}
?>
-
Step three, include new script in your PHP page
- Step four...relax and enjoy your handy work.
Obvious adaptations include pulling the div's from the
database, selecting only one div by replacing the foreach with
include($include[0])
Assuming the shuffle implementation is sufficiently random, always
selecting the first element in the array should also be sufficiently
random.