我的2个条件的图表数据(无日期输入和日期输入)未显示任何结果

时间:2019-04-09 02:25:22

标签: javascript php json mysql-workbench

目前,我的代码有问题。有2页。

我尝试从读取的文件中的数据库中提取数据,然后将其发布到索引以图表的形式显示数据。我还创建了2种类型的输入,用户可以在其中按日期和时间范围过滤数据。问题是我的索引没有显示任何数据结果(图表)。请帮忙。

  1. index.php

    <script type="text/javascript" language="javascript">
        $(document).ready(function(data){
            $('#datetimepicker').datetimepicker({
                format:'yyyy-mm-dd HH:mm:ss'
            });
    
            $('#datetimepicker1').datetimepicker({
                format:'yyyy-mm-dd HH:mm:ss'
            });
    
            fetch_data('no');
    
            function fetch_data(is_date_search,datetimepicker='',datetimepicker1=''){
                $.ajax({
                    url:"read.php",
                    async: false,
                    dataType: 'json',
                    type:"post",
                    data:{
                        is_date_search:is_date_search,datetimepicker:datetimepicker,datetimepicker1:datetimepicker1
                    },
    
                    success: function(data){
                        console.log(data);
                        var grade = [];
                        var percentage = [];
    
                        for (var i in data){
                            grade.push(data[i].grade);
                            percentage.push(data[i].percentage);
                        }
    
                        var chartdata = {
                            labels: grade,
                            datasets : [
                                {
                                    label: 'Yield report',                      
                                    backgroundColor: [
                                        'rgba(255, 99, 132, 0.2)',
                                        'rgba(54, 162, 235, 0.2)',
                                        'rgba(255, 206, 86, 0.2)',
                                        'rgba(75, 192, 192, 0.2)',
                                        'rgba(153, 102, 255, 0.2)',
                                        'rgba(255, 159, 64, 0.2)',
                                        'rgba(0, 128, 128, 0.2)',
                                        'rgba(0, 255, 0, 0.2)',
                                        'rgba(0, 255, 255, 0.2)'
                                    ],
                                    borderColor: [
                                        'rgba(255, 99, 132, 1)',
                                        'rgba(54, 162, 235, 1)',
                                        'rgba(255, 206, 86, 1)',
                                        'rgba(75, 192, 192, 1)',
                                        'rgba(153, 102, 255, 1)',
                                        'rgba(255, 159, 64, 1)',
                                        'rgba(0, 128, 128, 1)',
                                        'rgba(0, 255, 0, 1)',
                                        'rgba(0, 255, 255, 1)'
                                    ],
                                    hoverBackgroundColor: [
                                        'rgba(255, 99, 132, 1)',
                                        'rgba(54, 162, 235, 1)',
                                        'rgba(255, 206, 86, 1)',
                                        'rgba(75, 192, 192, 1)',
                                        'rgba(153, 102, 255, 1)',
                                        'rgba(255, 159, 64, 1)',
                                        'rgba(0, 128, 128, 1)',
                                        'rgba(0, 255, 0, 1)',
                                        'rgba(0, 255, 255, 1)'
                                    ],
                                    hoverBorderColor: [
                                        'rgba(255, 99, 132, 1)',
                                        'rgba(54, 162, 235, 1)',
                                        'rgba(255, 206, 86, 1)',
                                        'rgba(75, 192, 192, 1)',
                                        'rgba(153, 102, 255, 1)',
                                        'rgba(255, 159, 64, 1)',
                                        'rgba(0, 128, 128, 1)',
                                        'rgba(0, 255, 0, 1)',
                                        'rgba(0, 255, 255, 1)'
                                    ],
                                    data: percentage
                                }
                            ]
                        };
    
                        var ctx = $("#myChart");
    
                        var barGraph = new Chart(ctx,{
                            type: 'bar',
                            data: chartdata         
                        });
                    },
                    error: function(){}
                });     
            }
    
            $('#search').click(function(){
                var start_date = $('.datetimepicker').val();
                var end_date = $('.datetimepicker1').val();
    
                if(start_date !='' && end_date !='')
                {
                    fetch_data('yes', start_date, end_date);
                }
                else
                {
                    alert("Both Date is Required");
                }
    
            });
    
    
        });
    </script>
    
  2. read.php

    $columns = array( 
        // datatable column index  => database column name
        0 => 'grade', 
        1 => 'quantity', 
        2 => 'percentage'
    );
    
    
    $sql = "SELECT category.grade as grade,gradesum.quantity as quantity, gradesum.percentage as percentage FROM";
    
    if($_POST["is_date_search"]!="yes")
    {
    
        $currentdate = new DateTime(date('Y-m-d H:i:s'));
    
        $datenow = $currentdate->format('Y-m-d');
        $timenow = $currentdate->format('H:i:s');
    
        $startdate = new DateTime(date('Y-m-d H:i:s', strtotime('-1 day', time())));
    
        $dateyesterday = $startdate->format('Y-m-d');
        $timeyesterday = $startdate->format('H:i:s');
    
        if($timenow >= '07:30:00')
        {
            $timenow1 = '07:30:00';
            $finaldate = date('Y-m-d H:i:s', strtotime("$datenow $timenow1"));
    
        }
        else if($timenow < '07:30:00')
        {
            $timenow2 = '07:30:00';
            $finaldate = date('Y-m-d H:i:s', strtotime("$datenow $timenow2"));          
        }
    
    
        if($timeyesterday >= '07:30:00')
        {
            $timeyesterday1 = '07:30:00';
            $finaldate1 = date('Y-m-d H:i:s', strtotime("$dateyesterday $timeyesterday1"));         
        }
        else if($timeyesterday < '07:30:00')
        {
            $timeyesterday2 = '07:30:00';
            $finaldate1 = date('Y-m-d H:i:s', strtotime("$dateyesterday $timeyesterday2"));         
        }
    
        $sql .="
                (
                    (
                        SELECT grade FROM mcs_cell2.mcs_stack_id_gen_combined
                        WHERE wo LIKE '%szb%'
                                AND (status NOT LIKE 'terminate%'
                                OR status IS NULL)
                        AND grade != 'RR'
                        GROUP BY grade
                    )category
    
                    LEFT JOIN        
    
                    (        
                        SELECT grade,sum(qtty) as quantity,
                        CAST((sum(qtty)/(
                                    SELECT sum(qtty) FROM mcs_cell2.mcs_stack_id_gen_combined
                                    WHERE wo LIKE '%szb%'
                                    AND (
                                            ((1065_move_in_time >= '".$finaldate1."'AND 1065_move_in_time < '".$finaldate."')AND grade !='A1')
                                            OR 
                                            ((1060_move_out_time >= '".$finaldate1."'AND 1060_move_out_time < '".$finaldate."')AND grade = 'A1')
                                        )
                                    AND (status NOT LIKE 'terminate%'OR status IS NULL)
                                    )* 100)as decimal (10,2)) as percentage
                        FROM mcs_cell2.mcs_stack_id_gen_combined
                        WHERE wo LIKE '%szb%'
                                AND
                                (
                                    ((1065_move_in_time >= '".$finaldate1."' AND 1065_move_in_time < '".$finaldate."')AND grade != 'A1')
                                    OR 
                                    ((1060_move_out_time >= '".$finaldate1."' AND 1060_move_out_time < '".$finaldate."') AND grade = 'A1')
                                )
                                AND (status NOT LIKE 'terminate%'
                                OR status IS NULL)
                        GROUP BY grade
                    )gradesum on category.grade = gradesum.grade
                )       
        ";
    }
    
    if($_POST["is_date_search"]=="yes")
    {
        $sql .= "               
        (
            (
                SELECT grade FROM mcs_cell2.mcs_stack_id_gen_combined
                WHERE wo LIKE '%szb%'
                        AND (status NOT LIKE 'terminate%'
                        OR status IS NULL)
                AND grade != 'RR'
                GROUP BY grade
            )category
    
                LEFT JOIN        
    
                (        
                    SELECT grade,sum(qtty) as quantity,
                    CAST((sum(qtty)/(
                                SELECT sum(qtty) FROM mcs_cell2.mcs_stack_id_gen_combined
                                WHERE wo LIKE '%szb%'
                                AND (
                                        ((1065_move_in_time >= '".$POST_['datetimepicker']."'AND 1065_move_in_time < '".$POST_['datetimepicker1']."')AND grade !='A1')
                                        OR 
                                        ((1060_move_out_time >= '".$POST_['datetimepicker']."'AND 1060_move_out_time < '".$POST_['datetimepicker1']."')AND grade = 'A1')
                                    )
                                AND (status NOT LIKE 'terminate%'OR status IS NULL)
                                )* 100)as decimal (10,2)) as percentage
                    FROM mcs_cell2.mcs_stack_id_gen_combined
                    WHERE wo LIKE '%szb%'
                            AND
                            (
                                ((1065_move_in_time >= '".$POST_['datetimepicker']."' AND 1065_move_in_time < '".$POST_['datetimepicker1']."')AND grade != 'A1')
                                OR 
                                ((1060_move_out_time >= '".$POST_['datetimepicker']."' AND 1060_move_out_time < '".$POST_['datetimepicker1']."') AND grade = 'A1')
                            )
                            AND (status NOT LIKE 'terminate%'
                            OR status IS NULL)
                    GROUP BY grade
                )gradesum on category.grade = gradesum.grade
            )
        ";
    
    }
    
    
    $query=mysqli_query($cell2, $sql) or die("-");
    $totalData = mysqli_num_rows($query);
    //echo $totalData;
    $totalFiltered = $totalData;  
    
    $sqls = "SELECT category.grade as grade,gradesum.quantity as quantity, gradesum.percentage as percentage FROM";
    
            if($_POST["is_date_search"]!="yes")
            {
    
        $currentdate = new DateTime(date('Y-m-d H:i:s'));
    
    $datenow = $currentdate->format('Y-m-d');
    $timenow = $currentdate->format('H:i:s');
    
    $startdate = new DateTime(date('Y-m-d H:i:s', strtotime('-1 day', time())));
    
    $dateyesterday = $startdate->format('Y-m-d');
    $timeyesterday = $startdate->format('H:i:s');
    
    if($timenow >= '07:30:00')
    {
        $timenow1 = '07:30:00';
        $finaldate = date('Y-m-d H:i:s', strtotime("$datenow $timenow1"));
    
    }
    else if($timenow < '07:30:00')
    {
        $timenow2 = '07:30:00';
        $finaldate = date('Y-m-d H:i:s', strtotime("$datenow $timenow2"));          
    }
    
    
    if($timeyesterday >= '07:30:00')
    {
        $timeyesterday1 = '07:30:00';
        $finaldate1 = date('Y-m-d H:i:s', strtotime("$dateyesterday $timeyesterday1"));         
    }
    else if($timeyesterday < '07:30:00')
    {
        $timeyesterday2 = '07:30:00';
        $finaldate1 = date('Y-m-d H:i:s', strtotime("$dateyesterday $timeyesterday2"));         
    }
    
    $sqls .="
            (
                (
                    SELECT grade FROM mcs_cell2.mcs_stack_id_gen_combined
                    WHERE wo LIKE '%szb%'
                            AND (status NOT LIKE 'terminate%'
                            OR status IS NULL)
                    AND grade != 'RR'
                    GROUP BY grade
                )category
    
                LEFT JOIN        
    
                (        
                    SELECT grade,sum(qtty) as quantity,
                    CAST((sum(qtty)/(
                                SELECT sum(qtty) FROM mcs_cell2.mcs_stack_id_gen_combined
                                WHERE wo LIKE '%szb%'
                                AND (
                                        ((1065_move_in_time >= '".$finaldate1."'AND 1065_move_in_time < '".$finaldate."')AND grade !='A1')
                                        OR 
                                        ((1060_move_out_time >= '".$finaldate1."'AND 1060_move_out_time < '".$finaldate."')AND grade = 'A1')
                                    )
                                AND (status NOT LIKE 'terminate%'OR status IS NULL)
                                )* 100)as decimal (10,2)) as percentage
                    FROM mcs_cell2.mcs_stack_id_gen_combined
                    WHERE wo LIKE '%szb%'
                            AND
                            (
                                ((1065_move_in_time >= '".$finaldate1."' AND 1065_move_in_time < '".$finaldate."')AND grade != 'A1')
                                OR 
                                ((1060_move_out_time >= '".$finaldate1."' AND 1060_move_out_time < '".$finaldate."') AND grade = 'A1')
                            )
                            AND (status NOT LIKE 'terminate%'
                            OR status IS NULL)
                    GROUP BY grade
                )gradesum on category.grade = gradesum.grade
            )       
    ";
    
    
    }
    
    
    if($_POST["is_date_search"]=="yes")
    {
    $sqls .= "              
    (
                (
                    SELECT grade FROM mcs_cell2.mcs_stack_id_gen_combined
                    WHERE wo LIKE '%szb%'
                            AND (status NOT LIKE 'terminate%'
                            OR status IS NULL)
                    AND grade != 'RR'
                    GROUP BY grade
                )category
    
                LEFT JOIN        
    
                (        
                    SELECT grade,sum(qtty) as quantity,
                    CAST((sum(qtty)/(
                                SELECT sum(qtty) FROM mcs_cell2.mcs_stack_id_gen_combined
                                WHERE wo LIKE '%szb%'
                                AND (
                                        ((1065_move_in_time >= '".$POST_['datetimepicker']."'AND 1065_move_in_time < '".$POST_['datetimepicker1']."')AND grade !='A1')
                                        OR 
                                        ((1060_move_out_time >= '".$POST_['datetimepicker']."'AND 1060_move_out_time < '".$POST_['datetimepicker1']."')AND grade = 'A1')
                                    )
                                AND (status NOT LIKE 'terminate%'OR status IS NULL)
                                )* 100)as decimal (10,2)) as percentage
                    FROM mcs_cell2.mcs_stack_id_gen_combined
                    WHERE wo LIKE '%szb%'
                            AND
                            (
                                ((1065_move_in_time >= '".$POST_['datetimepicker']."' AND 1065_move_in_time < '".$POST_['datetimepicker1']."')AND grade != 'A1')
                                OR 
                                ((1060_move_out_time >= '".$POST_['datetimepicker']."' AND 1060_move_out_time < '".$POST_['datetimepicker1']."') AND grade = 'A1')
                            )
                            AND (status NOT LIKE 'terminate%'
                            OR status IS NULL)
                    GROUP BY grade
                )gradesum on category.grade = gradesum.grade
            )
    ";
    
    }
    
    
    
    $query=mysqli_query($cell2, $sqls) or die("read.php: get events1");
    $totalFiltered = mysqli_num_rows($query); // when there is a search parameter then we have to modify total number filtered rows as per search result.
    
    
    
    $sqls.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."  LIMIT ".$requestData['start']." ,".$requestData['length']."   ";
    $query=mysqli_query($cell2, $sqls) or die("read.php: get events2");
    
    $data = array();
    while( $row=mysqli_fetch_array($query) ) {  // preparing an array
    $nestedData=array();
    
    $nestedData[] = $row["grade"];
    $nestedData[] = $row["quantity"];
    $nestedData[] = $row["percentage"];
    
    
    
    $data[] = $nestedData;
    }
    
    
    
    $json_data = array(
            "draw"            => intval( $requestData['draw'] ),  
            "recordsTotal"    => intval( $totalData ),  
            "recordsFiltered" => intval( $totalFiltered ), totalFiltered = totalData
            "data"            => $data   
    
    echo json_encode($json_data);
    

0 个答案:

没有答案