I am receiving this error Undefined offset: -1.
I am pretty sure it is because I am comparing a value of an array index with the value of the previous index, so on the first iteration its failing because there is no -1 index.
for ($i=0; $i<$rows_n; $i++)
{
formatTR($i, $rows, 'YEAR');
}
// Function
function formatTR($i, $rows, $year)
{
if ($rows[$year][$i] != $rows[$year][$i-1] ) {
print "<tr class='header-year-tr'><td colspan='2'>{$rows[$year][$i]}</td></tr>";
}
print "<tr>";
}
The function checks to see if the previous value of $rows[year][i] is the same as the previous $rows[year][i-1]. If its different, it prints out a formatted < TR >.
Seems like a way to avoid this error would be to first check if the value of $i is zero, but I'm wondering if there is another way. Possibly in my if conditional.
$i = 1.if ($rows[$year][$i] != $rows[$year][$i-1] ) {should beif (!isset($rows[$year][$i-1]) || $rows[$year][$i] != $rows[$year][$i-1]) {