<?php
include("config.php");
$rexist = 0;
if($_POST["plt_no"] && $_POST["plt_typeno"])
    {
  
    
    $opname = $_POST["opname"];
    $timein = $_POST["timein"];
    $timeout = $_POST["timeout"];
    $inqty = $_POST["inqty"];
    $iun = $_POST["iun"];
    $outqty = $_POST["outqty"];
    $oun = $_POST["oun"];
    $idno = $_POST["idno"];
    $mcno = $_POST["mcno"];
    $varqty = $_POST["varqty"];
    $varplt = $_POST["varplt"];
    $dateshift = $_POST["dateshift"];
    $shift = $_POST["shift"];
    
    $totalarr = count($opname) - 1;
    for($ctr=0; $ctr < $totalarr; $ctr++)
        {
        $inqty[$ctr] = (float) $inqty[$ctr];
        $varqty[$ctr] = (float) $varqty[$ctr];
        $outqty[$ctr] = (float) $outqty[$ctr];
        $query = "INSERT INTO traceability (operation_name, time_in, time_out, input_qty, input_unit, output_qty, output_unit, id_no, mc_no, variance_qty, variance_plt, date, shift, plt_transact_id) VALUES ('" . $opname[$ctr] . "', '" . $timein[$ctr] . "', '" . $timeout[$ctr] . "', '" . $inqty[$ctr] . "' , '" . $iun[$ctr] . "' ,'" . $outqty[$ctr] . "', '" . $oun[$ctr] . "' , '" . $idno[$ctr] . "', '" . $mcno[$ctr] . "', '" . $varqty[$ctr] . "', '" . $varplt[$ctr] . "', '" . $dateshift[$ctr] . "', '" . $shift[$ctr] . "', '" . $plt_transact_id . "')";
        $result = mysql_query($query);
        }   
    }

$rexist = 0;
?>

    
<html>
<head>
    <style type="text/css">
    .myclass {
     font-size: 10pt; font-family:Arial, Helvetica, sans-serif;
    }
    </style>
    <script language="javascript">
    function getplt(plt_typeno)
        {
        var myform = document.enumplt;
        myform.plt_typeno.value = plt_typeno;
        myform.submit();
        }
        
    function handleEnter(e, nextfield)
        {
        var characterCode = (e && e.which)? e.which: e.keyCode;
        if(characterCode == 13)
            {
            document.getElementById(nextfield).focus();
            return false;
            } 
        else
            {
            return true;
            }
        }
    </script>
</head>
<?php
if($_POST["enum_plt"] && $_POST["plt_typeno"])
    {
?>
<body onLoad="document.plttype.plt_no.focus();">
<?php
    }
else
    {
    echo "<body>";
    }
?>
Type:&nbsp;&nbsp;
<select name="plt_type" onChange="getplt(this.options[this.selectedIndex].value);">
    <?php
    $plt_typeno = "";
    if($_POST["enum_plt"]) $plt_typeno = $_POST["plt_typeno"];
    $query = "SELECT DISTINCT plt_typeno FROM plt_type GROUP BY plt_typeno ORDER BY plt_typeno";
    $result = mysql_query($query);
    echo "<option value=''></option>";
    if(!$plt_typeno)
        {
        while($row = mysql_fetch_array($result))
            echo "<option value='" . $row["plt_typeno"] . "'>" . $row["plt_typeno"] . "</option>";
        }
    else
        {
        while($row = mysql_fetch_array($result))
            {
            echo "<option value='" . $row["plt_typeno"] . "'";
            if($plt_typeno == $row["plt_typeno"]) echo " selected";
            echo ">" . $row["plt_typeno"] . "</option>";
            }
        }
    ?>
</select>


<form action="retrievev01.php" method="post" name="plttype">


<?php
//Show form

//this code is to select type and after the user select type the type option will appear and the plt_no text box also with the retrieve button.
if($_POST["enum_plt"] && $_POST["plt_typeno"])
    {
    $query = "SELECT * FROM plt_type WHERE plt_typeno = '" . $_POST["plt_typeno"] . "'";
    $result = mysql_query($query);
    if($result)
        {
        $rexist = 1;
        ?>
         <input type="hidden" name="retrieve" value="1" />
        <?php
        
       echo "<p>PLT No:&nbsp;&nbsp;<input type='text' name='plt_no' onkeypress='return handleEnter(event,\"plt_date\");' /></br />";
       echo "<p><input type='submit' value='Retrieve' id='retrievedata' /></p>";
        echo "<p>&nbsp;</p>";
        }
    }
        ?>
<?php 

   ///here is the code to view the retrieve data.
   if($_POST["plt_no"])
        {
        $query = "SELECT * FROM plt_transact WHERE plt_no = '" . $_POST["plt_no"] . "'";
        $result_no = mysql_query($query);
        if($result_no)
            {
            if(mysql_num_rows($result_no) > 0)
                {
              $plt_no = $_POST["plt_no"];
                echo "<p>PLT No:&nbsp;&nbsp;<input type='text' name='plt_no' value='$plt_no'; onkeypress='return handleEnter(event,\"plt_date\");' /></br />";
        echo "PLT Date:&nbsp;&nbsp;<input type='text' name='plt_date' onkeypress='return handleEnter(event,\"timein0\");' /></p>";
                $query = "SELECT d.operation_name, d.time_in, d.time_out, d.input_qty, d.input_unit, d.output_qty, d.output_unit, d.id_no, d.mc_no, d.variance_qty, d.variance_plt, d.date, d.shift FROM traceability d, plt_transact t WHERE t.plt_no = '" . $_POST["plt_no"] . "' AND d.plt_transact_id = t.plt_transact_id AND d.operation_name IN('Operation1' , 'Operation2' , 'Operation3' , 'Operation4') ORDER BY d.operation_name";
                $result = mysql_query($query);
                if($result)
                    {
                    echo "<table cellspacing='3'>";            
                    echo "<tr><th class='myclass'>OPERATION NAME</th><th class='myclass'>&nbsp;</th><th class='myclass'>TIME IN</th><th class='myclass'>&nbsp;</th><th class='myclass'>TIME OUT</th><th class='myclass'>&nbsp;</th><th class='myclass'>INPUT QTY</th><th class='myclass'>UNIT</th><th class='myclass'>&nbsp;</th><th class='myclass'>OUTPUT QTY</th><th class='myclass'>UNIT</th><th></th><th class='myclass'>ID #</th><th class='myclass'>&nbsp;</th><th class='myclass'>MC #</th><th class='myclass'>&nbsp;</th><th class='myclass'>VARIANCE QTY</th><th class='myclass'>VARIANCE PLT</th><th class='myclass'>&nbsp;</th><th class='myclass'>DATE</th>&nbsp;&nbsp;<th class='myclass'>SHIFT</th></tr>";
                    $last_operation_name = "";
                    while($row = mysql_fetch_array($result))
                        {
                        echo "\n\t<tr>";
                        
                        echo "\n\t\t<td width='800'>";
                        if($last_operation_name != $row["operation_name"]) echo $row["operation_name"];
                        echo "</td>";
                        $last_operation_name = $row["operation_name"];
                        echo "\n\t\t<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>";
                        echo "\n\t\t<td>" . $row["time_in"] . "</td>";
                        echo "\n\t\t<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>";
                        echo "\n\t\t<td>" . $row["time_out"] . "</td>";
                        echo "\n\t\t<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>";
                        echo "\n\t\t<td align='right'>" . $row["input_qty"] . "</td>";
                        echo "\n\t\t<td>" . $row["input_unit"] . "</td>";
                        echo "\n\t\t<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>";
                        echo "\n\t\t<td align='right'>" . $row["output_qty"] . "</td>";
                        echo "\n\t\t<td>" . $row["output_unit"] . "</td>";
                        echo "\n\t\t<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>";
                        echo "\n\t\t<td>" . $row["id_no"] . "</td>";
                        echo "\n\t\t<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>";
                        echo "\n\t\t<td>" . $row["mc_no"] . "</td>";
                        echo "\n\t\t<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>";
                        echo "\n\t\t<td>" . $row["variance_qty"] . "</td>";
                        echo "\n\t\t<td>" . $row["variance_plt"] . "</td>";
                        echo "\n\t\t<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>";
                        echo "\n\t\t<td>" . $row["date"] . "</td>";
                        echo "\n\t\t<td>" . $row["shift"] . "</td>";
                        echo "\n\t</tr>";
                        }
                    echo "</table>";
                    }
                }
                
                
                ///here is the code to also view at the below page the operation 5 to used to insert data in operation 5 but the problem here is it depends on the plt type that was choose on the first step, because after I retrieve data the plt_typeno was blank the plt type that I choosed was not appear. I want it to appear again. i think its tge reason why the operation 5 was not appear.
                $query = "SELECT * FROM trace_operations WHERE plt_typeno = '" . $_POST["plt_typeno"] . "'  AND operation_name IN ('OPeration 5')ORDER BY operation_name";
        $last_operation_name = "";
        $result = mysql_query($query);
        if($result)
            {  
            $rexist = 1;
            echo "<table>";
            $totalrows = mysql_num_rows($result);
            $trows = $totalrows - 1;
            for($ctr = 0; $ctr < $trows; $ctr++)
                {
                $row = mysql_fetch_array($result);
                $tctr = $ctr + 1;
                echo "\n\t<tr>";
                echo "\n\t\t<td width='800'>";
                if ($last_operation_name != $row["operation_name"]) echo $row["operation_name"];
                $last_operation_name = $row["operation_name"];
                echo "<input type='hidden' width='400' name='opname[]' value='" . $row["operation_name"] . "' /></td>";   
                echo "\n\t\t<td><input size='6' type='text' name='timein[]' id='timein" . $ctr . "' onkeypress='return handleEnter(event,\"timeout" . $ctr . "\");' /></td>";
                echo "\n\t\t<td><input size='6' type='text' name='timeout[]' id='timeout" . $ctr . "' onkeypress='return handleEnter(event,\"inqty" . $ctr . "\");' /></td>";
                echo "\n\t\t<td><input size='6' type='text' name='inqty[]' id='inqty" . $ctr . "' onkeypress='return handleEnter(event,\"outqty" . $ctr . "\");' /></td>";
                echo "\n\t\t<td>" . $row["input_unit"];
                echo "<input type='hidden' name='iun[]' value='" . $row["input_unit"] . "' /></td>";
                echo "\n\t\t<td><input size='6' type='text' name='outqty[]' id='outqty" . $ctr . "' onkeypress='return handleEnter(event,\"idno" . $ctr . "\");' /></td>";
                echo "\n\t\t<td>" . $row["output_unit"];
                echo "<input type='hidden' name='oun[]' value='" . $row["output_unit"] . "'></td>";
                echo "\n\t\t<td><input size='6' type='text' name='idno[]' id='idno" . $ctr . "' onkeypress='return handleEnter(event,\"mcno" . $ctr . "\");' /></td>";
                echo "\n\t\t<td><input size='6' type='text' name='mcno[]' id='mcno" . $ctr . "' onkeypress='return handleEnter(event,\"varqty" . $ctr . "\");' /></td>";
                echo "\n\t\t<td><input size='6' type='text' name='varqty[]' id='varqty" . $ctr . "' onkeypress='return handleEnter(event,\"varplt" . $ctr . "\");' /></td>";
                echo "\n\t\t<td><input size='6' type='text' name='varplt[]' id='varplt" . $ctr . "' onkeypress='return handleEnter(event,\"dateshift" . $ctr . "\");' /></td>";
                echo "\n\t\t<td><input size='6' type='text' name='dateshift[]' id='dateshift" . $ctr . "' onkeypress='return handleEnter(event,\"shift" . $ctr . "\");' /></td>";
               echo "\n\t\t<td><input size='6' type='text' name='shift[]' id='shift" . $ctr . "' onkeypress='return handleEnter(event,\"timein" . $tctr . "\");' /></td>";
                echo "\n\t</tr>";
                }
            if($totalrows > 1);
                {
                $row = mysql_fetch_array($result);
                echo "\n\t<tr>";
                echo "\n\t\t<td width='800'>";
                if ($last_operation_name != $row["operation_name"]) echo $row["operation_name"];
                $last_operation_name = $row["operation_name"];
                echo "<input type='hidden' width='400' name='opname[]' value='" . $row["operation_name"] . "' /></td>";   
                echo "<input type='hidden' name='opname[]' value='" . $row["operation_name"] . "' /></td>";
                echo "\n\t\t<td><input size='6' type='text' name='timein[]' id='timein" . $ctr . "' onkeypress='return handleEnter(event,\"timeout" . $ctr . "\");' /></td>";
                echo "\n\t\t<td><input size='6' type='text' name='timeout[]' id='timeout" . $ctr . "' onkeypress='return handleEnter(event,\"inqty" . $ctr . "\");' /></td>";
                echo "\n\t\t<td><input size='6' type='text' name='inqty[]' id='inqty" . $ctr . "' onkeypress='return handleEnter(event,\"outqty" . $ctr . "\");' /></td>";
                echo "\n\t\t<td>" . $row["input_unit"];
                echo "<input type='hidden' name='iun[]' value='" . $row["input_unit"] . "' /></td>";
                echo "\n\t\t<td><input size='6' type='text' name='outqty[]' id='outqty" . $ctr . "' onkeypress='return handleEnter(event,\"idno" . $ctr . "\");' /></td>";
                echo "\n\t\t<td>" . $row["output_unit"];
                echo "<input type='hidden' name='oun[]' value='" . $row["output_unit"] . "'></td>";
                echo "\n\t\t<td><input size='6' type='text' name='idno[]' id='idno" . $ctr . "' onkeypress='return handleEnter(event,\"mcno" . $ctr . "\");' /></td>";
                echo "\n\t\t<td><input size='6' type='text' name='mcno[]' id='mcno" . $ctr . "' onkeypress='return handleEnter(event,\"varqty" . $ctr . "\");' /></td>";
                echo "\n\t\t<td><input size='6' type='text' name='varqty[]' id='varqty" . $ctr . "' onkeypress='return handleEnter(event,\"varplt" . $ctr . "\");' /></td>";
                echo "\n\t\t<td><input size='6' type='text' name='varplt[]' id='varplt" . $ctr . "' onkeypress='return handleEnter(event,\"dateshift" . $ctr . "\");' /></td>";
                echo "\n\t\t<td><input size='6' type='text' name='dateshift[]' id='dateshift" . $ctr . "' onkeypress='return handleEnter(event,\"shift\");' /></td>";
                echo "\n\t\t<td><input size='6' type='text' name='shift[]' id='shift" . $ctr . "' onkeypress='return handleEnter(event,\"saveform\");' /></td>";
                echo "\n\t</tr>";
                }
            echo "</table>";
            }
             if($rexist) 
        {
        echo "<p><input type='submit' value='Save' id='saveform' /></p>";
        echo "<input type='hidden' name='plt_typeno' value='" . $_POST["plt_typeno"] . "' />";
        }
          }
                    
                
       else
                {
                echo "&nbsp;PLT No.:&nbsp;&nbsp;" . $_POST["plt_no"] . " not found.<br />";
                }
            }
                          
            
  
        
?>
</form>
</form>
<form name="enumplt" action="retrievev01.php" method="post">
<input type="hidden" name="enum_plt" value="1" />
<input type="hidden" name="plt_typeno" />
</form>
</body>
</html>
