A friend taught me something cool today that I thought that I would share with the group. With simple modular division, you can test for evens and odds and “cycle” between different results.
Follow me through the following scenario. My actual code for this was done in SMARTY, but whether done in a script or the template, it applies the same.
I had an array of values…
$array = array(‘A4′,’A6′,’A8′,’TT’,'allroad quattro’);
I wanted to loop through them and print out some HTML along with each value in the array. A simple foreach takes care of this nicely, but I needed something more. I wanted to print out a different set of HTML for every other value. In other words, the loop progresses and the values in the array are printed out with/into the HTML, the first would be printed out with/into “HTML chunk A”, then the second with/into “HTML chunk B”, then the third with/into “HTML chunk A”, then the forth with/into “HTML chunk B”, and so forth through for all the values. I needed a way to say “this… then that… then this…. then that.”
My friend introduced me to modular division. It does some math and returns the remainder of the fraction, making it easy to detect odds or evens. Each item in the array already has a numerical value assigned to it automatically in the array (the key), so I decided to use that to compare against.
So, if my code were in PHP, it would look something like this…
foreach ($array as $key => $value)
{
if ($key %2) // divide by two; ie, every other
{
echo ‘HTML chunk A’ . $value;
}
else
{
echo ‘HTML chunk B’ . $value;
}
}
And, using this modular division, you would be able test for more than two possibilities as well.
Hope this helps someone,
wade