使用 rowspan 从 mysql 结果动态创建 table
create dynamically table from mysql result with rowspan
我以这种方式从 Mysql 输出创建 HTML table:
$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
自动 加入 这些单元格
我无法找到在同一 列.
中动态创建包含合并单元格的 table 的解决方案
因为你想改变行跨度,你必须做两件事:
- 检查该列中的下一个值以了解有多少相同
- 检查之前的 rowspan 值以避免打印一个值
不应该(因为前一个的 rowspan > 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>';
这样就可以了:
$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>';
我以这种方式从 Mysql 输出创建 HTML table:
$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
自动 加入 这些单元格
我无法找到在同一 列.
中动态创建包含合并单元格的 table 的解决方案因为你想改变行跨度,你必须做两件事:
- 检查该列中的下一个值以了解有多少相同
- 检查之前的 rowspan 值以避免打印一个值 不应该(因为前一个的 rowspan > 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>';
这样就可以了:
$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>';