2017-01-05 38 views
0

我想动态生成多级头表。不幸的是,经过漫长的试验后,我意识到我无法单独做到这一点。动态生成表clospan报头

我我试图做的是标题单元格合并为一个单元格一样在屏幕上: enter image description here

这是我工作的PHP代码。只有一两件事需要做的是特里尔1头

PHP代码:

<html> 
    <head> 
     <meta charset="UTF-8"> 
    </head> 
    <body> 


<!-- 
/************** 
* CSS styles * 
**************/ 
--> 

    <style type="text/css"> 
    h1 {font-family:Segoe UI;font-size:18px;} 
    h2 {font-family:Segoe UI;font-size:14px;font-weight:normal;} 
    table {border-collapse:collapse;border-spacing:0;border-color:#ccc;} 
    th {font-family:Segoe UI;font-size:13px;font-weight:bold;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#ccc;color:#333;background-color:#f0f0f0;} 
    td {font-family:Segoe UI;font-size:13px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#ccc;color:#333;background-color:#fff;} 
    </style> 


<!-- 
/********************** 
* connection details * 
**********************/ 
--> 

<?php 
    $host = "127.0.0.1"; 
    $user = "dnlw"; 
    $pass = ""; 
    $database = "ksiegarnia"; 
    $port = 3306; 
    $connect = mysql_connect($host, $user, $pass, $port); 

    if (!$connect) 
    { 
     die("Can't connect to database"); 
    } 

    if (!mysql_select_db($database)) 
    { 
     die("Can't select database"); 
    } 


/***************** 
* sending query * 
*****************/ 

    $query = file_get_contents("query.txt"); 

    mysql_query("SET NAMES 'utf8'"); 
    $result = mysql_query($query); 

    $row_num = mysql_num_rows($result); 
    $fields_num = mysql_num_fields($result); 

    if (!$result) 
    { 
     die("Query to show fields from table failed"); 
    } 


/********************** 
* printing the table * 
**********************/ 


    echo " 
    <title>Query result</title> 
    <h2>znaleziono: <b>{$row_num}</b></h2> 


    <table> 
     <thead> 
      <tr>"; 
       // ????????????????????????????????????????????? 
       for($i=0; $i<$fields_num; $i++) 
       { 
        $header = mysql_fetch_field($result); 
        echo "<th>{$header->table}</th>"; 
       } 
    echo " 
      </tr> 

      <tr>"; 
       // printing table headers 
       for($i=0; $i<$fields_num; $i++) 
       { 
        $header2 = mysql_fetch_field($result); 
        echo "<th>{$header->name}</th>"; 
       } 
    echo " 
      </tr> 
     </thead> 

     <tbody> 
      <tr>"; 
       // printing table rows 
       while($row = mysql_fetch_row($result)) 
       { 
        echo "<tr>"; 

        // $row is array... foreach(..) puts every element 
        // of $row to $cell variable 
        foreach($row as $cell) 
         echo "<td>$cell</td>"; 

        echo "</tr>\n"; 
       } 
       mysql_free_result($result); 
      echo" 
      </tr> 
     </tbody> 
    </table>"; 
    ?> 

    </body> 
</html> 

链接到数据库: https://1drv.ms/u/s!AvpVESbXIIvxhrByuEodnjt5JTdgLA

回答

0

我想我会简单地构建所有的关联数组您的表名称首先,每个都有一个计算它们跨度的列数,然后将其用于第一个标头级别:

$table_names = array(); 
for($i=0; $i<$fields_num; $i++) { // count the number of times each $header->table occurs: 
    $header = mysql_fetch_field($result); 
    if(!isset($table_names[$header->table])) $table_names[$header->table] = 1; 
    else $table_names[$header->table]++; 
} 
// at this point, you get something like $table_names = [klienci=>4, zamowienia=>5, ksiazki=>5] 

然后输出你的头的第一级:

foreach($table_names as $table_name => $nb_of_occurences) { 
    echo '<th colspan="'.$nb_of_occurences.'">'.$table_name.'</th>'; 
} 

希望这有助于!

+0

哇!太优雅了!非常感谢你!!! – Daniel

+0

当然!所以$表最终将看起来像{klienci:4,zamowienia:5,ksiazki:5} foreach循环每个项目,并将关联数组的键分配给$ table,并且它对应的值为$ span。在第一个循环中,$ table将是“klienci”,$ span将保存值“4”。这有帮助吗? – MacPrawn

+0

是的,它有帮助,但我仍然不明白为什么在第一个循环中$ span是4。据我了解,它应该从第一回路的1增加到第四回路的4 ... – Daniel