對不起我的英文不好: 在這裏,我已經回答了這個問題How to show data from database with dynamic rowspan。再次讓我試着回答這個問題。首先讓我們看看mysql的問題。
MySQL的:
在你還沒有查詢訂單的MySQL查詢。因爲在現實生活中,你不能指望湯姆的所有記錄,賬單記錄都會在那裏。例如,採取以下插入。
INSERT INTO test_work(ename, sal)
VALUES("tom", 100),
("bill", 450),
("bill", 100),
("tom", 200),
("bill", 250),
("bill", 400),
("James", 50);
SELECT * FROM test_work;
結果:
+-------+------+
| ename | sal |
+-------+------+
| tom | 100 |
| bill | 450 |
| bill | 100 |
| tom | 200 |
| bill | 250 |
| bill | 400 |
| James | 50 |
+-------+------+
所以你的MySQL查詢應該是爲了用的ename。這裏也應該對每個人的sal進行編配。因此,我們的查詢:
SELECT * FROM emp ORDER BY ename, sal;
編碼:
- 整個任務中,我們可以分爲3個部分。
- Mysql數據提取並存儲在數組中。
- 計算行跨度
- 印刷
MySql的Datafetching:
在數據從MySQL服務器獲取我們總是應該嘗試使用mysql_fetch_assoc函數,而不是mysql_fetch_array的。因爲mysql_fetch_assoc將只返回ename和sal。但mysql_fetch_array將返回索引ename,sal,0,1的數組。
# connect to mysql server
# and select the database, on which
# we will work.
$conn = mysql_connect('', 'root', '');
$db = mysql_select_db('test');
# Query the data from database.
$query = 'SELECT * FROM test_work ORDER BY ename, sal';
$result = mysql_query($query);
# Intialize the array, which will
# store the fetched data.
$sal = array();
$emp = array();
# Loop over all the fetched data, and save the
# data in array.
while($row = mysql_fetch_assoc($result)) {
array_push($emp, $row['ename']);
array_push($sal, $row['sal']);
}
計算行跨度:
# Intialize the array, which will store the
# rowspan for the user.
$arr = array();
# loop over all the sal array
for ($i = 0; $i < sizeof($sal); $i++) {
$empName = $emp[$i];
# If there is no array for the employee
# then create a elemnt.
if (!isset($arr[$empName])) {
$arr[$empName] = array();
$arr[$empName]['rowspan'] = 0;
}
$arr[$empName]['printed'] = "no";
# Increment the row span value.
$arr[$empName]['rowspan'] += 1;
}
時,你會print_r的ARR的陣列輸出將是:
Array
(
[bill] => Array
(
[rowspan] => 4
[printed] => no
)
[James] => Array
(
[rowspan] => 1
[printed] => no
)
[tom] => Array
(
[rowspan] => 2
[printed] => no
)
)
打印與行跨度:
echo "<table cellspacing='0' cellpadding='0'>
<tr>
<th>Ename</th>
<th>Sal</th>
</tr>";
for($i=0; $i < sizeof($sal); $i++) {
$empName = $emp[$i];
echo "<tr>";
# If this row is not printed then print.
# and make the printed value to "yes", so that
# next time it will not printed.
if ($arr[$empName]['printed'] == 'no') {
echo "<td rowspan='".$arr[$empName]['rowspan']."'>".$empName."</td>";
$arr[$empName]['printed'] = 'yes';
}
echo "<td>".$sal[$i]."</td>";
echo "</tr>";
}
echo "</table>";
代碼優化:
現在我們可以結合行跨度計算和mysql數據抓取。因爲在數組中保存獲取的數據時,我們可以計算rowspan。因此,我們的最終代碼:
<!DOCTYPE html>
<html>
<head>
<style>
table tr td, table tr th{
border: black 1px solid;
padding: 5px;
}
</style>
</head>
<body>
<?php
# connect to mysql server
# and select the database, on which
# we will work.
$conn = mysql_connect('', 'root', '');
$db = mysql_select_db('test');
# Query the data from database.
$query = 'SELECT * FROM test_work ORDER BY ename, sal';
$result = mysql_query($query);
# $arr is array which will be help ful during
# printing
$arr = array();
# Intialize the array, which will
# store the fetched data.
$sal = array();
$emp = array();
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#
# data saving and rowspan calculation #
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#
# Loop over all the fetched data, and save the
# data.
while($row = mysql_fetch_assoc($result)) {
array_push($emp, $row['ename']);
array_push($sal, $row['sal']);
if (!isset($arr[$row['ename']])) {
$arr[$row['ename']]['rowspan'] = 0;
}
$arr[$row['ename']]['printed'] = 'no';
$arr[$row['ename']]['rowspan'] += 1;
}
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# DATA PRINTING #
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%#
echo "<table cellspacing='0' cellpadding='0'>
<tr>
<th>Ename</th>
<th>Sal</th>
</tr>";
for($i=0; $i < sizeof($sal); $i++) {
$empName = $emp[$i];
echo "<tr>";
# If this row is not printed then print.
# and make the printed value to "yes", so that
# next time it will not printed.
if ($arr[$empName]['printed'] == 'no') {
echo "<td rowspan='".$arr[$empName]['rowspan']."'>".$empName."</td>";
$arr[$empName]['printed'] = 'yes';
}
echo "<td>".$sal[$i]."</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
結果:
你能提供您當前的代碼,以便我們有一些工作? – erisco 2011-05-30 07:36:01