【发布时间】: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