Skip to content

Instantly share code, notes, and snippets.

@benedict-w
Created January 18, 2013 17:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save benedict-w/4566307 to your computer and use it in GitHub Desktop.
Save benedict-w/4566307 to your computer and use it in GitHub Desktop.
PHP dynamic rowspan for mysql results
<?php
/**
* addRowspan
*
* from an array of assoc mysql results, appends a column 'rowspan' by looking ahead
* for keys with the same value.
*
* @param string $span_key - key to base span on
* @param array $rows - 2d array of rows
*/
function addRowspan($span_key, &$rows) {
for($i=0; $i<count($rows); $i++) {
$rowspan = 1;
for($j=$i; $j<$rows; $j++) {
// lookahead
if(isset($rows[$j+1])) {
// this is not the last row...
if ($rows[$j][$span_key] === $rows[$j+1][$span_key]) {
// the next date is the same
$rowspan++;
// so set its rowspan to 0
$rows[$j+1][$span_key] = 0;
// and check the next
continue;
}
}
// here we have calculated the rowspan
$rows[$i][$span_key] = $rowspan;
// so jump ahead spanned rows in the main loop
$i += $rowspan -1;
break; // no more calculation necessary
}
}
}
@redfoxglove
Copy link

Hi, thanks for the code, although having tried it, it does not appear append a column 'rowspan' but instead changes the span_key value to become the calculated rowspan value. Or maybe I'm missing something? Regards, RP

@PierreC2
Copy link

Good technique. Thank you!

@ektakakadia
Copy link

in codeigniter its not worked how can i use this

@chiahungwang
Copy link

Share a little improvement:

  1. $span_key change to array
  2. fixed append 'rowspan' array
  3. added $unique_key avoid span confusion
function addRowspan($unique_key, $span_key, &$rows)
{
    for ($k = 0; $k < count($span_key); $k++) {
        for ($i = 0; $i < count($rows); $i ++) {
            $rowspan = 1;
            for ($j = $i; $j < $rows; $j ++) {
                // lookahead
                if (isset($rows[$j + 1]))
                {
                    // this is not the last row...
                    if ($rows[$j][$span_key[$k]] === $rows[$j + 1][$span_key[$k]] // the next date is the same
                        && 
                        $rows[$j][$unique_key] === $rows[$j + 1][$unique_key] // check next pk is the same
                    ) {
                        $rowspan ++;
                        // so set its rowspan to 0
                        $rows[$j + 1]['rowspan'][$span_key[$k]] = 0;
                        // and check the next
                        continue;
                    }
                }
                // here we have calculated the rowspan
                $rows[$i]['rowspan'][$span_key[$k]] = $rowspan;
                // so jump ahead spanned rows in the main loop
                $i += $rowspan - 1;
                break; // no more calculation necessary
            }
        }
    }
}

Call function:

addRowspan('field1', array('field1','field2','field3'), $rows);

HTML:

echo '<table border="1" style="border-collapse:collapse; font-size: 12px;">';
$pre_key = '';
foreach ($rows as $row) {
    echo '<tr>';
    if ($row['rowspan']['field1'])
    {
        echo "<td rowspan=".$row['rowspan']['field1'].">$row[field1]</td>";
    }
    if ($row['rowspan']['field2'])
    {
        echo "<td rowspan=".$row['rowspan']['field2'].">$row[field2]</td>";
    }
    if ($row['rowspan']['field3'])
    {
        echo "<td rowspan=".$row['rowspan']['field3'].">$row[field3]</td>";
    }
    echo "<td>$row[field4]</td>";
    echo "<td>$row[field5]</td>";
    echo "<td>$row[field6]</td>";
    echo '</tr>';
}
echo '</table>';

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment