<?php
//====== CONSTANTS AND ARRAY BUILDS======//
define("DOCROOT", "/home/screevo/amt/");        #The root directory that all files are in
define("IMAGEDIR","/home/screevo/amt/images");    #The directory in which site-wide images are stored.
define("SQLSERVER", "mysql.screevo.com");        #The SQL server that pages are stored in
define("SQLUSER", "dbroot");                    #The user with which to connect to the database
define("SQLPASSWORD", "dbpassword");            #The password with which to connect to the database
define("DATABASE", "dbname");                    #The name of the Database
define("ADMINEMAIL","admin@server.com");        #The Webmaster's Email Address
define("CURRENTDAY",date(j));                    #Defines a constant for the current day (Numerical)
define("CURRENTMONTH",date(n));                    #Defines a constant for the current month (Numerical)
define("CURRENTYEAR",date(Y));                    #Defines a constant for the current year (4 digit)
$partCount = array();
$sidecount = array("F" => array(), "B" => array());
$rowCount = 1;
//========END=========// ?>
<!-- begin header -->
<html>
<head>
<style type="text/css">
body {
        text-align:center;
}
#wrapper {
        margin: 0 auto;
        width: 850px;
        text-align:left;
}
#printlist {
        float: left;
        margin: 0 auto;
        width: 420px;
        text-align:center;
}
#bothsides {
        float: right;
        margin: 0 auto;
        width: 420px;
        text-align:center;
}
table.parts {
        font-family: Tahoma;
        text-align: center;
}
        table.parts td.td1{
        background-color:#33CCFF;
}
        table.parts td.td2 {
        background-color:#FFFFFF;
}
        table.parts td.td3 {
        background-color:#B3FF66;
}
</style>
<title>Pad Print Scanning</title>
</head>
<body>
<div id="wrapper">
        <div id="printlist">
        <!--end header-->
