maaf untuk bahasa Inggris saya yang buruk:Di sini saya telah menjawab pertanyaan ini Cara menampilkan data dari database dengan dynamic rowspan . Sekali lagi izinkan saya mencoba menjawab pertanyaan ini. Pertama, jangan sampai kita mengerjakan kueri mysql.
MySql Work:
Dalam kueri mysql Anda belum meminta untuk dipesan. Karena dalam kehidupan nyata, Anda tidak dapat mengharapkan bahwa setelah semua catatan tom, catatan tagihan akan ada di sana. Misalnya, ambil penyisipan berikut.
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;
Hasil:
+-------+------+
| ename | sal |
+-------+------+
| tom | 100 |
| bill | 450 |
| bill | 100 |
| tom | 200 |
| bill | 250 |
| bill | 400 |
| James | 50 |
+-------+------+
Jadi kueri mysql Anda harus diurutkan berdasarkan ename. Di sini juga sal . setiap orang harus dipesan. Jadi pertanyaan kami:
SELECT * FROM emp ORDER BY ename, sal;
KODE:
- Seluruh tugas dapat kita bagi menjadi 3 bagian.
- Pengambilan dan penyimpanan Data MySQL dalam array.
- Menghitung rentang baris
- Mencetak
Pengambilan Data MySql:
Selama pengambilan data dari server mysql selalu kita harus mencoba menggunakan fungsi mysql_fetch_assoc daripada mysql_fetch_array . Karena mysql_fetch_assoc hanya akan mengembalikan ename dan sal. Tapi mysql_fetch_array akan mengembalikan array dengan indeks 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']);
}
Menghitung Rentang Baris:
# 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;
}
ketika Anda akan print_r array arr, hasilnya adalah:
Array
(
[bill] => Array
(
[rowspan] => 4
[printed] => no
)
[James] => Array
(
[rowspan] => 1
[printed] => no
)
[tom] => Array
(
[rowspan] => 2
[printed] => no
)
)
Mencetak dengan rentang baris:
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>";
Pengoptimalan Kode:
Sekarang kita dapat menggabungkan perhitungan rowspan dan pengambilan data mysql. Karena selama menyimpan data yang diambil dalam array, kita dapat menghitung rentang baris. Jadi kode terakhir kita:
<!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>
Hasil: