使用rowspan从mysql结果创建动态表

时间:2015-02-26 14:15:59

标签: php mysql html-table

我以这种方式从Mysql输出创建一个HTML表:

$i = 0;
while ( $row = mysql_fetch_assoc($result) ) {
    if ( $i == 0 ) {
        echo "<tr>";
        foreach ( array_keys($row) as $fieldName ) echo "<th>".$fieldName."</th>";
        echo "</tr>";
    }
    echo "<tr>";
    foreach ( $row as $value ) echo "<td>".$value."</td>";      
    echo "</tr>";   
    $i++;
}

如果列中有两个或更多相同的单元格,则应使用HTML属性rowspan

自动加入此单元格

我无法找到在同一中动态创建包含已连接单元格的表格的解决方案。

2 个答案:

答案 0 :(得分:0)

当您想要更改行数时,您必须做两件事:

  • 检查该列中的下一个值,以了解相同的数量
  • 检查先前的rowspan值以避免打印值 不应该(因为前一个有一个rowpan&gt; 1)

以下是一些带有示例值的代码,以便您可以对其进行测试。第一个foreach()必须替换为while ($row = mysql_fetch_assoc($result)) {才能使其与您的数据库数据一起使用。

// Example datas for tests
$rows = array(
    0 => array(
        'name' => 'Product 1',
        'price' => 20,
        'option_x_included' => 'No',
        'duration' => 12
    ),
    1 => array(
        'name' => 'Product 2',
        'price' => 30,
        'option_x_included' => 'Yes',
        'duration' => 12
    ),
    2 => array(
        'name' => 'Product 3',
        'price' => 45,
        'option_x_included' => 'Yes',
        'duration' => 18
    ),
    3 => array(
        'name' => 'Product 4',
        'price' => 60,
        'option_x_included' => 'Yes',
        'duration' => 24
    )
);

echo '<table>';

$i = 0;
$rowspanValues = array();
foreach($rows as $index => $row) {
// Replace this foreach (for tests) by:
// while ($row = mysql_fetch_assoc($result)) {

    // First row: Column headers
    if ($i == 0) {
        echo '<tr>';
        foreach (array_keys($row) as $fieldName) {
            echo '<th>'.$fieldName.'</th>';

            // Init rowspan value for each column
            $rowspanValues[$fieldName] = 1;
        }
        echo '</tr>';
    }

    // Other rows: Values
    echo '<tr>';
    foreach ($row as $fieldName => $value) {

        if ($rowspanValues[$fieldName] == 1) {
        // rowspan for that column is 1: means that we have not tested next values

            // Test if next values in the same column is the same
            $nextIndex = $index+1;
            while (!empty($rows[$nextIndex][$fieldName]) && $value == $rows[$nextIndex][$fieldName]) {
                // Increment nextIndex to test the row after
                $nextIndex++;

                // Increment current rowspan value
                $rowspanValues[$fieldName]++;
            }

            // Put the calculated rowspan to current cell
            echo '<td rowspan="'.$rowspanValues[$fieldName].'">'.$value.'</td>'; 
        } else {
        // rowspan value > 1: means that the previous column had a bigger rowspan, so this cell must not been printed
            $rowspanValues[$fieldName]--;
        }

    }    
    echo '</tr>';   
    $i++;
}

echo '</table>';

答案 1 :(得分:0)

这就是工作:

$result = mysql_query ($sql, $con);

$numRows = mysql_num_rows($result);
while ( $row = mysql_fetch_assoc($result) ) $rows[] = $row;

echo "<table>";
foreach($rows as $index => $row) {
    // First row: Column headers
    if ($index == 0) {
        echo '<tr>';
        foreach (array_keys($row) as $fieldName) {
            echo '<th>'.$fieldName.'</th>';

            // Init rowspan value for each column
            $rowspanValues[$fieldName] = 1;
        }
        echo '</tr>';
    }
    echo '<tr>';
    // Other rows: Values
    foreach ($row as $fieldName => $value) {

        if ($rowspanValues[$fieldName] == 1) {
        // rowspan for that column is 1: means that we have not tested next values

            // Test if next values in the same column is the same
            $nextIndex = $index + 1;            
            while ($value == $rows[$nextIndex][$fieldName]) {
                // Increment nextIndex to test the row after
                $nextIndex++;

                // Increment current rowspan value
                $rowspanValues[$fieldName]++;
            }

            // Put the calculated rowspan to current cell
            echo '<td rowspan="'.$rowspanValues[$fieldName].'">'.$value.'</td>'; 
        } else {
        // rowspan value > 1: means that the previous column had a bigger rowspan, so this cell must not been printed
            $rowspanValues[$fieldName]--;
        }
    }    
    echo '</tr>';   
}
echo '</table>';