<?
if (!$_POST) { // Have scans been entered? If not, display the form.
?>
</script>
Scans:
<form name="the_form" action="index2.php" method="post">
<textarea rows="20" cols="20" name="scans" id="scans">
</textarea>
<br />
<input type="submit" value="Submit" />
</form>
<script language="JavaScript">
<!--
document.the_form.scans.focus();
-->
</script>
<?
} else { //Scans have been entered. Start scan processing logic
mysql_connect(SQLSERVER, SQLUSER, SQLPASSWORD) or die("Can not connect to DB server."); //open connection
mysql_select_db(DATABASE) or die("Can not connect to Database."); //open DB
$scans = explode("\n",$_POST['scans']); // Explode posted scans into scans array
foreach ($scans as $currentscan) { // step through each scan
        if ($currentscan[0] == "E") { //Cheap check for real part numbers.
                $partselect[] = substr(mysql_real_escape_string($currentscan), 0, 5); // Strip off the extraneous data from the scan
                $partCount[substr(mysql_real_escape_string($currentscan), 0, 5)]++; //Count instances of particular parts.
        };
};//end foreach
$inExpr = implode("','",array_unique($partselect));
$query = "SELECT part, french, length, plate, side, type FROM parts WHERE part IN('$inExpr') "; //Form beginning of query
if (isset($_GET['sort'])) {
        $query .= " ORDER BY ".$_GET['sort']." ".$_GET['dir']; //add on end of query
} else {
        $query .= " ORDER BY side DESC, plate ASC"; //add on end of query
};
$result = mysql_query($query);// execute query
//build row headers ?>
        <table class="parts"><tr>
        <td> # </td>
        <td>Part #
        <form action="index.php?sort=part&dir=ASC"" method="post">
        <input type="hidden" name="scans" value="<?=$_POST['scans']?>">
        <input type="image" src="./images/up.gif" />
        </form>
        <form action="index.php?sort=part&dir=DESC"" method="post">
        <input type="hidden" name="scans" value="<?=$_POST['scans']?>">
        <input type="image" src="./images/down.gif" />
        </form>
        </td>
        <td>Type
        <form action="index.php?sort=type&dir=ASC"" method="post">
        <input type="hidden" name="scans" value="<?=$_POST['scans']?>">
        <input type="image" src="./images/up.gif" />
        </form>
        <form action="index.php?sort=type&dir=DESC"" method="post">
        <input type="hidden" name="scans" value="<?=$_POST['scans']?>">
        <input type="image" src="./images/down.gif" />
        </form>
        </td>
        <td>French
        <form action="index.php?sort=french&dir=ASC"" method="post">
        <input type="hidden" name="scans" value="<?=$_POST['scans']?>">
        <input type="image" src="./images/up.gif" />
        </form>
        <form action="index.php?sort=french&dir=DESC"" method="post">
        <input type="hidden" name="scans" value="<?=$_POST['scans']?>">
        <input type="image" src="./images/down.gif" />
        </form>
        </td>
        <td>Length
        <form action="index.php?sort=length&dir=ASC"" method="post">
        <input type="hidden" name="scans" value="<?=$_POST['scans']?>">
        <input type="image" src="./images/up.gif" />
        </form>
        <form action="index.php?sort=length&dir=DESC"" method="post">
        <input type="hidden" name="scans" value="<?=$_POST['scans']?>">
        <input type="image" src="./images/down.gif" />
        </form>
        </td>
        <td>Plate
        <form action="index.php?sort=plate&dir=ASC"" method="post">
        <input type="hidden" name="scans" value="<?=$_POST['scans']?>">
        <input type="image" src="./images/up.gif" />
        </form>
        <form action="index.php?sort=plate&dir=DESC"" method="post">
        <input type="hidden" name="scans" value="<?=$_POST['scans']?>">
        <input type="image" src="./images/down.gif" />
        </form>
        </td>
        <td>Side
        <form action="index.php?sort=side&dir=ASC"" method="post">
        <input type="hidden" name="scans" value="<?=$_POST['scans']?>">
        <input type="image" src="./images/up.gif" />
        </form>
        <form action="index.php?sort=side&dir=DESC"" method="post">
        <input type="hidden" name="scans" value="<?=$_POST['scans']?>">
        <input type="image" src="./images/down.gif" />
        </form>
        </td>
        </tr>
<?
while($row = mysql_fetch_array($result)){ // step through results
        extract($row);
        for ($i = 0; $i < $partCount[$part]; $i++) { //if a part was scanned $partCount{$row['part']} times, display it that many times
        //parse entries, display results
                strtoupper($side);
                $sidecount[$side][$plate] = 1;
                echo '<tr class="tr'.$type.'">
                <td class="td'.$type.'">'.$rowCount.'</td>
                <td class="td'.$type.'">'.$part.'</td>
                <td class="td'.$type.'">';
                if ($type == "1") {echo 'Product 1';};    //convert type into text
                if ($type == "2") {echo 'Product 2';};    //convert type into text
                if ($type == "3") {echo 'Product 3';};    //convert type into text
                echo'</td>
                <td class="td'.$type.'">'.$french.'</td>
                <td class="td'.$type.'">'.$length.'</td>
                <td class="td'.$type.'">'.$plate.'</td>
                <td class="td'.$type.'">'.$side.'</td>
                </tr>
                ';
                $rowCount++;
    };// close for loop
};//close while loop
?>
</table>
</div>
<div id="bothsides">
<b>PLATES WHERE BOTH SIDES ARE USED</b>:<br><br><br>
<?
foreach ($sidecount['F'] as $plate => $partCounted) {if ($sidecount['B'][$plate] == 1) { echo $plate.' <br>'; }; };
/* foreach($partCount as $part => $numItems) { echo $part.' was scanned '.$numItems.' times. <br>'; }; //this code lets you view the amount of times a prt was scanned.*/
};//close else //footer ?>
<center><a href="/">Start over</a></center>
</div>
</div>
</body>
</html>