2008-10-20 127 views
1

我有來自MySQL的數據顯示了一個客戶得到的所有組織,以及每個組織中employess的所有詳細信息。我想列出每一個組織的名字只有一次在一個單元(行間距),即在該組織所有員工對這個名字一樣:如何使用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 

如何顯示這個數據,因爲employess的每個組織的數量在高級中是不知道的,所以我不想設置rowspan的值。同樣,我如何開始爲其他組織開一排?我必須寫兩個查詢嗎?

很多謝謝。

回答

4

經典。

解決方法:只顯示與上一個名稱不同的名稱。你甚至可以不用擔心行間距(你保留一個空單元格)。

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

不是最美麗但非常簡單。

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

我投了你的答案是唯一的人至今使用htmlentities()來轉義輸出。不過,我可能會在這裏使用sprintf()而不是連接。 – 2008-10-20 18:16:13

0

要做出正確的rowspan,您需要提前知道該號碼。

這使得您:

  • 迭代查詢結果兩次,計數的值,直到他們改變
  • 詢問DB服務器爲計數

就個人而言,我會去的方法第二。數據庫服務器對計數行非常有效,當顯示很多行時,這可能會快很多。

+0

如果您使用INNODB引擎,我不會推薦第二種方法。 – Gaurav 2008-10-20 16:34:30

0

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

更好的方法是預先循環訪問數組。例如:

$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>'; 
} 
0

爲了節省內存,你可以遍歷結果集,而組織是相同的緩衝行,當組織的變化,打印當前的批次,並開始緩衝下一批次。

0

它不會幫助您使用rowspan,但請查看WITH ROLLUP修改器。它以與您想要的相似的格式返回數據。

0

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

    ?> 

塞爾瓦斯,boerl