簡體   English   中英

如何使用PHP在行跨度中顯示mysql數據?

[英]How do I display mysql data in row span using PHP?

我從MySQL獲得的數據顯示了客戶獲得的所有組織,以及每個組織中雇員的所有詳細信息。 我只想列出每個組織名稱一次,即在一個單元格(行跨度)中列出該組織的所有員工,並針對該名稱列出所有雇員,例如:

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

我如何顯示此數據,因為每個組織的雇員人數在高級情況下都是未知的,所以我不打算設置rowspan的值。 同樣,我如何開始其他組織的活動? 我必須寫兩個查詢嗎?

非常感謝。

經典。

解決方法:僅在名稱與前一個名稱不同的情況下顯示。 您甚至不必理會行距(保留一個空單元格)。

$currentOrg = '';
while ($row = mysql_fetch_object($query)) {
   if ($row->org != $currentOrg) {
      echo "$row->org".
   }
   $currentorg = $row->org;
}

不是最美麗,而是那么簡單。

// 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>');
    }
}

要使行rowspan正確,您需要提前知道數字。

剩下的就是:

  • 將查詢結果重復兩次,對值進行計數,直到它們更改為止
  • 向數據庫服務器詢問計數

就個人而言,我將采用第二種方法。 數據庫服務器在計算行數方面非常有效,當要顯示的行數很多時,這可能會更快。

對每個組織進行查詢可能更容易(但效率較低)(加上一個查詢以查找大概有多少個組織)。

更好的方法是事先遍歷數組。 例如:

$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更改時,打印當前批次並開始緩沖下一個批次。

它對行距沒有幫助,但是請查看WITH ROLLUP修飾符。 它以類似於您想要的格式返回數據。

關於使用梨的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();

    ?>

塞爾瓦斯,波爾

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM