这个jquery代码在php中的等效物是什么


What is the equivalent in php for this jquery code?

Jquery Code 是这个。

$.each(data, function(index, elem) {
    if (elem.stdid != stdid) {
        var cols = new Array(days);
        $.each(data, function(ind, el) {
            if (el.stdid == elem.stdid) {
                cols[el.day] = el.status.substring(0, 1);
                console.log(cols[el.day]);
            }
        });

        var row = "<tr><td class='txtcenter level4row'>"+ (counter++) +"</td><td class='txtcenter level4row'>"+ elem.student_name +"</td>";
        for ( i = 1; i<=cols.length; i++) {
            console.log(typeof(cols[i]));
            if (i%2 == 0) {
                row += "<td class='txtcenter' style='background: #e6f3fd;'>"+ ((typeof(cols[i]) == "undefined") ? '-' : cols[i]) +"</td>";
            } else {
                row += "<td class='txtcenter' style='background: #fff;'>"+ ((typeof(cols[i]) == "undefined") ? '-' : cols[i]) +"</td>";
            }
        }
        row += "</tr>";
        $(row).appendTo('#atnd-table tbody');
        stdid = elem.stdid;
    }
});

我转换后的代码是这样的:

<?php    foreach ($vrdetail as $row):?>
   <?php if ($row['stdid'] !== $stdid): ?>
                                                                                             <?php $cols =  array($days); ?>    
        <?php  foreach ($vrdetail as $rowTwo):?>
            <?php if ($rowTwo['stdid'] === $row['stdid']): ?>
                <?php  $cols[$rowTwo['day']] = substr($rowTwo['status'],0,1); echo $cols[$rowTwo['day']];?>
            <?php endif; ?>
        <?php endforeach; ?>
    <?php $tr =  "<tr>".
                 "<td colspan='3' style=''>". $counter++."</td><td>" . $row['student_name'] ."</td>"; ?>
    <?php $length = count($cols);
            for ($i=1; $i <= $length; $i++) { 
                    if ($i%2 === 0 ) {
                        $tr +=  "<td>".  ((gettype($cols[$i]) == 'NULL') ? '-' : $cols[$i]) ."</td>";                                               
                    }else{
                        $tr += "<td >".((gettype($cols[$i]) == 'NULL') ? '-' 
                                                        : $cols[$i]) ."</td>";
                    }
                 }  
            $tr +=  "</tr>";
            $stdid = $row['stdid'];                  
            ?>
                                                                                                    <?php endif; ?>
    <?php endforeach ?>

这个 PHP 代码在以下几行上给了我一个错误:

 $tr +=  "<td>".  ((gettype($cols[$i]) == 'NULL') ? '-' : $cols[$i])."</td>";   
 $tr += "<td >".((gettype($cols[$i]) == 'NULL') ? '-' : $cols[$i]) ."</td>";

哪个说

遇到 PHP 错误 严重性: 通知 消息: 未定义偏移量: 1 文件名: 报告打印/monthlyAttendanceReport_pdf.php 行号:151 严重性:通知消息:未定义偏移量:2

第二行也是如此,但偏移量只是变化。

尝试使用

 $tr .= "<td >".((gettype($cols[$i]) == 'NULL') ? '-' : $cols[$i]) ."</td>";

而不是

 $tr += "<td >".((gettype($cols[$i]) == 'NULL') ? '-' : $cols[$i]) ."</td>";

在第 3 行,您有:

<?php $cols =  array($days); ?>

我假设$days本身就是一个数组。上面的语句将创建一个数组$cols该数组在索引 0 处只有一个元素(即数组)。

在导致错误的行中,您正在尝试访问索引 1 和 2 中不存在的$cols数组元素。