[英]How do I display mysql data in row span using PHP?
I have data from MySQL showing all organisations a customer got, with all details of employess in each organisation. 我从MySQL获得的数据显示了客户获得的所有组织,以及每个组织中雇员的所有详细信息。 I want to list each organisation name only once ie in a single cell ( row span) and all employees in that organisation against this name like:
我只想列出每个组织名称一次,即在一个单元格(行跨度)中列出该组织的所有员工,并针对该名称列出所有雇员,例如:
Org1 Emp1 Name, Emp1 Phone, Emp1 Address
Emp2 Name, Emp2 Phone, Emp2 Address
Org2 Emp1 Name, Emp1 Phone, Emp1 Address
Emp2 Name, Emp2 Phone, Emp2 Address
How do I display this data because the number of employess for each organisation is not known in advanced, so I do'nt about setting value of rowspan. 我如何显示此数据,因为每个组织的雇员人数在高级情况下都是未知的,所以我不打算设置rowspan的值。 Similarly how do I start a row for other organisation?
同样,我如何开始其他组织的活动? Do I have to write two queries?
我必须写两个查询吗?
Many Thanks. 非常感谢。
Classic. 经典。
Workaround: only display the name if different than the previous one. 解决方法:仅在名称与前一个名称不同的情况下显示。 You can even not bother about the rowspan (you keep an empty cell).
您甚至不必理会行距(保留一个空单元格)。
$currentOrg = '';
while ($row = mysql_fetch_object($query)) {
if ($row->org != $currentOrg) {
echo "$row->org".
}
$currentorg = $row->org;
}
Not the most beautiful but so simple. 不是最美丽,而是那么简单。
// Get the data
$data = mysql_query('SELECT org, emp_name, emp_phone, emp_address FROM x');
// Store it all in a 2D array, keyed by org
$rows = array();
while ($row = mysql_fetch_assoc($data))
{
// Initialise each org to an empty array (not really needed in PHP but I prefer it)
if (empty($rows[$row['org']]))
$rows[$row['org']] = array();
$rows[$row['org']][] = $row;
}
// Print it out
foreach ($rows as $org => $employees)
{
print('<tr><td rowspan="' . count($employees) . '">' . htmlentities($org) . '</td>');
foreach ($employees as $i => $employee)
{
// If $i == 0, we've already printed the <tr> before the loop
if ($i)
print('<tr>');
print('<td>......</td></tr>');
}
}
To make a correct rowspan
, you need to know the number in advance. 要使行
rowspan
正确,您需要提前知道数字。
That leaves you with: 剩下的就是:
Personally, I would go with method number two. 就个人而言,我将采用第二种方法。 DB servers are quite efficient with counting rows, this will probably be a lot faster when there are many rows to display.
数据库服务器在计算行数方面非常有效,当要显示的行数很多时,这可能会更快。
It could be easier (but less efficient) to make a query for each organisation (plus one query to find how many organisations there are presumably). 对每个组织进行查询可能更容易(但效率较低)(加上一个查询以查找大概有多少个组织)。
A better way to do it would be to loop through the array beforehand. 更好的方法是事先遍历数组。 For example:
例如:
$sql = $mysqli->query('SELECT * FROM `organisation_members` ORDER BY `organisation` DESC');
if (!$sql || $sql->num_rows) {
// No data
} else {
$data = array();
while ($row = $sql->fetch_assoc()) {}
if (!array_key_exists($row['organisation'], $data)) {
$data[$row['organisation']] = array();
}
$data[$row['organisation']][]['name'] = $row['name'];
// ...
}
$sql->close();
echo '<table>';
foreach ($data as $org => $people) {
$people_in_org = count($data[$org]) - 1;
$counter = 0;
echo '<tr>';
echo '<td rowspan="' . $people_in_org + 1 . '">' . $org . '</td>';
while ($counter < $people_in_org) {
if (counter > 0) {
echo '<tr>';
}
echo '<td>' . $people[$counter]['name'] . '</td>';
// etc
echo '</tr>';
}
}
echo '</table>';
}
为了节省内存,您可以遍历结果集,而org是相同的,用于缓冲行;当org更改时,打印当前批次并开始缓冲下一个批次。
It won't help you with the rowspan, but look into the WITH ROLLUP
modifier. 它对行距没有帮助,但是请查看
WITH ROLLUP
修饰符。 It returns the data in a format similiar to what you want. 它以类似于您想要的格式返回数据。
What about using pear's HTML_Table package like in the following example, through i also like Jrgns's ROLLUP version 关于使用梨的HTML_Table包(如以下示例)怎么样,我也喜欢Jrgns的ROLLUP版本
<?php
require_once "HTML/Table.php";
$table = new HTML_Table(array('border'=>'1'));
$bo=array(
array('6','a2','a3','a4'),
array('1','b2','b3','b4'),
array('1','c2','c3','c4') ,
array('2','c2','c3','c4') ,
array('2','c2','c3','c4') ,
array('4','c2','c3','c4') );
foreach ($bo as $r => $borow)
$table->addRow($borow);
$rsFirst=0;
$rsLen=0;
foreach ($bo as $r => $borow) {
if ($r!=0 and $borow[0]!=$prevrow[0] ) {
//jump in values
$table->setCellAttributes ( $rsFirst,0, array('rowspan'=>$rsLen));
$rsFirst=$r;
$rsLen=0;
}
$prevrow=$borow;
$rsLen++;
if ($r==sizeof($bo) - 1) {
$table->setCellAttributes ( $rsFirst,0, array('rowspan'=>$rsLen));
}
}
echo $table->toHTML();
?>
servas, boerl 塞尔瓦斯,波尔
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.