Location: PHPKode > scripts > Roof Pitch Calculator > roof-pitch-calculator.php
<?php
/******************************************************************************************************
This calculator is absolutely free, provided by ShedBuilder.info. 
There is a link to our site in it and we'll appreciate if you don't remove it, 
but this is not a requirement. You can remove the link if you wish. 
Feel free to link to our site or some articles on it that you find useful.
*******************************************************************************************************/
?>
<style type="text/css">
#rpCalculator {
    width:280px;
    padding:5px;
    border:1pt solud black;
}

#rpCalculator form label{
    display:block;
    float:left;
    width:130px;
}

#rpResults {
    width:280px;
    padding:5px;
    border:1pt solud green;
    background:#f0f0f0;
}
</style>
<?php
/* perform calculations */
if(!empty($_POST['calc_ok']))
{
    // calculate pitch
    $pitch=round( ($_POST['rise'] / ($_POST['width']/2)) * 12 );
    
    // in degrees
    $degrees=array(1=>4.5, 2=>9.5, 3=>14, 4=>18.5, 5=>22.5, 6=>26.5, 7=>30.5, 8=>33.75,  
        9=>37, 10=>40, 11=>42.5, 12=>45);
        
    $deg=@$degrees[$pitch];
    
    // Pythagorean theorem
    $gable=round(sqrt($_POST['rise']*$_POST['rise'] + ($_POST['width']/2)*($_POST['width']/2)),2);
    
    // cost calculation requested?
    $calculate_cost=false;
    if(!empty($_POST['length']) and !empty($_POST['cost']))
    {
        $calculate_cost=true;
        
        // calculate area
        $area=$gable*$_POST['length']*2;
        
        // cost
        $final_cost=$area*$_POST['cost'];
    }
}
?>
<div id="calculatorWrapper">
    <div id="rpCalculator">
    <form method="post" onsubmit="return validateRPCalc(this);">
    <fieldset>
    <legend>Roof Pitch Calculation</legend>
    <div><label>Roof Width (Base):</label> <input type="text" name="width" value="<?php echo @$_POST['width']?>" size="6"> ft</div>
    <div><label>Roof Rise:</label> <input type="text" name="rise" value="<?php echo @$_POST['rise']?>" size="6"> ft</div>
    </fieldset>
    <fieldset>
    <legend>Area/Cost Calculation (optional)</legend>
    <div><label>Roof Length:</label> <input type="text" name="length" value="<?php echo @$_POST['length']?>" size="6"> ft</div>
    <div><label>Cost per sqft:</label> $<input type="text" name="cost" value="<?php echo @$_POST['cost']?>" size="6"></div>
    </fieldset>
    <div style="text-align:center;"><input type="submit" value="Calculate"></div>
    <input type="hidden" name="calc_ok" value="1">
    </form>
    <p style="text-align:center;margin:0px;"><a style="font-size:11px;" href="http://shedbuilder.info">By Shed Builder</a></p>
    </div>    
    
    <?php if(!empty($_POST['calc_ok'])):?>
        <div id="rpResults">    
        <p>Your roof pitch is <b><?php echo $pitch?></b>.</p>
        <?php if($pitch>=1 and $pitch<=12):?>
            <p>This means your roof slope is <b><?php echo $deg?>&deg;</b></p>
        <?php else:?>
            <p>Because pitch is out of <b>1 - 12 range</b> we can't calculate slope in degrees.</p>
        <?php endif;?>
            <p>The gable of your roof is <b><?php echo $gable?></b> ft</p>
        <?php if($calculate_cost):?>
            <p>Cost of your roof: considering cost of <b>$<?php echo $_POST['cost']?></b> per sqft and that 
            your roofing area is <b><?php echo $area;?> sqft</b> total, the cost of this roof will 
            be <b>$<?php echo number_format($final_cost);?></b>.</p>
        <?php endif;?>
        </div>
    <?php endif;?>
</div>

<script type="text/javascript">
<!--
function validateRPCalc(frm)
{
    if(frm.elements['width'].value=="" || isNaN(frm.elements['width'].value))
    {
        alert("Please enter your roof width, numbers only!");
        frm.elements['width'].focus();
        return false;
    }
    
    if(frm.rise.value=="" || isNaN(frm.rise.value))
    {
        alert("Please enter your roof rise, numbers only!");
        frm.rise.focus();
        return false;
    }
    
    if(frm.length.value!="" && isNaN(frm.length.value))
    {
        alert("Please enter only numbers for roof length or leave empty!");
        frm.length.focus();
        return false;
    }
    
    if(frm.cost.value!="" && isNaN(frm.cost.value))
    {
        alert("Please enter only numbers for cost or leave empty!");
        frm.cost.focus();
        return false;
    }
}
//-->
</script>
Return current item: Roof Pitch Calculator