【问题标题】:What is the equivalent in php for this jquery code?这个 jquery 代码在 php 中的等价物是什么?
【发布时间】:2015-01-26 18:39:25
【问题描述】:

Jquery 代码是这样的..

$.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 文件名: reportPrints/monthlyAttendanceReport_pdf.php 行号:151 严重性:通知消息:未定义的偏移量:2

第二行也一样,只是偏移量是变化的。

【问题讨论】:

  • 使用点 '.'取而代之的是加号“+”来组合。它是 $tr .= 不是 $tr +=
  • 我已经尝试过了,但它没有工作给我同样的错误......
  • @usama 这不是错误的原因,但为了让代码执行您想要的操作,您也需要修复它。
  • @TimSeguine 我已经这样做了,但是我得到了结果,但是我得到的错误是相同的,并且每次 for 循环运行时都不会附加表中的行。知道我应该怎么做...?
  • 您能否var_dump($days); 并发布结果。与您使用它的方式相比,您的 $cols 变量有问题。您正在尝试访问不存在的索引,但没有看到数据,这有点难以诊断。通过阅读代码,您使用它的方式没有意义,但这就是我能从这里真正说的。

标签: javascript php jquery arrays foreach


【解决方案1】:

尝试使用

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

而不是

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

【讨论】:

  • Thnx 但它仍然在这两行上给我错误我想添加一个新行来表示循环运行的时间......
【解决方案2】:

在第 3 行你有:

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

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

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

【讨论】:

  • $days 变量只是我从输入中获得的一个值,其中包含 30...
  • 如果 $days = 30,那么 $cols = array(30) 将创建一个只有 1 个元素的数组,而不是 30 个元素的数组。如果要创建一个包含 30 个元素的数组,请使用 $cols = array_pad(array(), $days, '');
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-14
  • 1970-01-01
相关资源
最近更新 更多