Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Help with a function

I'm trying to create a small function which generates option values and the corresponding text for my website backend to manage software releases. I know this is easy, but I can't figure out how to go about it. Below is the code. The end result should be every number between 1.0.0 to 3.9.9 to monitor builds / releases.

function version_automater() { $v = 3; $sv = 9; $bv = 9;

for ($fv = 1; $fv <= $v; $fv++ ) {
    (string)$string = $fv;
    for ($fsv = 0; $fsv <= $sv; $fsv++) {
        $string .= "." . $fsv;
        for ($fbv = 0; $fbv <= $bv; $fbv++) {
            $string .= "." . $fbv;
            echo '<option val="'. $string . '">' . $string . '</option>';
            $string = "";
        }
        $string = "";           
    }
    $string = "";
}

}

2 Answers

Managed to figure it out. Here's the code if you find it useful. It might not be super efficient, but it groups versions, subversions, and build versions in to the notorius x.x.x format. the function takes a minimum release version and a maximum release version and maps out all the subversions and buildversions automatically.

<?php
function version_automater($minReleaseNum, $maxReleaseNum) {
    // End result $v.$sv.$bv
    $v  = $minReleaseNum;
    $sv = 0;
    $bv = 0;
        (string)$string = "";
        do {
        if ($bv <= 9) {
            $string = $v . ".".$sv."." .$bv;
            $bv++;
        } else {
            $sv++;
            $bv = 0;    
            if ($sv >= 9) {
                $sv = 0;
                $bv = 0;
                $v++;           
            }
        }       
        echo '<option value="'.$string.'">'.$string.'</option>';
    } while ($v <= $maxReleaseNum);
}
?>

Hi Travis Coats,

Glad you got it sorted. Nesting for loops is sometimes just part of the game, you can look in to custom iterator classes if you want to go further into a better way to manage this.

Also, here is a way to solve just the list (not really the answer) using good ol math.

<?php
$versions = array();
for($version = 1; $version <= 3; $version = $version + .01){
    //remove our decimals (for now)
    $str_version = str_replace(".", "", (string)$version);
    //pad our string with zeros
    $str_version = str_pad($str_version, 3, "0");
    //add back in our decimals how we want them in two steps
    //step one, split all the numbers
    $chars = preg_split('//', $str_version, -1, PREG_SPLIT_NO_EMPTY);
    //step two, glue them back together with some decimals
    $str_version = implode(".", $chars);
    //add this version to our array of versions
    $versions[] = $str_version;
}
?>

<select>
    <?php foreach ($versions as $v) {
        echo "<option value='{$v}'>{$v}</option>";
    }?>
</select>

It is just a nice way of looking at the problem differently.