Dynamically generate table clospan header

72 views Asked by At

I am trying to dynamically generate multilevel header table. Unfortunately after long trials I realized that I am not able to do that alone.

What I am trying to do is to merge the header cells into one cell like on the screen: enter image description here

Here is my working PHP code. Only one thing which needs to be done is trier 1 header

PHP Code:

<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>

Link to database: https://1drv.ms/u/s!AvpVESbXIIvxhrByuEodnjt5JTdgLA

1

There are 1 answers

4
nibnut On BEST ANSWER

I think I'd simply build an associative array of all your table names first, each with a count of how many columns they span, and then use that for your first header level:

$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]

And then to output your first level of headers:

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

Hope this helps!