Source of Cut List Formatter


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Cut List Formatter</title>
<style type="text/css" media="all">
    body,a,p,ul { font-family: Arial, sans-serif; }
    img { border: 0; }
    h1 { font-size: 145%; }
    h2 { font-size: 100%; }
    h3 { font-size: 120%; }
    
    .red
    {
        padding: 2px;
        background: #c00;
    }
    
    .float-lt
    {
        float: left;
        padding: 0 0 0 25px;
    }
    
    .float-rt
    {
        float: right;
        padding: 0 50px 0 0;
    }
    
    .clearall { clear: both; }
</style>
</head>
<body>
<h1>Cut List Formatter</h1>
<h2>Create a collated list of properly rounded fractional <br />
numbers from a random list of decimal numbers</h2>

This is a handy little form that can turn a bunch of random notes into a sorted, collated list suitable for a cutting list or a purchase order. Or whatever.
<br />
<ins><hr /></ins>
<div class="float-lt">

<?php
$radio = $_POST['radio'];   

// set default at 1/16
if ($radio == 0) {$radio = 16;}

$my_text = $_POST['my_text'];
if (strlen ($my_text) > 6000){$stop ="stop";}
else {
$my_text = preg_replace("/[\sA-Za-z-_\/|()@{}\[\],;:\'\\\"]+/", "\n", $my_text);
$array1=preg_split("/[\s]+/", $my_text);
$array1 = array_filter($array1, strval);     //get rid of zeros
$array1 = array_filter($array1, is_numeric); //get rid of strings
rsort($array1); //sort it

$my_text2 = implode("\n", $array1);
}

if ($stop == "stop") {$my_text2 ="maximum length exceeded

try again with less than 6000 characters total

(including spaces and returns)";}
if (empty($my_text)){$my_text2 = "
Replace this with some numbers.

Or see what happens when you click \"create list\"

1.01 1.02 1.03 3.4 5.6 .7 .9

3.14159";}
?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">

<h3>enter your numbers</h3>
<div class="red"><textarea name="my_text" rows="12" cols="25" id="my_text"><?php echo $my_text2; ?></textarea></div>

<h3>fractional precision</h3>
<input type="radio" name="radio" value="1" <?php if ($radio ==1){echo "checked=\"checked\""; } ?> /> no fractions<br />
<input type="radio" name="radio" value="2" <?php if ($radio ==2){echo "checked=\"checked\""; } ?> /> 1/2<br />
<input type="radio" name="radio" value="4" <?php if ($radio ==4){echo "checked=\"checked\""; } ?> /> 1/4<br />
<input type="radio" name="radio" value="8" <?php if ($radio ==8){echo "checked=\"checked\""; } ?> /> 1/8<br />
<input type="radio" name="radio" value="16" <?php if ($radio ==16){echo "checked=\"checked\""; } ?> /> 1/16<br />
<input type="radio" name="radio" value="32" <?php if ($radio ==32){echo "checked=\"checked\""; } ?> /> 1/32<br />
<input type="radio" name="radio" value="64" <?php if ($radio ==64){echo "checked=\"checked\""; } ?> /> 1/64<br />
<input type="submit" value="create list" />
<input type="hidden" name="MAX_FILE_SIZE" value="300" />
</form>
</div>
<div class="float-lt">

<?php 
if ($array1[0] > 0)
{
echo "<h3>Collated List</h3>";
}
else 
{
echo "<h3>Instructions</h3>
Enter some decimal numbers in the box at <br /> 
the left, separated by commas, tabs, or <br /> 
most anything you want. All text will be <br /> 
removed and separators replaced with <br /> 
newlines. There is a  6000 character <br />
maximum, including separators. 
<br /><br /> 
Enjoy
";
}

// fraction function
function frac($num) { $mod=fmod($num,1)*64; 
if (1 & $mod) { return " - ".$mod."/64"; } 
else $mod=$mod/2;
if (1 & $mod) { return " - ".$mod."/32"; } 
else $mod=$mod/2;
if (1 & $mod) { return " - ".$mod."/16"; } 
else $mod=$mod/2;
if (1 & $mod) { return " - ".$mod."/8"; }
else $mod=$mod/2;
if (1 & $mod) { return " - ".$mod."/4"; }
else $mod=$mod/2;
if (1 & $mod) {return " - ".$mod."/2";}
}

if ($array1[0] > 0)
{
echo '<table>';

// round the values
array ($array2);
while ($key = current ($array1)) {
  $key = strval(intval(round($key*$radio)) / $radio);
  next ($array1);
$array2[] = $key;
}

// collate and make fractions
$array2 = array_count_values ($array2);
krsort ($array2);
while (list ($key, $val) = each ($array2)) {
echo "<tr><td>$val</td><td>&nbsp; @ &nbsp;</td><td align=\"right\">".intval($key)." </td><td> ".frac($key)." </td></tr>";
}
echo '</table> ';
}

echo "</div>";
 ?>
<br class="clearall" />
<div class="float-lt">
<br />
If you want to see the ugly code I wrote to make this happen, <a href="frac.html">here it is</a>.
</div>
</body>
</html>