Ename Sal
tom 100
tom 200
bill 100
bill 250
bill 450
bill 400
This is the query and html structure which has given the
sorry for my poor english: Here I had answered this question How to show data from database with dynamic rowspan. Again let me try to answer this question. First lest us work on mysql query.
MySql Work:
In the mysql query you have not queried for order by. Because in real life, you can not expect that after all the records of tom, bills record will be there. For example take the following insertion.
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;
Result:
+-------+------+
| ename | sal |
+-------+------+
| tom | 100 |
| bill | 450 |
| bill | 100 |
| tom | 200 |
| bill | 250 |
| bill | 400 |
| James | 50 |
+-------+------+
So your mysql query should be order by ename. Here also each person's sal should be ordred . So Our query:
SELECT * FROM emp ORDER BY ename, sal;
CODING:
MySql Datafetching:
During data fetching from mysql server always we should try to use mysql_fetch_assoc function instead of mysql_fetch_array . Because mysql_fetch_assoc will return only ename and sal. But mysql_fetch_array will return array with indexes 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']);
}
Calculating Row Span:
# 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;
}
when you will print_r the arr array the output will be:
Array
(
[bill] => Array
(
[rowspan] => 4
[printed] => no
)
[James] => Array
(
[rowspan] => 1
[printed] => no
)
[tom] => Array
(
[rowspan] => 2
[printed] => no
)
)
Printing with rowspan:
echo "
Ename
Sal
";
for($i=0; $i < sizeof($sal); $i++) {
$empName = $emp[$i];
echo "";
# 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 "".$empName." ";
$arr[$empName]['printed'] = 'yes';
}
echo "".$sal[$i]." ";
echo " ";
}
echo "
";
Code Optimization:
Now we can combine the rowspan calculation and mysql data fetching. Because during saving the fetched data in array we can calculate the rowspan. So our final code:
Ename
Sal
";
for($i=0; $i < sizeof($sal); $i++) {
$empName = $emp[$i];
echo "";
# 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 "".$empName." ";
$arr[$empName]['printed'] = 'yes';
}
echo "".$sal[$i]." ";
echo " ";
}
echo "";
?>
Result: