Can someone help me on debugging this code

`<?php

include "../template/php/myconnection.php";
include "../template/php/sessionChecker.php";
include "functions.php";
include "ext.php";

$date = date("Y-m-d");

    $breakdown = "";
    $transkey = "";
    $isFound = false;
    $file_value = file("header.txt");

if(!empty($_GET['tn'])){
    foreach($file_value as $line){
        if(strpos($line,$_GET['tn']) !== false){
            $isFound = true;
            $breakdown = explode(" | ",$line);
            $transkey = $breakdown[3];
            break;
        }
    }
    if(!$isFound == true){
            $transkey = Transkey($transkey);
            $fp = fopen('header.txt','a');
            fwrite($fp,"\n".$date." | ".$_GET['tn']." | ".$_GET['oc']." | ".$transkey);
            fclose($fp);
        }   
}
    $transkey = trim($transkey," ");

if(isset($_POST['state']) && $_POST['state'] == "es"){
    $item = explode(" - ",$_POST['prod']);  

    $fp = fopen('detail.txt','a');
    fwrite($fp,"\n".$_GET['tn']." | ".$item[0]);
    fclose($fp);

    $sql = "select ah.DatePrep, ad.TransNo, ah.RefNo, ad.ProductNo, ah.RefDoc, ah.TransType, 
            pmr.ProductCode, pmr.ProdType, pmr.Description, ah.Remarks, ad.Quantity from posdbase.ar_header as ah
            left join posdbase.ar_detail as ad on ah.outletcode = ad.outletcode and ad.TransNo ='".$_GET['tn']."'
            left join posdbase.prod_master_ref as pmr on ad.ProductNo = pmr.ProductNo
            where ah.DatePrep = '".$_GET['dp']."' and ad.ProductNo = '".$item[0]."' group by ad.ProductNo;";    
    $query = mysqli_query($mysqli,$sql);
    $row = mysqli_fetch_assoc($query);

    sendToMS($row['DatePrep'],$row['TransNo'],$_POST['itype'],$date,$_POST['outlet'],$transkey,$_GET['oc'],
    $row['ProductCode'],$row['ProductNo'],$row['ProdType'],$row['RefNo'],$row['RefDoc'],$row['Quantity'],$_POST['PO_date']);    
}
if(isset($_POST['state']) && $_POST['state'] == "ea"){

$file = file("detail.txt");
$found = false;

    $sql = "select ah.DatePrep, ad.TransNo, ah.RefNo, ad.ProductNo, ah.RefDoc, ah.TransType, 
            pmr.ProductCode, pmr.ProdType, pmr.Description, ah.Remarks, ad.Quantity from posdbase.ar_header as ah
            left join posdbase.ar_detail as ad on ah.outletcode = ad.outletcode and ad.TransNo ='".$_GET['tn']."'
            left join posdbase.prod_master_ref as pmr on ad.ProductNo = pmr.ProductNo
            where ah.DatePrep = '".$_GET['dp']."' and ad.outletcode = '".$_GET['oc']."' group by ad.ProductNo;";    
    $query = mysqli_query($mysqli,$sql);

    while($row = mysqli_fetch_assoc($query)){
            foreach($file as $item){
                if(strpos($item,$row['TransNo']." | ".$row['ProductNo']) !== false){
                    $found = true;
                    break;
                }else{
                    $found = false; 
                }
            }
                if($found == false){
                    $fp = fopen('detail.txt','a');
                    fwrite($fp,"\n".$row['TransNo']." | ".$row['ProductNo']);
                    fclose($fp);
                    sendToMS($row['DatePrep'],$row['TransNo'],$_POST['itype'],$date,$_POST['outlet'],$transkey,$_GET['oc'],
                    $row['ProductCode'],$row['ProductNo'],$row['ProdType'],$row['RefNo'],$row['RefDoc'],$row['Quantity'],$_POST['PO_date']);    

                }
        }

}
mysqli_close($mysqli);
?> <!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"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link href="openCSS.css" rel="stylesheet" type="text/css" /> <?php include "openDeclaration.php"; ?> <title>Untitled Document</title> </head> <body> <form method="POST"> <?php
include "../template/php/msconnection.php";

        $sql = "SELECT OutletName from DB001.dbo.TB080 where OutletCode = '".$_GET['oc']."' ORDER BY OutletName ASC";
        $query = odbc_exec($odbccon,$sql);
        $exec = odbc_fetch_array($query);

        echo '<div style="height: 30px; width: 97%; background-color:#e2e0e0;" class="pad10">';
        echo '<label style="width:33.33%; float: left;" class="fs3 b" name="outlet">'.$exec['OutletName'].'</label>';
        echo '<input type="hidden" value="'.$exec['OutletName'].'" name="outlet" />';
        echo '<label style="width:33.33%; text-align: center; float: left; clear: right;" class="fs7 b">'.$transkey.'</label>';
        echo '<label style="width:33.33%; text-align: right; float: right;" class="fs3 b">'.$_GET['dp'].'</label>';
        echo '</div>';

odbc_close($odbccon);
?> <table id="tableRecords" class="table table-bordered" width="99%"> <thead> <tr> <td></td> <td>Product No.</td> <td>Name</td> <td>Qty</td> <td>Ref. Doc</td> </tr> </thead> <tbody> <?php
include "../template/php/myconnection.php";
# QUERY USE IN SELECT(PROD)
        $sql = "select ah.DatePrep, ad.TransNo, ah.RefNo, ad.ProductNo,ah.RefDoc,ad.Quantity,
                ah.Remarks, pmr.sDescription from posdbase.ar_header as ah, posdbase.ar_detail as ad, posdbase.prod_master_ref as pmr
                where ah.DatePrep = ad.DatePrep and pmr.ProductNo = ad.ProductNo and ad.TransNo = ah.TransNo
                and ah.DatePrep = '".$_GET['dp']."' and ah.TransNo = '".$_GET['tn']."' and ad.outletCode = '".$_GET['oc']."'
                GROUP BY ad.ProductNo ORDER BY ad.ProductNo ASC";
        $query = mysqli_query($mysqli,$sql);

        $detail = file("detail.txt");
        $dFound = false;

            foreach($detail as $row){
                if(strpos($row,$_GET['tn']) !== false){
                    $h = explode(" | ",$row);
        # QUERY USED IN EXTRACT MODULE TABLE    
                    $sql_row = "select ah.DatePrep, ad.TransNo, ah.RefNo, ad.ProductNo,ah.RefDoc,ad.Quantity,
                                ah.Remarks, pmr.sDescription from posdbase.ar_header as ah, posdbase.ar_detail as ad, posdbase.prod_master_ref as pmr
                                where ah.DatePrep = ad.DatePrep and pmr.ProductNo = ad.ProductNo and ad.TransNo = ah.TransNo
                                and ah.DatePrep = '".$_GET['dp']."' and ah.TransNo = '".$h[0]."' and pmr.ProductNo = '".$h[1]."'
                                GROUP BY ad.ProductNo ORDER BY ad.ProductNo ASC";
                    $query_row = mysqli_query($mysqli,$sql_row);

                    if(mysqli_num_rows($query_row) > 0){
                        while($rowItem = mysqli_fetch_assoc($query_row)){
                            echo "<tr>";
                            echo "<td>".'<span class="icon-list">'."</td>";
                            echo "<td>".$rowItem['ProductNo']."</td>";
                            echo "<td>".$rowItem['sDescription']."</td>";
                            echo "<td>".$rowItem['Quantity']."</td>";
                            echo "<td>".$rowItem['RefDoc']."</td>";
                            echo "</tr>";
                        }
                    }

                }   
            } # foreach 

    ?> </tbody> </table> <div style="float: left; clear: left; margin: 5px 0 0 0;"> <select name="itype" style="float: left; clear: left;" id="itype" onchange="ifset()"> <option value="">--- SELECT ITEM TYPE ---</option> <option value="R">MERCHANDISE</option> <option value="I">NON - MERCHANDISE</option> <option value="C">CUSTOMER PROPERTY</option> </select> <select name="prod" style="float: left; clear: left; margin: 5px 0 0 0; width: 400px;" id="prod" onchange="ifset()"> <option value="">--- SELECT ITEM ---</option> <?php
        while($row = mysqli_fetch_assoc($query)){
            foreach($detail as $line){
                if(strpos($line,$_GET['tn']." | ".$row['ProductNo']) !== false){
                    $dFound = true;
                    break;
                }else{
                    $dFound = false;
                }
            }
                if($dFound == false){
                    echo '<option value="'.$row['ProductNo'].'">'.$row['ProductNo']." - ".$row['sDescription'].'</option>';
                }
        }     # END WHILE
mysqli_close($mysqli);
?> </select> <?php 
include "../template/php/msconnection.php";

        $sql_PO = "SELECT PODate FROM DB003.dbo.TB034 WHERE Transkey ='".$transkey."'";
        $query_PO = odbc_exec($odbccon,$sql_PO);
        $exec_date = odbc_fetch_array($query_PO);

odbc_close($odbccon);
?> <input type="text" readonly="readonly" name="PO_date" style="float: left; clear: left;margin: 5px 0 0 0;" placeholder="--- PULL OUT DATE ---" 
    id="date" onchange="ifset()" value="<?php echo $exec_date['PODate'];?>"> <button type="button" class="btn btn-primary" style="float: left; clear: left;margin: 5px 0 0 0;" name="eSelected" id="es" disabled="disabled"> <span class="icon-plus-sign icon-white" style="margin: 0 5px 0 0;"></span>Extract selected item</button> <button type="button" class="btn btn-danger" style="float: left; clear: right;margin: 5px 0 0 5px;" name="eAll" id="ea" disabled="disabled"> <span class="icon-check icon-white" style="margin: 0 5px 0 0;"></span>Extract All</button> <input type="hidden" id="state" name="state"/> <?php odbc_close($odbccon);?> </div> </form> <div id="prompt"> <label></label> </div> <script>
/*function isCheck(n){
    var check = document.getElementsByClassName('cboxes');
    for(var i = 0; i < check.length; i++){
        check[i].checked = n.checked;
    }   
}*/
function ifset(){
    var type = $("#itype").val();
    var itm = $("#prod").val();
    var dt = $("#date").val();

    if(!(type == "") && !(itm == "") && !(dt == "")){
        $("#ea").removeAttr("disabled");
        $("#es").removeAttr("disabled");    
    }else{

            if(!(type == "") && !(dt == "") && !(itm == "")){
                $("#ea").removeAttr("disabled");
            }else{
                $("#ea").attr("disabled","disabled");       
            }
    $("#es").attr("disabled","disabled");   
    }

}
$(document).ready(function(){ 

    $("#prompt").hide();
    $("#tableRecords").tableScroll({
        height: 180
    });

    $("#es").click(function(){
        $("#state").val("es");
        $("#prompt label").html("Are you sure you want to extract this item(s)?");
        $("#prompt").show();
        $("#prompt").dialog({
            modal: true,
            buttons: {
                "YES": function(){
                    var l = $("#prod").val().length
                    if(l > 0){
                        $("form").submit();
                    }
                },
                "NO": function(){
                    $(this).dialog('close');    
                }
            }
        });

    });
    $("#ea").click(function(){
        $("#state").val("ea");
        $("#prompt label").html("Are you sure you want to extract all the item(s)?");
        $("#prompt").show();
        $("#prompt").dialog({
            modal: true,
            buttons: {
                "YES": function(){
                    $("form").submit(); 
                },
                "NO": function(){
                    $(this).dialog('close');    
                }
            }
        });

    });
    $( "#date" ).datepicker({
        buttonImage: "images/calendar.gif",
        showButtonPanel: true,
        changeMonth: true,
        changeYear: true,
        yearRange: '2016:2030'
    },"option", "dateFormat", "yyyy-mm-dd");

});
</script> </body> </html>

my problem is on line 156. The code somehow work on a particular execution but not on other

my problem is on line 194

194 above is a blank line.

sorry for my mistake, The suspected error is on line 156

the code run using table.
for example. I click row that contains an ID of 192 then another form loads which contains the code i've posted and it display the correct result.
However if I click a row that contains an ID of 193. It doesn't display anything and the output source is on query at line 156

Member Avatar for diafol

Aside: You totally mashed up html and php. Keep them separate - this is a nightmare to maintain and to try and debug by hand. We have 241 lines of code - most of it irrelevant to the problem. You seem to have loops all over the place - bit of a mess. In addition, you seem to be running loads and loads of separate SQL queries. Is this really necessary?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.