如果列 运行 离开页面,如何将由数据库数据生成的 html table 分隔在其自身下方?

How to separate one html table, generated by database data, below itself if columns run off the page?

下面是 HTML table 系统地从 MySQL 数据库生成并使用 TCPDF 生成的。每列都是根据用户输入的分析数量生成的。如图所示,一旦列数超过页面的容量,系统地创建 table 就会成为一个问题。

我想做的是只允许生成 4 列,然后附加相应的结果,如图所示,然后在第一个 table 下方生成第二个和接下来的 4 列,依此类推在。

有谁知道可以做到这一点的简单方法吗?

如果没有简单的方法,那么操纵 PHP 代码是唯一的选择。我不允许显示整个代码(客户的要求),但是,我可以显示生成列的部分。

中的$micro_analysis是以逗号分隔的数字,如:1,2,3,4对应一个主键,即分析名,从tablemicrobiology_analysis_database,就增加一些清晰度。

如有任何帮助,我们将不胜感激。

    $sqla = "SELECT * FROM samples_database WHERE order_id=$order_id AND sample_group='$group' AND $analysis!='';";
    $resulta = mysqli_query($conn, $sqla);

    $sqlb = "SELECT * FROM samples_database WHERE order_id=$order_id AND sample_group='$group' AND $analysis!='';";
    $resultb = mysqli_query($conn, $sqlb);


    $micro_analysis = '';
    while($input = mysqli_fetch_array($resultb)) {
        $micro_analysis .= $input[$analysis] . ',';
    }

    $micro_analysis_arr = array_filter(array_unique(explode(',', $micro_analysis)));

    $array_size = count($micro_analysis_arr);

    if (mysqli_num_rows($resulta) > 0) {

        $html .= '
        <table border="1" cellspacing="0" cellpadding="3">      
        <thead>
        <tr>';
        //<th><strong>Lab ID</strong></th>
        $html .= '<th width="200px"><strong>Client ID</strong></th>';

        foreach ($micro_analysis_arr as $row1) {
            $query = "SELECT * FROM microbiology_analysis_database WHERE id=$row1 AND analysis_type='$analysis_type';"; 
            $resultc = mysqli_query($conn, $query);
            $inputc = mysqli_fetch_array($resultc);


            if (empty($inputc['analysis_sample_type'])) {
                $html .= '<th style="text-align:center;"><strong>'.$inputc['analysis_name'].'<br /> ('.$inputc['analysis_unit'].')</strong></th>';
            } else {
                $html .= '<th style="text-align:center;"><strong>'.$inputc['analysis_name'].'<br /> ('.$inputc['analysis_unit'].')</strong></th>';
            }
        }

        $html .= '
        </tr>                           
        </thead>
        <tbody>';

while ($row = mysqli_fetch_assoc($resulta)) {

                    $sam_id = $row['sample_id'];

                    $sql3 = "SELECT * FROM samples_database WHERE order_id=$order_id AND $analysis!='' AND sample_group='$group' AND sample_id=$sam_id;";
                    $result3 = mysqli_query($conn, $sql3);

                    $micro_analysis2 = '';

                    while($input = mysqli_fetch_array($result3)) {
                        $micro_analysis2 .= $input[$analysis] . ',';
                    }
                    $micro_analysis_arr_2 = array_filter(array_unique(explode(',', $micro_analysis2)));


                    $html .= '
                    <tr>'; 
                //<td>'.$row['env_sam_id'].'</td>
                    $html .= '<td>'.$row['c_sam_id'].'</td>';

                    foreach ($table as $row2) {

                //if statement to select sample with corresponding analysis

                        if (!in_array($row2, $micro_analysis_arr_2)) {

                            $html .= '<td></td>';

                        } else {

                            $query5 = "SELECT * FROM results_database WHERE order_id=$order_id AND sample_id=$sam_id AND m_analysis_id=$row2"; 
                            $result5 = mysqli_query($conn, $query5);
                            $input5 = mysqli_fetch_array($result5);
                            $result_id = $input5['id']; 

                            $query6 = "SELECT * FROM analysis_database WHERE order_id=$order_id AND m_analysis_id=$row2"; 
                            $result6 = mysqli_query($conn, $query6);
                            $input6 = mysqli_fetch_array($result6);
                            $step_10_progress = $input6['step_10_progress'];

                            if (empty($input5['result'])) {

                                $html .= '<td style="text-align:center;"><em>Analysis Pending</em></td>'; 

                            } else {

                                $html .= '<td style="text-align:center;">'.htmlspecialchars($input5['result']).'</td>';

                            }                   
                        }
                    }

                    $html .= '</tr>';
                }       

                $html .= '</tbody></table>';
            }
        }
    }

听起来你想使用 array_chunk 函数,然后循环遍历结果数组,如下所示:

    $sqla = "SELECT * FROM samples_database WHERE order_id=$order_id AND sample_group='$group' AND $analysis!='';";
    $resulta = mysqli_query($conn, $sqla);

    $sqlb = "SELECT * FROM samples_database WHERE order_id=$order_id AND sample_group='$group' AND $analysis!='';";
    $resultb = mysqli_query($conn, $sqlb);


    $micro_analysis = '';
    while($input = mysqli_fetch_array($resultb)) {
        $micro_analysis .= $input[$analysis] . ',';
    }

    $micro_analysis_arr = array_filter(array_unique(explode(',', $micro_analysis)));

    $array_size = count($micro_analysis_arr);

    // Chunk the array into smaller parts
    $tables = array_chunk($micro_analysis_arr, 4);

    if (mysqli_num_rows($resulta) > 0) {

        // Loop through 4-part arrays as unique tables
        foreach($tables as $table) {

        $html .= '
        <table border="1" cellspacing="0" cellpadding="3">      
        <thead>
        <tr>';
        //<th><strong>Lab ID</strong></th>
        $html .= '<th width="200px"><strong>Client ID</strong></th>';

        foreach ($table as $row1) {
            $query = "SELECT * FROM microbiology_analysis_database WHERE id=$row1 AND analysis_type='$analysis_type';"; 
            $resultc = mysqli_query($conn, $query);
            $inputc = mysqli_fetch_array($resultc);


            if (empty($inputc['analysis_sample_type'])) {
                $html .= '<th style="text-align:center;"><strong>'.$inputc['analysis_name'].'<br /> ('.$inputc['analysis_unit'].')</strong></th>';
            } else {
                $html .= '<th style="text-align:center;"><strong>'.$inputc['analysis_name'].'<br /> ('.$inputc['analysis_unit'].')</strong></th>';
            }
        }

        $html .= '
        </tr>                           
        </thead>
        <tbody>';

while ($row = mysqli_fetch_assoc($resulta)) {

                    $sam_id = $row['sample_id'];

                    $sql3 = "SELECT * FROM samples_database WHERE order_id=$order_id AND $analysis!='' AND sample_group='$group' AND sample_id=$sam_id;";
                    $result3 = mysqli_query($conn, $sql3);

                    $micro_analysis2 = '';

                    while($input = mysqli_fetch_array($result3)) {
                        $micro_analysis2 .= $input[$analysis] . ',';
                    }
                    $micro_analysis_arr_2 = array_filter(array_unique(explode(',', $micro_analysis2)));


                    $html .= '
                    <tr>'; 
                //<td>'.$row['env_sam_id'].'</td>
                    $html .= '<td>'.$row['c_sam_id'].'</td>';

                    foreach ($table as $row2) {

                //if statement to select sample with corresponding analysis

                        if (!in_array($row2, $micro_analysis_arr_2)) {

                            $html .= '<td></td>';

                        } else {

                            $query5 = "SELECT * FROM results_database WHERE order_id=$order_id AND sample_id=$sam_id AND m_analysis_id=$row2"; 
                            $result5 = mysqli_query($conn, $query5);
                            $input5 = mysqli_fetch_array($result5);
                            $result_id = $input5['id']; 

                            $query6 = "SELECT * FROM analysis_database WHERE order_id=$order_id AND m_analysis_id=$row2"; 
                            $result6 = mysqli_query($conn, $query6);
                            $input6 = mysqli_fetch_array($result6);
                            $step_10_progress = $input6['step_10_progress'];

                            if (empty($input5['result'])) {

                                $html .= '<td style="text-align:center;"><em>Analysis Pending</em></td>'; 

                            } else {

                                $html .= '<td style="text-align:center;">'.htmlspecialchars($input5['result']).'</td>';

                            }                   
                        }
                    }

                    $html .= '</tr>';
                }       

                $html .= '</tbody></table>';

                }
            }
        }
    }